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

Java导出Excel数据

2017-03-18 18:59 281 查看
导出已准备好的数据集合List

// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("联系人表");
//表头字体
HSSFFont headerFont = (HSSFFont) wb.createFont();
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 字体加粗
headerFont.setFontName("仿宋");
headerFont.setFontHeightInPoints((short)15);
HSSFFont contentFont = (HSSFFont) wb.createFont();
contentFont.setFontName("仿宋");
contentFont.setFontHeightInPoints((short)10);
sheet.setColumnWidth((short)0, (short)3000);
sheet.setColumnWidth((short)1, (short)3000);
sheet.setColumnWidth((short)2, (short)6000);
sheet.setColumnWidth((short)3, (short)9000);
sheet.setColumnWidth((short)4, (short)5000);
sheet.setColumnWidth((short)5, (short)5000);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
row.setHeight((short)600);
// 第四步,创建单元格,并设置值表头 设置表头居中

HSSFCellStyle style1 = wb.createCellStyle();
style1.setFont(contentFont);
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 创建一个居中格式
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//表头样式
HSSFCellStyle style = wb.createCellStyle();
style.setFont(headerFont);
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
//style.setFillBackgroundColor(HSSFColor.GOLD.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置颜色
style.setBorderBottom((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 创建一个居中格式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("序号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("手机");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("公司");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("职务");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("创建日期");
cell.setCellStyle(style);

for (int i = 0; i < list.size(); i++){
row = sheet.createRow((int) i + 1);
Contacts ctt=list.get(i);
// 第四步,创建单元格,并设值
cell = row.createCell((short) 0);
cell.setCellStyle(style1);
cell.setCellValue(i+1);
//姓名
cell = row.createCell((short) 1);
cell.setCellStyle(style1);
cell.setCellValue(ctt.getName());
//手机
cell = row.createCell((short) 2);
cell.setCellStyle(style1);
cell.setCellValue(ctt.getPhone());
//公司
cell = row.createCell((short) 3);
cell.setCellStyle(style1);
cell.setCellValue(ctt.getCompany());
//职务
cell = row.createCell((short) 4);
cell.setCellStyle(style1);
cell.setCellValue(ctt.getJob());
//创建日期
cell = row.createCell((short) 5);
cell.setCellStyle(style1);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cell.setCellValue(sdf.format(ctt.getCreateTime()));
}
//创建文件exl
String fileName="联系人表.xls";
response.reset();//清空response的buffer
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-disposition", "inline; filename=\"" +  new String(fileName.getBytes("gbk"), "ISO8859-1" )  + "\"");
ServletOutputStream sos=response.getOutputStream();
wb.write(sos);
sos.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: