行业知识
Apache POl
2026-12-04 04:15:00
来源:互联网转载
�� Apache POl是一个处理Miscrosoft Ofice各种文件格式的开源项目。简单来说就是,我们可以使用 POI 在 Java 程序中对Miscrosoft Office各种文件进行读写操作。一般情况下,POI都是用于操作 Excel 文件.
Apache POl 的应用场景:
- 银行网银系统导出交易明细
- 各种业务系统导出Excel报表
- 批量导入业务数据
使用POI需要导入下面2个坐标:
org.apache.poi poi org.apache.poi poi-ooxml
往表格存数据:
public class POITest { /* * 通过POI创建Excel文件并且写入文件内容 * */ @Test public void writeTest() throws IOException { //在内存中创建一个Excel文件 XSSFWorkbook excel = new XSSFWorkbook(); //在Excel文件中创建一个Sheet页 XSSFSheet sheet = excel.createSheet("info"); //在Sheet中创建行对象,rownum编号从0开始 XSSFRow row = sheet.createRow(1); //1代表第2行 row.createCell(1).setCellValue("姓名");//创建单元格写入内容 row.createCell(2).setCellValue("城市"); //创建一个新行 row = sheet.createRow(2);//第3行 row.createCell(1).setCellValue("张三");//创建单元格写入内容 row.createCell(2).setCellValue("厦门"); row = sheet.createRow(3);//第4行 row.createCell(1).setCellValue("李四");//创建单元格写入内容 row.createCell(2).setCellValue("南京"); //上面写的都是在内存,现在想在磁盘看到 FileOutputStream out = new FileOutputStream(new File("C://software/info.xlsx"));//设置文件 excel.write(out);//写入到文件 //关闭资源 out.close(); excel.close(); }} 读取excel中的数据:
@Testpublic void readTest() throws IOException{ FileInputStream in = new FileInputStream(new File("C://software/info.xlsx")); //读取磁盘上已经存在的Excel文件 XSSFWorkbook excel = new XSSFWorkbook(in); //读取Excel文件中的第一个Sheet页 XSSFSheet sheet = excel.getSheetAt(0); //获取Sheet中最后一行行号 int lastRowNum = sheet.getLastRowNum(); for(int i=1;i
下一篇:方舟手机联机怎么创服务器?