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();
相关文章推荐
- java中jxl导出数据到excel的例子
- 将数据库中的数据用java程序导出成Excel
- 用Java读取Excel表数据和将数据导出成Excel表
- java——导出数据到Excel
- java中使用JXL对Excel文件进行数据的写入、导出操作
- Java利用xml将大批量数据导出到excel的一个方法
- java中将数据和图片导出到Excel文件中
- Java中将JTable中的数据导出到Excel中
- Java操作Excel(二)将数据库中的数据导出到Excel中
- 将数据导出到Excel(java操作Excel)
- Java数据导入导出Excel
- Java数据导出到excel
- Java中读取Excel文件的内容和导出数据到Excel文件
- java导出数据到excel的几种方法的比较
- Java 使用jxl.jar 导入导出Excel数据
- Java操作Excel(二)将数据库中的数据导出到Excel中
- java中jxl导出数据到excel的例子
- Java数据导出到excel和word
- java中jxl导出数据到excel的例子
- [JAVA]Java导出Excel数据的一种方法(完美实现,包含表格线)