zoukankan      html  css  js  c++  java
  • Excel——读取——导出目录

    /**
         * 导出Excel文件到具体的目录
         * <一句话功能简述>
         * <功能详细描述>
         * @param fileName 导出的文件名
         * @param sheetName 导出的签页名称
         * @param headMap 列表名映射Map(英文与中文的映射,key为英文,value为中文)
         * @param dataList 需要导出的数据集
         * @return
         * @see [类、类#方法、类#成员]
         * @author
         * @throws IOException 
         */
        public static String exportExcel(String fileName,String sheetName,LinkedHashMap<String, String> headMap,List<GenericValue> dataList) throws IOException {
            XSSFWorkbook workbook = null;
            FileOutputStream outputStream = null;
            
            workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet(sheetName);
            
            int startRowLine=3;    //导出的数据从第三行开始填写,前面空两行
            int startCellLine=2;
            //生成表头相关的首行
            int startHeadCellLine=startCellLine;
            XSSFRow headRow = sheet.createRow(startRowLine);
            for(String key:headMap.keySet()) {
                headRow.createCell(startHeadCellLine).setCellValue(headMap.get(key));
                startHeadCellLine++;
            }
            startRowLine++;
            
            //将数据行填充到Excel中
            for(GenericValue gv:dataList) {
                int startDataCellLine=startCellLine;
                XSSFRow dataRow = sheet.createRow(startRowLine);
                for(String key:headMap.keySet()) {
                    for(String gvk:gv.keySet()) {
                        if(key.equals(gvk)) {
                            dataRow.createCell(startDataCellLine).setCellValue(gv.getString(key));
                            startDataCellLine++;
                        }
                    }
                }
                startRowLine++;
            }
            
            //保存文件到本地
            String filePath = ConstantFields.SFTP_PATH + fileName+"-"+System.currentTimeMillis()+".xlsx";
            File file = new File(filePath);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdirs();
            }
            
            outputStream = new FileOutputStream(new File(filePath));
            workbook.write(outputStream);
            workbook.close();
            outputStream.flush();
            outputStream.close();
            return filePath;
        }    
  • 相关阅读:
    Convert Sorted Array to Binary Search Tree数组变成高度平衡的二叉树
    695. Max Area of Island最大岛屿面积
    257. Binary Tree Paths返回所有深度优先的遍历
    112. Path Sum二叉树路径和
    对称二叉树 · symmetric binary tree
    100. Same Tree同样的树
    计数排序 (Counting Sort)
    基数排序 (Radix Sort)
    Linux 文件基本属性
    gensuitemodule (Mac OS) – Python 中文开发手册
  • 原文地址:https://www.cnblogs.com/gzhcsu/p/11126128.html
Copyright © 2011-2022 走看看