济南网站建设找凌峰,阜城网站建设代理,邢台专业做网站的地方,网站制作国际连锁1.Java后台生成指定路径下创建指定名称的CSV文件
/*** 生成csv文件* param filePath 文件路径名称* param fileName 文件名称* param colNameList 标题数据信息* param dataList CSV的文件数据* return filePathfileName* throws*/public static File genera…1.Java后台生成指定路径下创建指定名称的CSV文件
/*** 生成csv文件* param filePath 文件路径名称* param fileName 文件名称* param colNameList 标题数据信息* param dataList CSV的文件数据* return filePathfileName* throws*/public static File generateCsv(String filePath, String fileName,ListString colNameList, ListListString dataList) throws IOException {BufferedWriter csvWrite null;String fileRealPath filePath / fileName .csv;try {//定义文件类型File csvFile new File(fileRealPath);//获取文件目录if (!csvFile.exists()){File parentFile csvFile.getParentFile();if (!parentFile.exists()){if (parentFile.mkdirs()){log.info(目录创建成功parentFile.getAbsolutePath());}else{log.info(目录创建失败:parentFile.getAbsolutePath());}}}//创建文件if (csvFile.createNewFile()){log.info(文件创建成功:csvFile.getAbsolutePath());}else{log.info(文件创建失败csvFile.getAbsolutePath());}//先写入UTF-8-BOM编码头内容防止用Excel文件打开CSV文件出现标题乱码情况byte[] utf8bom{(byte)0xef,(byte)0xbb,(byte)0xbf};FileOutputStream fileOutputStream new FileOutputStream(csvFile);fileOutputStream.write(utf8bom);csvWrite new BufferedWriter(new OutputStreamWriter(fileOutputStream, UTF-8), 1024);//写入表头write(colNameList, csvWrite);//写入数据for (ListString dataPerRow : dataList) {write(dataPerRow, csvWrite);}csvWrite.flush();return csvFile;}catch (IOException e) {log.error(csv文件生成失败原因, e);throw new IOException(csv文件生成失败);}finally {try {if (null ! csvWrite) {csvWrite.close();}}catch (IOException e) {log.error(关闭文件流失败原因, e);throw new IOException(关闭文件流失败);}}}/***将数据按行写入数据*param dataList 每一行的数据集合*param csvWreite *throws IOException*/private static void write(ListString dataList, BufferedWriter csvWrite) throws IOException {for (String data : dataList) {StringBuffer buffer new StringBuffer();String rowStr buffer.append(\).append(data).append(\,).toString();csvWrite.write(rowStr);}csvWrite.newLine();}2.Java后台生成指定路径下创建指定名称的xlsx文件 /*** 导出excel文件* param filePath 文件路径* param fileName 文件名称* param colNameList 标题名称* param dataList 每一页sheet数据列表* return*/public static File generateExcel(String filePath, String fileName,ListString colNameList, ListMapString,Object dataList) throws IOException {String fileRealPath filePath / fileName .xlsx;File excelFile new File(fileRealPath);//获取文件目录if (!excelFile.exists()){File parentFile excelFile.getParentFile();if (!parentFile.exists()){if (parentFile.mkdirs()){log.info(目录创建成功parentFile.getAbsolutePath());}else{log.info(目录创建失败:parentFile.getAbsolutePath());}}}//创建文件if (excelFile.createNewFile()){log.info(文件创建成功:excelFile.getAbsolutePath());}else{log.info(文件创建失败excelFile.getAbsolutePath());}Workbook workbook new XSSFWorkbook(); // 创建Workbookfor (MapString, Object map : dataList) {//sheet的名称String sheetName MapUtils.getString(map, sheetName);//当前sheet的数据集合ListListString tempDataList (ListListString)MapUtils.getObject(map, dataList);Sheet sheet workbook.createSheet(sheetName); // 创建Sheet// 创建表头Row headerRow sheet.createRow(0);for (int i 0; i colNameList.size(); i) {headerRow.createCell(i).setCellValue(colNameList.get(i));}if (tempDataList ! null tempDataList.size() 0){// 写入数据for (int i 0; i tempDataList.size(); i) {ListString lineDataList tempDataList.get(i);Row row sheet.createRow(i 1); // 从第二行开始写数据for (int j 0; j lineDataList.size(); j) {row.createCell(j).setCellValue(lineDataList.get(j));}}}}// 写入文件try (FileOutputStream fileOut new FileOutputStream(excelFile)) {workbook.write(fileOut);} catch (IOException e) {log.error(写入文件失败e,e.getMessage());} finally {try {workbook.close(); // 关闭Workbook释放资源} catch (IOException e) {log.error( 关闭Workbook失败e,e.getMessage());}}return excelFile;}