package cn.com.goldenwater.dcproj.utils.export; import org.apache.commons.collections.MapUtils; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; import java.io.OutputStream; import java.util.Map; import java.util.Set; public class ExcelExport extends ExportAbstract { private HSSFWorkbook wb = null; HSSFSheet downloadSheet; HSSFCellStyle headStyle; HSSFCellStyle dataStyle; HSSFCellStyle rowStyle; HSSFDataFormat dataFormat; HSSFFont headFont; HSSFFont dataFont; HSSFFont rowFont; HSSFRow row = null; HSSFCell cell = null; int startsheet = -1; int maxline = 50000; //一个Sheet最大数据行数 // private ZipOutputStream zipOut = null; private void cE() { wb = null; wb = new HSSFWorkbook(); downloadSheet = wb.createSheet(); headStyle = wb.createCellStyle(); dataStyle = wb.createCellStyle(); rowStyle = wb.createCellStyle(); dataFormat = wb.createDataFormat(); headFont = wb.createFont(); dataFont = wb.createFont(); rowFont = wb.createFont(); // headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headFont.setBold(true); dataFont.setFontHeightInPoints((short) 10); dataFont.setColor(IndexedColors.LIGHT_BLUE.index); // dataFont.setBoldweight((short) 500); dataFont.setBold(true); rowFont.setColor(IndexedColors.DARK_RED.index); rowFont.setFontHeightInPoints((short) 12); headStyle.setFont(headFont); headStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); headStyle.setAlignment(HorizontalAlignment.CENTER); headStyle.setLocked(true); dataStyle.setFont(dataFont); dataStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); dataStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); dataStyle.setBorderBottom(BorderStyle.THIN); dataStyle.setBorderTop(BorderStyle.THIN); dataStyle.setBorderLeft(BorderStyle.THIN); dataStyle.setBorderRight(BorderStyle.THIN); dataStyle.setWrapText(true); dataStyle.setLocked(true); rowStyle.setFont(rowFont); rowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); rowStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index); rowStyle.setBorderLeft(BorderStyle.THICK); rowStyle.setBorderBottom(BorderStyle.THIN); } public ExcelExport() { wb = new HSSFWorkbook(); downloadSheet = wb.createSheet(); headStyle = wb.createCellStyle(); dataStyle = wb.createCellStyle(); rowStyle = wb.createCellStyle(); dataFormat = wb.createDataFormat(); headFont = wb.createFont(); dataFont = wb.createFont(); rowFont = wb.createFont(); // headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headFont.setBold(true); dataFont.setFontHeightInPoints((short) 10); dataFont.setColor(IndexedColors.LIGHT_BLUE.index); // dataFont.setBoldweight((short) 500); dataFont.setBold(true); rowFont.setColor(IndexedColors.DARK_RED.index); rowFont.setFontHeightInPoints((short) 12); headStyle.setFont(headFont); headStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); //headStyle.setBorderBottom(headStyle.BORDER_THIN); //headStyle.setBorderTop(headStyle.BORDER_THICK); //headStyle.setBorderLeft(headStyle.BORDER_THIN); //headStyle.setBorderRight(headStyle.BORDER_THIN); // headStyle.setTopBorderColor(HSSFColor.DARK_YELLOW.index); // headStyle.setBottomBorderColor(HSSFColor.DARK_BLUE.index); headStyle.setAlignment(HorizontalAlignment.CENTER); headStyle.setLocked(true); dataStyle.setFont(dataFont); dataStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); dataStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); dataStyle.setBorderBottom(BorderStyle.THIN); dataStyle.setBorderTop(BorderStyle.THIN); dataStyle.setBorderLeft(BorderStyle.THIN); dataStyle.setBorderRight(BorderStyle.THIN); dataStyle.setWrapText(true); dataStyle.setLocked(true); rowStyle.setFont(rowFont); rowStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); rowStyle.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.index); rowStyle.setBorderLeft(BorderStyle.THICK); rowStyle.setBorderBottom(BorderStyle.THIN); } @Override public void ExportHead(OutputStream _out, java.util.Collection co) throws Exception { // Auto-generated method stub out = _out; wb.setSheetName(startsheet + 1, this.fileName + "_" + (startsheet + 2)); row = downloadSheet.createRow(0); row.setHeight((short) 0x160); //冻结第一行 第一列 downloadSheet.createFreezePane(1, 1, 1, 1); toHeadRow(); } public void toHeadRow() { short k = 0; cell = row.createCell((0)); cell.setCellValue("序号"); cell.setCellType(CellType.STRING); cell.setCellStyle(headStyle); downloadSheet.setColumnWidth(0, 2400); for (int i = 0; i < rSearchfields.size(); i++) { String obj = (String) rSearchfields.get(i); //这里需要进行可读性质转换 if (!index_h.containsKey(obj)) { k++; continue; } String head = ""; if (!bdata) { head = obj; } else { head = loadHeadValue(obj); } cell = row.createCell((i - k + 1)); cell.setCellValue(head); cell.setCellType(CellType.STRING); int columnWidth = (int) ((35.7 + 3) * ((Column) index_h.get(obj)).getGridwidth()); downloadSheet.setColumnWidth((i - k + 1), columnWidth >= 65280 ? 65280 : columnWidth); cell.setCellStyle(headStyle); // downloadSheet.setColumnWidth(cj, (short) (35.7 * col.getWidth())); } } @Override public void ContinueExport(java.util.Collection co) throws Exception { java.util.Iterator it = co.iterator(); System.out.println("export:" + rowcount); while (it.hasNext()) { Map dbrow = (Map) it.next(); if (rowcount % maxline == 0) { if (startsheet != -1) { downloadSheet = wb.createSheet(); wb.setSheetName(startsheet + 1, this.fileName + "_" + (startsheet + 2));//startsheet+1 row = downloadSheet.createRow(0); row.setHeight((short) 0x160); //冻结第一行 downloadSheet.createFreezePane(1, 1, 1, 1); toHeadRow(); } startsheet++; } row = downloadSheet.createRow((rowcount % maxline + 1)); toTextRow(dbrow); rowcount++; } } public void toTextRow(Map dbrow) { short k = 0; cell = row.createCell(0); cell.setCellValue(rowcount + 1); Set keySet = dbrow.keySet(); int idx = 0; for (String key : keySet) { //这里需要进行可读性质转换 if (!index_h.containsKey(key)) { idx++; k++; continue; } String value = MapUtils.getString(dbrow, key); String _v; if (!bdata) { _v = (value == null) ? "" : value; } else { _v = loadDicValue(value, key); } cell = row.createCell((idx - k + 1)); String type = null;//outField.getType(); if ("NUMBER".equals(type)) { try { cell.setCellValue(Double.parseDouble(_v)); } catch (Exception e) { cell.setCellValue(_v); } } else { cell.setCellValue(_v); } idx++; } } public void toTextRow(DBRow dbrow) { short k = 0; cell = row.createCell(0); cell.setCellValue(rowcount + 1); for (int i = 0; i < dbrow.size(); i++) { Object obj = dbrow.get(i); DataField outField = (DataField) obj; //这里需要进行可读性质转换 if ("ROWNUM_".equals(outField.getFieldID()) || !index_h.containsKey(outField.getFieldID())) { k++; continue; } String value = (String) outField.getValue(); String _v; if (!bdata) { _v = (value == null) ? "" : value; } else { _v = loadDicValue(value, outField.getFieldID()); } cell = row.createCell((i - k + 1)); String type = outField.getType(); if ("NUMBER".equals(type)) { try { cell.setCellValue(Double.parseDouble(_v)); } catch (Exception e) { cell.setCellValue(_v); } } else { cell.setCellValue(_v); } } } @Override public void EndExport() throws Exception { if (index_h != null) { index_h.clear(); } wb.write(out); out.flush(); out.close(); // out.finish(); // out.flush(); } }