您的位置:首页 > 编程语言 > Java开发

java数据记录导出到Excel

2016-08-17 20:10 225 查看
在这里介绍的主要是用 Apache POI的方式来导出。首先下载到官方(https://www.apache.org/dyn/closer.lua/poi/dev/bin/poi-bin-3.15-beta2-20160702.tar.gz)下载jar包,所需要的jar包名称如下,下面来直接看代码:

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;

import org.apache.poi.hssf.usermodel.HSSFComment;

import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFFooter;

import org.apache.poi.hssf.usermodel.HSSFPatriarch;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.util.HSSFColor;

import org.apache.poi.hssf.util.Region;


</pre><pre name="code" class="java">public static void commonExportData(String title,Object[] obj, Vector v, HttpServletResponse response)
{
response.setContentType("application/vnd.ms-excel");

if ((title == null) || (title.equals(""))){
title = "合同导出";
}

String sheetName = title;
try
{
title = title + "_"+DateUtil.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss");
title = new String(title.getBytes("gb2312"), "iso-8859-1");

response.setHeader("Content-Disposition", "attachment;filename=" + title + ".xls");
response.setHeader("Pragma", "no-cache");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet();

wb.setSheetName(0, sheetName.trim());
sheet1.setDefaultRowHeightInPoints(20.0F);
sheet1.setDefaultColumnWidth((short)18);

HSSFFooter footer = sheet1.getFooter();
footer.setRight("Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages());

HSSFCellStyle style1 = wb.createCellStyle();
style1.setAlignment((short)2);
HSSFFont font1 = wb.createFont();
font1.setFontHeightInPoints((short)13);
font1.setBoldweight((short)700);
style1.setFont(font1);

HSSFCellStyle style2 = wb.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = wb.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);

/*HSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment((short)2);
style2.setWrapText(true);
style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);*/
//sheet1.addMergedRegion(new Region(0, (short)0, 0, (short)(obj.length - 2)));

/*HSSFCellStyle style3 = wb.createCellStyle();
style3.setAlignment((short)2);
HSSFFont font3 = wb.createFont();
font3.setFontHeightInPoints((short)20);
font3.setBoldweight((short)700);
style3.setFont(font3);
HSSFRow row0 = sheet1.createRow(0);
row0.setHeightInPoints(35.0F);

HSSFCell cell0 = row0.createCell((short)0);

cell0.setCellValue(obj[0].toString());
cell0.setCellStyle(style3);*/

HSSFRow row1 = sheet1.createRow(0);
row1.setHeightInPoints(20.0F);
for (int i = 0; i < obj.length; i++) {
HSSFCell cell1 = row1.createCell((short)(i));

cell1.setCellValue(obj[i].toString());
cell1.setCellStyle(style1);
}

for (int j = 0; j < v.size(); j++) {
HSSFRow row2 = sheet1.createRow((short)(j + 1));
Object[] o = (Object[])v.get(j);
for (int k = 0; k < o.length; k++) {
HSSFCell cell = row2.createCell((short)k);

cell.setCellValue(o[k] == null ? "" : o[k].toString());
cell.setCellStyle(style2);
}
}
wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
}
catch (Exception localException)
{
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java excel 数据