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;
    }    
原文地址:https://www.cnblogs.com/gzhcsu/p/11126128.html