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

POI的简单入门

POI的简单入门

  • 导入jar包
  • 将数据写入Excel文件
  • 读取Excel文件中的数据

导入jar包

Apache POI的maven坐标

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version>
</dependency>

将数据写入Excel文件

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class Wan {public static void write() throws Exception{//在内存中创建一个Excel文件对象XSSFWorkbook excel = new XSSFWorkbook();//创建Sheet页XSSFSheet sheet = excel.createSheet("daye");//在Sheet页中创建行,0表示第1行XSSFRow row1 = sheet.createRow(0);//创建单元格并在单元格中设置值,单元格编号也是从0开始,1表示第2个单元格row1.createCell(1).setCellValue("姓名");row1.createCell(2).setCellValue("城市");XSSFRow row2 = sheet.createRow(1);row2.createCell(1).setCellValue("张三");row2.createCell(2).setCellValue("北京");XSSFRow row3 = sheet.createRow(2);row3.createCell(1).setCellValue("李四");row3.createCell(2).setCellValue("上海");FileOutputStream out = new FileOutputStream(new File("D:\\daye.xlsx"));//通过输出流将内存中的Excel文件写入到磁盘上excel.write(out);//关闭资源out.flush();out.close();excel.close();}public static void main(String[] args) throws Exception {write();}
}

读取Excel文件中的数据

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;public class Wan {public static void read() throws Exception{FileInputStream in = new FileInputStream(new File("D:\\daye.xlsx"));//通过输入流读取指定的Excel文件XSSFWorkbook excel = new XSSFWorkbook(in);//获取Excel文件的第1个Sheet页XSSFSheet sheet = excel.getSheetAt(0);//获取Sheet页中的最后一行的行号int lastRowNum = sheet.getLastRowNum();for (int i = 0; i <= lastRowNum; i++) {//获取Sheet页中的行XSSFRow titleRow = sheet.getRow(i);//获取行的第2个单元格XSSFCell cell1 = titleRow.getCell(1);//获取单元格中的文本内容String cellValue1 = cell1.getStringCellValue();//获取行的第3个单元格XSSFCell cell2 = titleRow.getCell(2);//获取单元格中的文本内容String cellValue2 = cell2.getStringCellValue();System.out.println(cellValue1 + " " +cellValue2);}//关闭资源in.close();excel.close();}public static void main(String[] args) throws Exception {read();}
}
http://www.lryc.cn/news/101299.html

相关文章:

  • 如何将笔记本作为另一台电脑的副屏显示
  • 深入理解正则表达式:为什么它在Java中如此重要?
  • jmeter实现webservice接口测试
  • js 四舍五入保留一位小数 求百分比
  • 文件上传漏洞总结2
  • 【组内工作】木马回联
  • 未来将会有更多基于 Cortana 的设备
  • 嵌入式硬件系统的基本组成
  • def __init__(self, **kwargs):中的**kwargs是什么意思
  • web攻击面试|网络渗透面试(三)
  • 数据分析方法
  • Spring全家桶---白虎篇
  • 深度优先搜索|79, 695,212
  • 论文阅读与管理方法论
  • 基于OAI与Ueransim的5G网络切片平台构成简述
  • 论文笔记:Adjusting for Autocorrelated Errors in Neural Networks for Time Series
  • DataEase开源BI工具安装_数据全量_增量同步_大屏拖拽自动生成_多数据源支持_数据血缘分析---大数据工作笔记0183
  • 如何提升程序员的软素质
  • msvcp100.dll丢失怎么修复,这三个常用的修复方法可以解决
  • python实现递推算法解决分鱼问题
  • 【LeetCode】142.环形链表Ⅱ
  • 16.Netty源码之ChannelPipeline
  • “使用Spring Boot构建微服务应用的最佳实践“
  • redis高可用之主从复制,哨兵,集群
  • 【Ajax】笔记-原生jsonp跨域请求案例
  • QT--day2(信号与槽,多界面跳转)
  • 热备份路由协议原理
  • 模拟实现定时器
  • TCP/IP的分包粘包
  • 盘点:查快递教程