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();
}
相关推荐
左师佑图33 分钟前
综合案例:Python 数据处理——从Excel文件到数据分析
开发语言·python·数据分析·excel·pandas
SamDeepThinking7 小时前
EasyExcel 流式处理中实现末尾行过滤的技术方案
excel
小白学鸿蒙8 小时前
鸿蒙数据库表中的数据如何导出为Excel存到系统下载目录
数据库·excel·harmonyos
CodeCraft Studio10 小时前
MPP文件处理组件Aspose.Tasks教程:使用Python在Excel中打开MPP文件
python·ui·excel·csv·mpp·aspose·ms project
Python大数据分析@12 小时前
如何用 Python xlwings库自动化操作 Excel?
python·自动化·excel
一个天蝎座 白勺 程序猿12 小时前
Apache IoTDB(7):设备模板管理——工业物联网元数据标准化的破局之道
数据库·apache·时序数据库·iotdb
惜.己12 小时前
apache启动失败Failed to start The Apache HTTP Server.
apache
老师可可13 小时前
成绩查询系统如何制作?
经验分享·学习·小程序·excel·学习方法
yanweijie031713 小时前
Excel(WPS表格)中多列去重就用Tocol+Unique组合函数
excel·wps
Jackiejin5261 天前
WPS表格选择性粘贴快捷键怎么使用?WPS如何给表格某一行都加上指定数字?
数据分析·excel·wps