中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Java開發(fā)之導(dǎo)出excel工具類

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

web開發(fā)中,一個(gè)系統(tǒng)的普通需求也包括導(dǎo)出excel,一般采用POI做統(tǒng)計(jì)報(bào)表導(dǎo)出excel。

導(dǎo)出excel工具類:

    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.OutputStream;  
    import java.util.List;  
    import java.util.Map;  
      
    import org.apache.poi.hssf.usermodel.HSSFCell;  
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
    import org.apache.poi.hssf.usermodel.HSSFRow;  
    import org.apache.poi.hssf.usermodel.HSSFSheet;  
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
      
    public class ExportExcel {  
      
        private ExportExcel() {  
            super();  
        }  
      
        public static void exportExcel(List<Object> list, Map<Integer, Long> map,  
                String[] titles) throws IOException {  
            // 創(chuàng)建Excel文檔  
            HSSFWorkbook hwb = new HSSFWorkbook();  
            // sheet 對應(yīng)一個(gè)工作頁  
            HSSFSheet sheet = hwb.createSheet("exportReport");  
            int colNum = titles.length;  
            // 創(chuàng)建第一行  
            HSSFRow firstrow = sheet.createRow(0);  
            HSSFCell[] firstcell = new HSSFCell[colNum];  
            for (int col = 0; col < colNum; col++) {  
                firstcell[col] = firstrow.createCell(col);  
                firstcell[col].setCellValue(new HSSFRichTextString(titles[col]));  
            }  
      
            // 插入記錄  
            int rowNum = map.size();  
            for (int i = 0; i < rowNum; i++) {  
                // 從第二行開始  
                HSSFRow row = sheet.createRow(i + 1);  
                // 插入list中的字段  
                for (int col = 0; col < colNum - 2; col++) {  
                    HSSFCell cell = row.createCell(col);  
                    cell.setCellValue(list.get(col).toString());  
                }  
                // 插入月份或日期  
                row.createCell(colNum - 2).setCellValue(i + 1);  
                // 插入總量  
                row.createCell(colNum - 1).setCellValue(map.get(i + 1));  
            }  
            String fileName = titles[1].substring(0, 2);  
            if (colNum == 4) {  
                fileName += list.get(0) + "_" + list.get(1) + "年_年度報(bào)表";  
            } else if (colNum == 5) {  
                fileName += list.get(0) + "_" + list.get(1) + "年" + list.get(2)  
                        + "月_月度報(bào)表";  
            }  
            // 創(chuàng)建文件輸出流,準(zhǔn)備輸出電子表格  
            OutputStream out = new FileOutputStream("../webapps/UsedMallMinaServer/files/"  
                    + fileName + ".xls");  
            hwb.write(out);  
            out.close();  
        }  
    }  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:JS簡單驗(yàn)證密碼強(qiáng)度

下一篇:判斷是否是合理的IP地址