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();
}
相关推荐
SeaTunnel4 小时前
Apache SeaTunnel 人物专访 | 张东浩:从使用者到Committer的开源历程
开源·apache
SeaTunnel7 小时前
2025年 Apache SeaTunnel 2月份社区月报速递
apache
带娃的IT创业者7 小时前
《Python实战进阶》No18: 使用 Apache Spark 进行分布式计算
python·spark·apache
Ιτ-ωoгκεг10 小时前
在 Java 中使用 Apache POI 为 Word 文档添加水印
java·word·apache·poi·水印
kylezhao20191 天前
C# Excel开源操作库MiniExcel使用教程
开发语言·c#·excel
初级代码游戏1 天前
VSTO(C#)Excel开发3:Range对象 处理列宽和行高
excel·vba·vsto
trabecula_hj1 天前
SpreadVue实现内置excel在线编辑并保存为后端可以接受的json格式
json·excel·spreadjs·spreadvue
西西弗Sisyphus1 天前
使用 Python pandas操作 Excel 文件
python·excel·pandas
初级代码游戏1 天前
VSTO(C#)Excel开发2:Excel对象模型和基本操作
excel·vba·vsto
_丿丨丨_2 天前
Apache Httpd 多后缀解析
网络·apache