积分与排名
- 积分 - 4002
- 排名 - 195329
文章档案
最新评论
- 1. Re:项目管理与SSM框架
泰酷拉
- --Rayajunhahaha
- 2. Re:selenium实现拼图滑动验证模拟登录QQ空间
1
- --YxinHaaa
Apache POI是用Java编写的免费开源的跨平台的Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能,其中使用最多的就是使用POI操作Excel文件
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>${poi.version}</version> </dependency>
HSSF - 提供读写Microsoft Excel XLS格式档案的功能 .xls XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能 .xlsx HWPF - 提供读写Microsoft Word DOC格式档案的功能 HSLF - 提供读写Microsoft PowerPoint格式档案的功能 HDGF - 提供读Microsoft Visio格式档案的功能 HPBF - 提供读Microsoft Publisher格式档案的功能 HSMF - 提供读Microsoft Outlook格式档案的功能
@Test public void readexcel() throws IOException { // 创建工作表 XSSFWorkbook xssfWorkbook = new XSSFWorkbook("E:\\upload\\学生信息表.xlsx"); // 获取工作簿 XSSFSheet xssfSheet = xssfWorkbook.getSheet("成绩表"); // 获取总行号 int rowtotal = xssfSheet.getLastRowNum(); for(int i=0;i<rowtotal;i++){ XSSFRow row = xssfSheet.getRow(i+1); System.out.println("学号:"+row.getCell(0).getStringCellValue()); System.out.println("科目编号:"+row.getCell(1).getNumericCellValue()); System.out.println("成绩:"+row.getCell(2).getNumericCellValue()); System.out.println("考试时间:"+row.getCell(3).getStringCellValue()); } }
//将数据写入到外部的xlsx文件中 /** * 写入execl */ @Test public void writeexcel() throws IOException { // 创建工作表 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); // 创建一个工作簿 XSSFSheet sheet1 = xssfWorkbook.createSheet("offcn"); // 向该工作簿中写数据 XSSFRow row1 = sheet1.createRow(0); XSSFCell cell1= row1.createCell(0); cell1.setCellValue("学号"); XSSFCell cell2= row1.createCell(1); cell2.setCellValue("姓名"); XSSFCell cell3= row1.createCell(2); cell3.setCellValue("性别"); XSSFCell cell4= row1.createCell(3); cell4.setCellValue("年龄"); XSSFRow row2 = sheet1.createRow(1); XSSFCell r2_c1= row2.createCell(0); r2_c1.setCellValue("s234"); XSSFCell r2_c2= row2.createCell(1); r2_c2.setCellValue("张三"); XSSFCell r2_c3= row2.createCell(2); r2_c3.setCellValue("女"); XSSFCell r2_c4= row2.createCell(3); r2_c4.setCellValue(29); // excel可以提前不存在 FileOutputStream fileOutputStream = new FileOutputStream("E:\\upload\\offcn.xlsx"); xssfWorkbook.write(fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); xssfWorkbook.close(); }
__EOF__
作 者:YXH
出 处:https://www.cnblogs.com/YxinHaaa/p/17567352.html
关于博主:编程路上的小学生,热爱技术,喜欢专研。评论和私信会在第一时间回复。或者直接私信我。
版权声明:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
泰酷拉
1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」