JAVA中将数据保存到EXCEL文件
java后端将数据库中数据或前端传来的数据保存到EXCEL文件中。代码中有详细注解。
- 依赖包
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.0</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.0</version></dependency>
- 代码
/*** 将数据保存到EXCEL文件** @param filePath EXCEL文件存放路径* @param fileName 文件名称* @param data 数据* data: {* name: '标准名称',* objName: '适用对象名称',* version: '标准版本',* remark: '备注',* fields: [* {* name: '数据项名称',* rules: [* {* name: '规则名称',* type: '规则类型',* params: '规则参数'* }* ]* }* ]* }*/public void writeExcelTable(String filePath, String fileName, ObjectNode data) throws Exception {try {String fullPath = filePath + fileName + ".xlsx";File pathDirectory = new File(filePath);if (!pathDirectory.exists()) {pathDirectory.mkdirs();}// 第一步:创建一个webbook文件,对应一个excel文件HSSFWorkbook wb = new HSSFWorkbook();// 第二步:在excel中添加一个sheet工作表,参数为该工作表名字,不写为默认;HSSFSheet sheet = wb.createSheet("数据标准导出");sheet.setAutobreaks(true);// 第三步,创建样式// 字体HSSFFont bigFontBold = createFont(wb, "等线", true, (short) 14);HSSFFont bigFont = createFont(wb, "等线", false, (short) 14);HSSFFont fontBold = createFont(wb, "等线", true, (short) 11);HSSFFont font = createFont(wb, "等线", false, (short) 11);// 单元格样式HSSFCellStyle bigCellStyleBold = createCellStyle(wb, bigFontBold, HSSFColor.HSSFColorPredefined.LIGHT_GREEN.getIndex(), HorizontalAlignment.CENTER);HSSFCellStyle bigCellStyle = createCellStyle(wb, bigFont, (short) 0, HorizontalAlignment.CENTER);HSSFCellStyle cellStyleBold = createCellStyle(wb, fontBold, HSSFColor.HSSFColorPredefined.GREY_25_PERCENT.getIndex(), HorizontalAlignment.CENTER);HSSFCellStyle cellStyle = createCellStyle(wb, font, (short) 0, HorizontalAlignment.CENTER);HSSFCellStyle cellStyleTitle = createCellStyle(wb, bigFontBold, HSSFColor.HSSFColorPredefined.SEA_GREEN.getIndex(), HorizontalAlignment.CENTER);// 合并部分特殊单元格for (int i = 1; i < 6; i++) {sheet.addMergedRegion(new CellRangeAddress(i, i, 0, 1));sheet.addMergedRegion(new CellRangeAddress(i, i, 2, 3));}// 合并表头部分单元格sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));sheet.addMergedRegion(new CellRangeAddress(6, 6, 0, 3));// 第三步:往sheet中添加表头及内容// 数据标准概要信息表头及内容HSSFRow rowSpecificationTitle = sheet.createRow(0);createCell(rowSpecificationTitle, 0, cellStyleTitle, "标准概要内容");HSSFRow rowSpecificationName = sheet.createRow(1);createCell(rowSpecificationName, 0, bigCellStyleBold, "标准名称");createCell(rowSpecificationName, 2, bigCellStyle, data.name);HSSFRow rowSubjectName = sheet.createRow(2);createCell(rowSubjectName, 0, bigCellStyleBold, "对象名称");createCell(rowSubjectName, 2, bigCellStyle, data.objName);HSSFRow rowVersion = sheet.createRow(3);createCell(rowVersion, 0, bigCellStyleBold, "版本");createCell(rowVersion, 2, bigCellStyle, data.version);HSSFRow rowRemark = sheet.createRow(4);createCell(rowRemark, 0, bigCellStyleBold, "备注");createCell(rowRemark, 2, bigCellStyle, data.remark);// 详细规则表头HSSFRow rowRuleTitle = sheet.createRow(5);createCell(rowRuleTitle, 0, cellStyleTitle, "标准详细内容");HSSFRow rowRuleColumnName = sheet.createRow(6);createCell(rowRuleColumnName, 0, cellStyleBold, "数据项名称");createCell(rowRuleColumnName, 1, cellStyleBold, "规则类型");createCell(rowRuleColumnName, 2, cellStyleBold, "规则名称");createCell(rowRuleColumnName, 3, cellStyleBold, "规则参数");// 获取标准规则并填充至表格int ruleRowBeginIndex = 7;for (JsonNode fieldNode : data.fields) {for (JsonNode rule : fieldNode.rules) {HSSFRow row = sheet.createRow(ruleRowBeginIndex);createCell(row, 0, cellStyle, fieldNode.name);createCell(row, 1, cellStyle, rule.type);createCell(row, 2, cellStyle, rule.name);createCell(row, 3, cellStyle, rule.params);ruleRowBeginIndex++;}}// 对合并单元格的边框进行特殊处理,前面的cellStyle只影响了合并单元格后的第一个单元格,其他单元格没有边框for (int i = 1; i < 6; i++) {setBorder(BorderStyle.THIN, HSSFColor.HSSFColorPredefined.BLACK.getIndex(), new CellRangeAddress(i, i, 0, 1), sheet);setBorder(BorderStyle.THIN, HSSFColor.HSSFColorPredefined.BLACK.getIndex(), new CellRangeAddress(i, i, 2, 3), sheet);}setBorder(BorderStyle.THIN, HSSFColor.HSSFColorPredefined.BLACK.getIndex(), new CellRangeAddress(0, 0, 0, 3), sheet);setBorder(BorderStyle.THIN, HSSFColor.HSSFColorPredefined.BLACK.getIndex(), new CellRangeAddress(6, 6, 0, 3), sheet);// 列宽自适应,支持中文字符setSizeColumn(sheet, 4);// 第四步:将文件保存到指定位置FileOutputStream out = new FileOutputStream(fullPath);wb.write(out);out.close();wb.close();} catch (Exception e) {throw e;}}
JAVA中将数据保存到EXCEL文件相关推荐
- Scrapy中将数据保存到Excel和MySQL中
目录标题 1. Excel 1.1 openpyxl 1.1.1 代码说明 1.1.2 注意 1.2 pandas 1.2.1 代码说明 1.2.2 常见错误 1.3 openpyxl和pandas对 ...
- 将爬取的数据保存到Excel表格
第一步.导入模块 import xlwt # 导入写入excel需要的包 第二步.定义函数,将爬取好的数据保存到excel文件中,下面以保存python的关键词为例,介绍详细流程. def write ...
- 利用xpath爬取链家租房房源数据并利用pandas保存到Excel文件中
我们的需求是利用xpath爬取链家租房房源数据,并将数据通过pandas保存到Excel文件当中 下面我们看一下链家官网的房源信息(以北京为例) 如图所示,我们通过筛选得到北京租房信息 那么我们需要将 ...
- asp.net学习笔记·将数据库中的数据保存在EXCEL文件中
提取数据库中的数据,将其保存在EXCEL文件中,并提供下载. 在一般处理程序中将数据库数据保存在EXCEL文件中的代码 using System; using System.Collections.G ...
- 豆瓣电影Top250信息爬取并保存到excel文件中
豆瓣电影Top250下载并保存到excel文件中 效果图 前言 确定目标网页url 爬取过程 导入相关库 页面内容的获取 页面解析 数据提取 主函数的编写 函数调用 数据存储 完整代码 结语 效果图 ...
- python爬虫爬取豆瓣电影排行榜并通过pandas保存到Excel文件当中
我们的需求是利用python爬虫爬取豆瓣电影排行榜数据,并将数据通过pandas保存到Excel文件当中(步骤详细) 我们用到的第三方库如下所示: import requests import pan ...
- 如何将网页内容保存到计算机中,如何将网站导出excel表格数据-如何把网页数据保存到EXCEL...
网页上的表格数据怎么复制到excel 1.打开excel表格. 2.打开菜单"数据->"导入外部数据"->"新建 Web 查询",在&qu ...
- 将DataTable中的数据保存到Excel
如何快速将DataTable中的数据保存到Excel 遇到的问题 ① 保存到Excel的时间比较长,用户体验差 ② 保存失败(原因:Excel程序打开:或前一次调用Excel线程没有关闭,等) 开发环 ...
- Python--爬虫爬取的数据保存到excel
Python–爬虫爬取的数据保存到excel 文章目录 Python--爬虫爬取的数据保存到excel 一.excel表格存储 二.python3.9没有自带的xlwt模块和xlrd,需要自行下载 三 ...
最新文章
- Ubuntu nfs配置
- create maven android project
- psycopg2 mysql_使用psycopg2操作PostgreSQL数据库之二
- csdn在markdown笔记中复制代码格式混乱的解决办法
- 李开复:AlphaGo 若打败了世界冠军,意味着什么?
- 【11.5校内测试】【倒计时5天】【DP】【二分+贪心check】【推式子化简+线段树】...
- TQ210——按键(中断查询法)
- [2013.9.10]vb.net坑爹的数组
- 【NOIP2016提高A组模拟10.15】算循环
- [转载] python的__del__()方法
- 阿里云ECS实例邮件发送不了的解决办法
- 大数据专业python实验报告_大数据导论实验报告
- 键盘怎么按出计算机,怎么在电脑键盘上打出艾特@键? 原来是这样的
- php 合成图片、合成圆形图片
- win10系统的计算机C盘在哪,win10系统电脑C盘programdata在哪的图文办法
- Android 开发摆脱数据线 - Android studio 无线调试App
- Linux-2.6 所有版本内核源码下载
- Facebook第三方登录切换账号的问题
- 衰老研究的大问题:百岁老人的长寿秘密是什么?
- Java-PTA 奇偶分家