加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java开发之导出excel工具类

发布时间:2020-12-15 00:13:32 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.Map; impor

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    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 {  
            // 创建Excel文档  
            HSSFWorkbook hwb = new HSSFWorkbook();  
            // sheet 对应一个工作页  
            HSSFSheet sheet = hwb.createSheet("exportReport");  
            int colNum = titles.length;  
            // 创建第一行  
            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) + "年_年度报表";  
            } else if (colNum == 5) {  
                fileName += list.get(0) + "_" + list.get(1) + "年" + list.get(2)  
                        + "月_月度报表";  
            }  
            // 创建文件输出流,准备输出电子表格  
            OutputStream out = new FileOutputStream("../webapps/UsedMallMinaServer/files/"  
                    + fileName + ".xls");  
            hwb.write(out);  
            out.close();  
        }  
    }  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读