java 生成Excel
2017-04-05 15:02
417 查看
首先网上关于代码创建excel大致分为两种:
1.采用poi;
2.采用jxl;
(菜鸟个人观点,当然也有很多现成的工具类等等)
我所采用的是poi的方式:
首先创建Workbook(poi提供了两种):HSSFWorkbook、XSSFWorkbook
注:HSSFWorkbook 是后缀为xls(2003-2007)版本
XSSFWorkbook 为2007以上版本
接下来创建sheet页内容
1、Title
注:sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
合并单元格
(注意包的路径:org.apache.poi.ss.util.CellRangeAddress,有的为org.apache.poi.hssf.util.CellRangeAddress已废弃)
方法的参数(index坐标从0开始)
firstRow :起始行;
lastRow:结束行;
firstCol:起始单元格;
lastCol:结束单元格;
2、单元格内容
到这里 一个excel就已用java创建完毕
接着需要把生成的Excel输出
以上为demo关键代码;小菜献上(注代码逻辑还需重新排列)
1.采用poi;
2.采用jxl;
(菜鸟个人观点,当然也有很多现成的工具类等等)
我所采用的是poi的方式:
首先创建Workbook(poi提供了两种):HSSFWorkbook、XSSFWorkbook
注:HSSFWorkbook 是后缀为xls(2003-2007)版本
XSSFWorkbook 为2007以上版本
HSSFWorkbook xls = new HSSFWorkbook();//创建Workbook HSSFSheet sheet = xls.createSheet("newsheet");//创建sheet HSSFCellStyle style = xls.createCellStyle();//设置单元格格式 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
接下来创建sheet页内容
1、Title
HSSFRow row = sheet.createRow((int) 0); //合并单元格 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1)); HSSFCell cell = row.createCell(0); cell.setCellValue("区域"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("商品名称"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("当日兑换量"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("成本价"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("结算单价"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("销售价"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("当日兑换金额"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("当日收入"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("累计兑换量"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("累计收入"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("累计兑换金额");
注:sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 1));
合并单元格
(注意包的路径:org.apache.poi.ss.util.CellRangeAddress,有的为org.apache.poi.hssf.util.CellRangeAddress已废弃)
方法的参数(index坐标从0开始)
firstRow :起始行;
lastRow:结束行;
firstCol:起始单元格;
lastCol:结束单元格;
2、单元格内容
HSSFRow row = sheet.createRow(1); sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0)); HSSFCell cell = row.createCell(0); cell.setCellValue("江苏"); cell.setCellStyle(style); cell = row.createCell(1); sheet.addMergedRegion(new CellRangeAddress(1, 2, 1, 1)); 4000 cell.setCellValue("移动"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("aaa"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("8"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("20.00"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("32.98"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("40.00"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("320.00"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("263.84"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("1110"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("36607.80"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("44400.00"); cell.setCellStyle(style); row = sheet.createRow(2); cell = row.createCell(2); cell.setCellValue("aaa"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("8"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("20.00"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("32.98"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("40.00"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("320.00"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("263.84"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("1110"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("36607.80"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("44400.00"); cell.setCellStyle(style); row = sheet.createRow(3); sheet.addMergedRegion(new CellRangeAddress(3, 3, 1, 1)); cell = row.createCell(1); cell.setCellValue("电信"); cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue("aaa"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("8"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("20.00"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("32.98"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("40.00"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("320.00"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("263.84"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("1110"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("36607.80"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("44400.00"); cell.setCellStyle(style); row = sheet.createRow(4); sheet.addMergedRegion(new CellRangeAddress(4, rowIndex, 0, 2)); cell = row.createCell(0); cell.setCellValue("江苏合计"); cell.setCellStyle(style); cell = row.createCell(3); cell.setCellValue("2"); cell.setCellStyle(style); cell = row.createCell(4); cell.setCellValue("-"); cell.setCellStyle(style); cell = row.createCell(5); cell.setCellValue("-"); cell.setCellStyle(style); cell = row.createCell(6); cell.setCellValue("-"); cell.setCellStyle(style); cell = row.createCell(7); cell.setCellValue("320"); cell.setCellStyle(style); cell = row.createCell(8); cell.setCellValue("263.84"); cell.setCellStyle(style); cell = row.createCell(9); cell.setCellValue("111"); cell.setCellStyle(style); cell = row.createCell(10); cell.setCellValue("3688"); cell.setCellStyle(style); cell = row.createCell(11); cell.setCellValue("4200"); cell.setCellStyle(style);
到这里 一个excel就已用java创建完毕
接着需要把生成的Excel输出
String webParentPath = new File(request.getSession().getServletContext().getRealPath("/")).getParent();// 当前WEB环境的上层目录 String realPath = webParentPath + "/file/";// 文件上传到服务器的真实路径 File f = new File(realPath); try{ if (!f.isDirectory()) { f.mkdirs(); } FileOutputStream xlsxfileOut = new FileOutputStream(realPath+"123.xls"); xls.write(xlsxfileOut); xlsxfileOut.close();
以上为demo关键代码;小菜献上(注代码逻辑还需重新排列)
相关文章推荐
- Java生成Excel文件通用程序
- java生成Excel
- Java Excel API jxl入门及jxl.jar下载(java生成excel文件)
- java生成Excel文件 xls
- 关于java生成excel格式
- 一个简单生成Excel文件格式的java包。
- JAVA 操作 excel 并生成 xml
- java中生成Excel表的封装类
- java操作Oracle存储过程,以及生成Excel报表
- JXL生成的excel文件+JAVA文件下载功能
- java-web-PIO实现自动生成excel录入模板(实现下拉列表框)[原创]
- java生成excel又一篇
- JAVA生成EXCEL文件
- java中生成Excel表的封装类
- Java操作Excel之理解JXL--读取Excel模板动态写入数据并生成Excel
- java 生成 pdf excel
- 使用开源项目JExcelApi在Java环境中生成MS Excel文档
- 使用Excel自动生成Java中的Model类
- java 生成excel
- Java POI 生成Excel时显示货币格式,如: 23,332.00