JAVA语言操作excel的api主流有两种方式:
1、POI Apache它是用来操作Office所有的软件excel/word/ppt/..。而且支持所有版本。
2、JXL 它是用来操作excel 2003以下版本,2007以上版本不支持。
早期微软Office系列,当时使用OLE2文档结构;微软在开发Office2207版本时,做了一个重大的改革。重写了Office,使用OOXML文档结构。现在excel文件实际上是一个xml格式文件。
POI支持OLE2格式文件,还支持OOXML,而且在OOXML格式文件时做了很大的优化。
JXL只支持OLE2格式文件。
POI低版本有个致命弱点,当数据量超大(海量数据),性能极具降低。JXL它解决数量大时性能的问题。POI在高版本时迎头追上,加了一个补丁。OOXML支持,解决大数量时的性能问题。
http://poi.apache.org/index.html POI 全方位的操作
http://www.andykhan.com/jexcelapi/index.html JXL 导入导出
java.lang.IllegalArgumentException: Invalid column index(256). Allowable column range for BIFF8 is(0..255) or (‘A’..’IV’)
java.lang.IllegalArgumentException: Invalid row number (65536)outside allowable range (0..65535)
HSSF对象支持excel 2003,excel 2003它支持行65536,支持列256
XSSF对象支持excel2007版本以上,行1048576,支持列65536。Sheet
SXSSF对象,支持excel 2007版本以上,构造函数。不支持模板方式。只能作为数据导出。
SXSSF如何实现
HSSF/XSSF它们在构建workbook中所有对象,全部放在内存中,数据全放在内存中,wb.write时才从内存向磁盘文件中写。写完才释放内存。数据量大,堆溢出。
SXSSF构造方法Workbook wb = new SXSSFWorkbook(100);
每处理完100个对象,将它写临时文件,这部分数据所占的内存,就释放。
上面代码存在的问题:
1) POI创建的这些对象统统在内存中
2) 行对象,列对象,样式对象,字体对象重复创建
15. 常规poi打印存在问题:
1) 列的宽度不精确,272非常相近
2) 设置纸张方向
3) 设置页眉页脚
4) 标题栏
5) 内容样式稍有不同,就需要创建不同的样式,写很多样式应用的语句
6) 表格头,写死表头,静态的文字方便维护
16. POI操作EXCEL,复杂 表终极解决方案~模板开发
1) 代码量急剧降低
2) POI日常API,将利用模板直接来设置,可视化修改。
3) 业务功能非常方便维护。
- //模板打印
- @RequestMapping("/cargo/outproduct/printTemplate.action")
-
public void printTemplate(String inputDate, HttpServletRequestrequest, HttpServletResponse response) throws IOException{
-
List dataList = outProductService.find(inputDate);
- //打开模板文件
-
String path =request.getSession().getServletContext().getRealPath("/"); //jdk1.8bug 在linux下不带后面你写的路径
-
String tempFile = path +"/make/xlsprint/tOUTPRODUCT.xls";
-
Workbook wb = new HSSFWorkbook(new FileInputStream(newFile(tempFile)));
- //写入业务内容
-
Sheet sheet = wb.getSheetAt(0); //获得工作表sheet
-
Row nRow = null;
-
Cell nCell = null;
-
int rowNo = 2;
-
int colNo = 1;
- //获取样式
- nRow = sheet.getRow(2); //获得行对象
- nCell = nRow.getCell(1); //获取单元格对象
-
CellStyle customNameStyle = nCell.getCellStyle();//获取到样式
- nCell = nRow.getCell(2);
-
CellStyle contractNoStyle = nCell.getCellStyle();
- nCell = nRow.getCell(3);
-
CellStyle productNoStyle = nCell.getCellStyle();
- nCell = nRow.getCell(4);
-
CellStyle cnumberStyle = nCell.getCellStyle();
- nCell = nRow.getCell(5);
-
CellStyle factoryStyle = nCell.getCellStyle();
- nCell = nRow.getCell(6);
-
CellStyle extStyle = nCell.getCellStyle();
- nCell = nRow.getCell(7);
-
CellStyle dateStyle = nCell.getCellStyle();
- nCell = nRow.getCell(9);
-
CellStyle tradeStyle = nCell.getCellStyle();
- //大标题
- nRow = sheet.getRow(0);
- nCell = nRow.getCell(1);
- nCell.setCellValue(inputDate.replaceFirst("-0","-").replaceFirst("-", "年") + "月份出货表"); //yyyy-MM
- //处理数据
-
for(int j=0;j
-
colNo = 1; //列 初始化
-
OutProductVO op = dataList.get(j); //获取出货表对象
- nRow = sheet.createRow(rowNo++);
- nRow.setHeightInPoints(24);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getCustomName());
- nCell.setCellStyle(customNameStyle); //设置样式
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getContractNo());
- nCell.setCellStyle(contractNoStyle);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getProductNo());
- nCell.setCellStyle(productNoStyle);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getCnumber());
- nCell.setCellStyle(cnumberStyle);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getFactoryName());
- nCell.setCellStyle(factoryStyle);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getExts());
- nCell.setCellStyle(extStyle);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getDeliveryPeriod());
- nCell.setCellStyle(dateStyle);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getShipTime());
- nCell.setCellStyle(dateStyle);
- nCell = nRow.createCell(colNo++);
- nCell.setCellValue(op.getTradeTerms());
- nCell.setCellStyle(tradeStyle);
- }
- //下载
-
DownloadUtil du = new DownloadUtil();
-
ByteArrayOutputStream os = new ByteArrayOutputStream();
- wb.write(os);
- du.download(os, response, "出货表.xls");
- }
-
public class DownloadUtil {
-
protected void download(String filePath,StringreturnName,HttpServletResponse response,boolean delFlag){
-
this.prototypeDownload(new File(filePath), returnName,response, delFlag);
- }
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!