当前位置: 首页 > news >正文

Java后台生成指定路径下创建指定名称的文件

1.Java后台生成指定路径下创建指定名称的CSV文件

/*** <生成csv文件>* @param filePath    文件路径名称* @param fileName    文件名称* @param colNameList 标题数据信息* @param dataList    CSV的文件数据* @return filePath+fileName* @throws*/public static File generateCsv(String filePath, String fileName,List<String> colNameList, List<List<String>> 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 (List<String> 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(List<String> 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,List<String> colNameList, List<Map<String,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 (Map<String, Object> map : dataList) {//sheet的名称String sheetName = MapUtils.getString(map, "sheetName");//当前sheet的数据集合List<List<String>> tempDataList = (List<List<String>>)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++) {List<String> 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;}
http://www.lryc.cn/news/478619.html

相关文章:

  • sqlcoder70b模型,如果需要训练或者微调需要什么样的GPU机器
  • 【Python实战案例】爬虫项目实例(附赠源码)
  • PDF多功能工具箱 PDF Shaper v14.6
  • Jupyter Notebook添加kernel的解决方案
  • Linux 无名管道
  • Java项目实战II基于Spring Boot的药店管理系统的设计与实现(开发文档+数据库+源码)
  • 前端拖拽库方案之react-beautiful-dnd
  • 【题解】CF2033G
  • 【error】 react 控制台报错Invalid hook call
  • SDL基本使用
  • 大模型的temperature参数
  • 软件项目功能复用指南,复用方案,评估方案(word原件)
  • leetcode 3255 长度为 K 的子数组的能量值 II 中等
  • CCS下载安装(以12.3.0版本为例)
  • C++STL容器详解——list
  • linux tar 打包为多个文件
  • json字符串与python字典的区别与联系
  • 数据结构-链表【chapter1】【c语言版】
  • OJ05:989. 数组形式的整数加法
  • 山东布谷科技:关于直播源码|语音源码|一对一直播源码提交App Store的流程及重构建议
  • docker搭建guacamole,web远程桌面
  • .baxia勒索病毒来袭:数据恢复与防护措施详解
  • [UUCTF 2022 新生赛]ezpop 详细题解(字符串逃逸)
  • 【Zynq UltraScale+ RFSoC】DFE
  • Ubuntu学习笔记 - Day3
  • scala list系列
  • TLS协议基本原理与Wireshark分析_wireshark分析tls协议
  • 【359】基于springboot的智慧草莓基地管理系统
  • 【智能算法应用】遗传算法求解车间布局优化问题
  • java 中List 的使用