POIUtils 导出

PoiExportUtils:

package com.icil.esolution.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 
 * @ClassName: PoiExportUtils
 * @Description: use export excel , some common code
 * @Author: Sea
 * @Date: 15 Oct 2018 2:26:38 PM
 * @Copyright: 2018 ICIL All rights reserved.
 */
public class PoiExportUtils {

    private static String STANDARD_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public Workbook workbook = new XSSFWorkbook();
    DataFormat format = null;

    {
        format = workbook.createDataFormat();
    }

    public Sheet createXSheet(String sheetName) {
        
        // 在Excel工作簿中建一工作表,其名为缺省值, 也可以指定Sheet名称
        Sheet sheet = null;
        if (StringUtils.isNotBlank(sheetName)) {
            sheet = workbook.createSheet(sheetName);
        } else {
            workbook.createSheet();
        }
        //Freeze the title row
                /**
                 *  cellNum:表示要冻结的列数;
                    rowNum:表示要冻结的行数;
                    firstCellNum:表示被固定列右边第一列的列号;
                    firstRollNum :表示被固定行下边第一列的行号;
                 */
        sheet.createFreezePane( 0, 1, 0, 1 ); 
                
        return sheet;

    }

    public CellStyle getTitleCellStyle() {
        // 用于格式化单元格的数据
//        DataFormat format = workbook.createDataFormat();
        // 设置字体
        Font font = workbook.createFont();
//      font.setFontHeightInPoints((short) 20); // 字体高度
//      font.setColor(Font.COLOR_RED); // 字体颜色
        font.setFontName("黑体"); // 字体
        font.setBold(true); // 加粗
//      font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 宽度
      font.setItalic(true); // 是否使用斜体
      font.setStrikeout(true); //是否使用划线
        // 设置单元格类型
        CellStyle titleCellStyle = workbook.createCellStyle();
        titleCellStyle.setBorderBottom(BorderStyle.THIN); // 下边框
        titleCellStyle.setBorderLeft(BorderStyle.THIN);// 左边框
        titleCellStyle.setBorderTop(BorderStyle.THIN);// 上边框
        titleCellStyle.setBorderRight(BorderStyle.THIN);// 右边框
//      titleCellStyle.setFillForegroundColor(HSSFColor.GREEN.index);    //
//      titleCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); //填充形式
        titleCellStyle.setFont(font);
        titleCellStyle.setAlignment(HorizontalAlignment.CENTER); // 水平布局:居中
        titleCellStyle.setWrapText(true);

        return titleCellStyle;
    }

    public CellStyle getDateCellStyle() {
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setDataFormat(format.getFormat(STANDARD_TIME_FORMAT));
        return cellStyle1;
    }
    /**
     * @ such as  0.000  |  yyyy-MM-dd hh:mm:ss
     * @param formats
     * @return
     */
    public CellStyle getDataCellStyle(String formats) {
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setDataFormat(format.getFormat(formats));
        return cellStyle1;
    }

}
View Code

test :

    public Workbook exportSoOrderList(ArrayList<SOOrderVO> sOOrderVOList) {
        logger.info("enter SoOrderServiceImpl class -->exportSoOrderList() method");
       //1.create a sheet 
        PoiExportUtils poiExportUtils = new PoiExportUtils();
        Sheet sheet = poiExportUtils.createXSheet("Store-out Orders");
        
        // 2. set title  //"seqId","partNo","partDesc","qtyInv","storeInDtLoc"
//        String[] title= {"E-Commerce Order No","Courier","Courier Bill No","Consignee Name","Consignee Tel","Created Date"};
        
        String[] title= {" 電商訂單號 ","快遞公司","快遞單號","收貨人姓名","收貨人電話","創建日期 "};
      //set  order by  
        sheet.setAutoFilter(CellRangeAddress.valueOf("A1:F1"));
        // set content    
        for (int contentColumn=0; contentColumn<=sOOrderVOList.size();contentColumn++) {
            
             Row contentRow = sheet.createRow(contentColumn);
            // set title
            if(contentColumn==0) {
                for (int titleColumn=0; titleColumn<title.length;titleColumn++) {
                    Cell titleCell = contentRow.createCell(titleColumn);
                    titleCell.setCellStyle(poiExportUtils.getTitleCellStyle());
                    titleCell.setCellValue(title[titleColumn]);
                }
                sheet.autoSizeColumn((short) contentColumn);
                logger.info("create excel title success");
                continue;
            }
            //set content body
            SOOrderVO sOOrder = sOOrderVOList.get(contentColumn-1);
            int i=0;
            sheet.autoSizeColumn((short) contentColumn); // 自动调整该列的宽度 
            contentRow.createCell(i++).setCellValue(sOOrder.getRefOrderNo());
            contentRow.createCell(i++).setCellValue(sOOrder.getShipping().getCarrierService());
            contentRow.createCell(i++).setCellValue(sOOrder.getShipping().getCourierBillNo());
            contentRow.createCell(i++).setCellValue(sOOrder.getShipping().getConsigneeContactName());
            contentRow.createCell(i++).setCellValue(sOOrder.getShipping().getConsigneePhoneNo());
            contentRow.createCell(i++).setCellValue(sOOrder.getCreatedDtLoc());
        }
        logger.info("end SoOrderServiceImpl class -->exportSoOrderList() method");
        return poiExportUtils.workbook;
    }
View Code

猜你喜欢

转载自www.cnblogs.com/lshan/p/10756437.html
今日推荐