HSSF,XSSF和SXSSF的区别与HSSF测试Demo

来源:互联网 发布:php离线手册下载 编辑:程序博客网 时间:2024/04/24 03:12
HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 
XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 
从POI 3.8版本开始,提供了一种基于XSSF的低内存占用的API----SXSSF 
SXSSF通过一个滑动窗口来限制访问Row的数量从而达到低内存占用的目录,XSSF可以访问所有行。旧的行数据不再出现在滑动窗口中并变得无法访问,与此同时写到磁盘上。 
在自动刷新的模式下,可以指定窗口中访问Row的数量,从而在内存中保持一定数量的Row。当达到这一数量时,在窗口中产生新的Row数据,并将低索引的数据从窗口中移动到磁盘中。 
或者,滑动窗口的行数可以设定成自动增长的。它可以根据需要周期的根据一次明确的flushRow(int keepRows)调用来进行修改。 
注意:针对 SXSSF Beta 3.8下,会有临时文件产生,比如: 
poi-sxssf-sheet4654655121378979321.xml 
文件位置:java.io.tmpdir这个环境变量下的位置 
Windows 7下是C:\Users\xxxxxAppData\Local\Temp 
Linux下是 /var/tmp/ 
要根据实际情况,看是否删除这些临时文件
 

官方也提供了一些解决方式: 
https://issues.apache.org/bugzilla/show_bug.cgi?id=53493 
与XSSF的对比 
在一个时间点上,只可以访问一定数量的数据 
不再支持Sheet.clone() 
不再支持公式的求值 

特性汇总 

package com.visec.sysManager.userSyslog.util;import java.io.File;import java.io.FileOutputStream;import java.util.Date;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import com.talent.tdp.base.util.DateStyle;import com.visec.sysManager.userSyslog.domain.UserSyslogVo;/** * <p>测试案例</p> * 导出系统部分数据excel表 * @author visec、Dana * [测试 HSSF系列性能Demo] */public class LogManagerUtil {/** * 导出日志 * @param list  需要导出的数据集合 * @param headName 表头名 * @return */public static String exprotExcelSystem(List<UserSyslogVo> list,String[] headName){String fileName = new LogManagerUtil().getItemPath();//删除之前导出的日志信息File fis = new File(fileName);if(fis.isDirectory()){for(File fi:fis.listFiles()){if(fi.getName().indexOf("systemLog")>-1){fi.delete();}}}try {HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet();HSSFRow row = sheet.createRow((short)0);HSSFCellStyle style = LogManagerUtil.getCellStyle(wb);HSSFCell cell = null;int count = headName.length;//创建列标题for(int i = 0;i<count;i++){cell = row.createCell(i+1);//改变cell样式LogManagerUtil.setCellStyle(cell, wb, 230,style);// 定义单元格为字符串类型cell.setCellValue(headName[i]);}//填充值信息count = list.size();HSSFRow valueRow = null;UserSyslogVo vo = null;for(int i = 0;i<count;i++){valueRow = sheet.createRow(i+1);vo = (UserSyslogVo)list.get(i);cell = valueRow.createCell(1);LogManagerUtil.setCellStyle(cell, wb, 200,style);cell.setCellValue(i+1);//序号cell = valueRow.createCell(2);LogManagerUtil.setCellStyle(cell, wb, 200,style);cell.setCellValue(vo.getLogObject());//操作用户cell = valueRow.createCell(3);LogManagerUtil.setCellStyle(cell, wb, 200,style);cell.setCellValue(vo.getLogTime());//日志时间cell = valueRow.createCell(4);LogManagerUtil.setCellStyle(cell, wb, 200,style);cell.setCellValue(vo.getContent());//日志内容cell = valueRow.createCell(5);LogManagerUtil.setCellStyle(cell, wb, 200,style);cell.setCellValue(vo.getTypeDetail());//日志类型cell = valueRow.createCell(6);LogManagerUtil.setCellStyle(cell, wb, 200,style);cell.setCellValue(vo.getLogIp());//ip地址}fileName+="systemLog"+DateStyle.getCurrentDate(new Date())+".xls";FileOutputStream fileOut = new FileOutputStream(fileName);wb.write(fileOut);fileOut.close();} catch (Exception e) {e.printStackTrace();}return fileName;}/** * 改变单元格样式 * @param cell需要改变的单元格 * @return */public static HSSFCell setCellStyle(HSSFCell cell,HSSFWorkbook wb,int num,HSSFCellStyle cellStyle){cell.setCellType(HSSFCell.ENCODING_UTF_16);// 设置单元格字体HSSFFont font = wb.createFont();font.setFontName("宋体");font.setFontHeight((short) num);cellStyle.setFont(font);cell.setCellStyle(cellStyle); return cell;}/** * 获取单元格样式 * @param wb * @return */public static HSSFCellStyle getCellStyle(HSSFWorkbook wb){HSSFCellStyle cellStyle = wb.createCellStyle();cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐cellStyle.setWrapText(true);// 指定单元格自动换行return cellStyle;}/** * 获取项目名称 * @return */public String getItemPath(){String strPath = this.getClass().getResource("/").toString();String osName = System.getProperties().getProperty("os.name");if(osName.toLowerCase().indexOf("window")>-1){//windows 操作系统strPath = strPath.substring(6, strPath.lastIndexOf("WEB-INF"));}else{//linux 操作系统strPath = strPath.substring(5, strPath.lastIndexOf("WEB-INF"));}return strPath+"tdp/upload/";}}



0 0
原创粉丝点击