《poi大数据导出20万没内存溢出过》由会员分享,可在线阅读,更多相关《poi大数据导出20万没内存溢出过(5页珍藏版)》请在金锄头文库上搜索。
1、专针对导出excel2007 ,用到poi3.9的jarpackage com.cares.ynt.util;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;i
2、mport org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.xssf.streaming.SXSSFCell;import org.apache.poi.xssf.streaming.SXSSFRow;import org.apache.poi.xssf.streaming.SXSSFSheet;import org.apache.poi.xssf
3、.streaming.SXSSFWorkbook;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFCellStyle;import org.apache.poi.xssf.usermodel.XSSFDataFormat;import org.apache.poi.xssf.usermodel.XSSFFont;public class ExportData private XSSFCellStyle head_Style;private SXSSFWorkbook w
4、orkbook;/ 当前sheetprivate SXSSFSheet sheet;private SXSSFRow row = null;/ 创建一行private SXSSFCell cell = null;private String headers;private int currentRow = 0;private XSSFCellStyle date_Style ;private XSSFCellStyle time_Style ;private XSSFCellStyle string_style;private SimpleDateFormat sdf = new Simple
5、DateFormat(yyyy/MM/dd HH:mm:ss);/* * 构造函数初始化参数 * param out * param title * param headers * param sheeatName */public ExportData(String title,String headers,String sheeatName)this.headers = headers;tryworkbook=new SXSSFWorkbook();this.head_Style=(XSSFCellStyle) this.workbook.createCellStyle();head_St
6、yle.setBorderBottom(XSSFCellStyle.BORDER_THIN);head_Style.setBorderLeft(XSSFCellStyle.BORDER_THIN);head_Style.setBorderRight(XSSFCellStyle.BORDER_THIN);head_Style.setBorderTop(XSSFCellStyle.BORDER_THIN);head_Style.setFillForegroundColor(IndexedColors.AQUA.getIndex();head_Style.setFillPattern(CellSty
7、le.SOLID_FOREGROUND);head_Style.setAlignment(XSSFCellStyle.ALIGN_CENTER);XSSFFont head_font = (XSSFFont) workbook.createFont();head_font.setFontName(宋体);/ 设置头部字体为宋体head_font.setBoldweight(Font.BOLDWEIGHT_BOLD); / 粗体head_font.setFontHeightInPoints(short) 11);this.head_Style.setFont(head_font);/ 单元格样式
8、使用字体XSSFDataFormat format = (XSSFDataFormat) workbook.createDataFormat();XSSFFont data_font = (XSSFFont) workbook.createFont();data_font.setFontName(宋体);/ 设置头部字体为宋体data_font.setFontHeightInPoints(short) 10);this.date_Style = (XSSFCellStyle) this.workbook.createCellStyle();date_Style.setBorderBottom(
9、XSSFCellStyle.BORDER_THIN);date_Style.setBorderLeft(XSSFCellStyle.BORDER_THIN);date_Style.setBorderRight(XSSFCellStyle.BORDER_THIN);date_Style.setBorderTop(XSSFCellStyle.BORDER_THIN);date_Style.setAlignment(XSSFCellStyle.ALIGN_CENTER);date_Style.setFont(data_font);/ 单元格样式使用字体date_Style.setDataFormat
10、(format.getFormat(yyyy-m-d);this.time_Style = (XSSFCellStyle) this.workbook.createCellStyle();time_Style.setBorderBottom(XSSFCellStyle.BORDER_THIN);time_Style.setBorderLeft(XSSFCellStyle.BORDER_THIN);time_Style.setBorderRight(XSSFCellStyle.BORDER_THIN);time_Style.setBorderTop(XSSFCellStyle.BORDER_TH
11、IN);time_Style.setAlignment(XSSFCellStyle.ALIGN_CENTER);time_Style.setFont(data_font);/ 单元格样式使用字体time_Style.setDataFormat(format.getFormat(yyyy-m-d h:mm:s);this.string_style = (XSSFCellStyle) this.workbook.createCellStyle();string_style.setBorderBottom(XSSFCellStyle.BORDER_THIN);string_style.setBord
12、erLeft(XSSFCellStyle.BORDER_THIN);string_style.setBorderRight(XSSFCellStyle.BORDER_THIN);string_style.setBorderTop(XSSFCellStyle.BORDER_THIN);string_style.setAlignment(XSSFCellStyle.ALIGN_CENTER);string_style.setFont(data_font);/ 单元格样式使用字体 createSheet( sheeatName,headers);catch(Exception exc)exc.pri
13、ntStackTrace();/* * 创建表头 * param sheetName * param headers */private void createSheet(String sheetName,String headers) sheet = (SXSSFSheet) workbook.createSheet(sheetName);row = (SXSSFRow) sheet.createRow(currentRow);for (int i = 0; i headers.length; i+) cell = (SXSSFCell) row.createCell(i);cell.set
14、CellType(XSSFCell.CELL_TYPE_STRING);cell.setCellValue(headersi0);cell.setCellStyle(head_Style);currentRow+;/* * 导出excel * param listRows * throws ParseException */public synchronized void PoiWriteExcel_To2007(List listRows,OutputStream out) throws ParseException for (int i = 0; i listRows.size(); i+) row = (SXSSFRow) sheet.createRow(currentRow);ArrayList ListCells = (ArrayList)listRows.get(i);for (int j = 0; j ListCells.size(); j+) Object obj = ListCells.get(j);cell = (SXSSFCell) row.createCell(j);if(obj inst