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();
}
相关推荐
D愿你归来仍是少年17 小时前
Apache Spark 第 9 章:Spark 性能调优
大数据·spark·apache
fengyehongWorld21 小时前
VBA 操作PowerQuery表格和带名字的表格
excel·vba
Hello.Reader21 小时前
Apache Arrow 在 PySpark 中的使用提速 Pandas 转换与 UDF 的关键武器
apache·pandas
tangyal1 天前
Linux 核心操作合集(网络配置、XShell远程连接、vim文本编辑与操作、权限管理 实操手册)
excel
言之。1 天前
Apache ZooKeeper 核心技术全解(面试+实战版)
zookeeper·面试·apache
softbangong2 天前
815-批量Excel文件合并工具,批量excel文件、工作表合并软件
linux·windows·excel·文件合并·excel合并·数据整理
城数派2 天前
2000-2024年1km精度人口分布栅格数据(全球/全国/分省/分市)
arcgis·信息可视化·数据分析·excel
城数派2 天前
1984-2024年中国10米分辨率城市土地利用栅格数据(商业、公服、居住等9类)
arcgis·信息可视化·数据分析·excel
城数派2 天前
2015-2024年我国1km分辨率逐日地表温度(LST)栅格数据
数据库·arcgis·信息可视化·数据分析·excel
城数派2 天前
中国全国土壤有机碳密度数据集(2010-2024年)
数据库·arcgis·信息可视化·数据分析·excel