| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- 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<String, Object> 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<String, Object> dbrow) {
- short k = 0;
- cell = row.createCell(0);
- cell.setCellValue(rowcount + 1);
- Set<String> 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();
- }
- }
|