Apache POI 处理excel文件 记录用法

Apache POI

写excel

bash 复制代码
public static void write() throws IOException {
        //再内存中创建了一个Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();

        //创建一个sheet页
        XSSFSheet sheet = excel.createSheet("info");

        //这里创建行对象,这里的rownum 是从0开始的,类似于数组
        XSSFRow row = sheet.createRow(1);

        //创建单元格 这里的index也是从0开始的
        row.createCell(1).setCellValue("姓名"); //创建单元格,并且设置单元格内容
        row.createCell(3).setCellValue("城市");

        //新起一行
        row = sheet.createRow(2);
        row.createCell(1).setCellValue("jjking");
        row.createCell(3).setCellValue("广州");

        FileOutputStream fos = new FileOutputStream(new File("info.xlsx"));
        excel.write(fos);


        //关闭资源
        fos.close();
        excel.close();
    }

这个也不用过多的介绍,我们只要照着写就ok了,没什么难的

这样子,过后写好的excel,是这样的

读excel

bash 复制代码
public static void read() throws IOException, InvalidFormatException {
   XSSFWorkbook excel = new XSSFWorkbook(new File("info.xlsx"));

   //读取sheet页
   XSSFSheet sheet = excel.getSheetAt(0);

   //读取最后一个有文字的行号
   int lastRowNum = sheet.getLastRowNum();


   //从第2行开始读
   for(int i = 1; i <= lastRowNum; i++) {
       //获取行
       XSSFRow row = sheet.getRow(i);

       //获取单元格的对象
       String cellValue1 = row.getCell(1).getStringCellValue();
       String cellValue2 = row.getCell(3).getStringCellValue();

       System.out.println(cellValue1 + " " + cellValue2);
   }


   excel.close();
}
相关推荐
禁默41 分钟前
Linux Vim 编辑器详解:从入门到进阶(含图示+插件推荐)
linux·vim·excel
Tomorrow'sThinker19 小时前
[特殊字符] Python 批量生成词云:读取词频 Excel + 自定义背景 + Excel to.png 流程解析
python·excel
UrbanJazzerati1 天前
Excel 使用中的“坑”:拆分与合并列的陷阱及解决方案
excel
云游1 天前
利用外部Postgresql及zookeeper,启动Apache Dolphinscheduler3.1.9
分布式·postgresql·zookeeper·apache·工作流任务调度
KeThink1 天前
国民经济行业分类 GB/T 4754—2017 (PDF和exce版本)
pdf·excel
_oP_i1 天前
Excel 的多线程特性
excel
V1ncent Chen2 天前
Excel基础:数据查看
excel
谁他个天昏地暗2 天前
Java 实现 Excel 文件对比与数据填充
java·开发语言·excel
梦想blog2 天前
Spring Boot + Easy Excel 自定义复杂样式导入导出
excel
Aurora_NeAr2 天前
Apache Iceberg数据湖基础
apache