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();
}
相关推荐
Abigail_chow1 小时前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
Yungoal4 小时前
php & apache构建 Web 服务器
服务器·php·apache
xiaohezi12 小时前
Rag chunk 之:Excel 文档解析
excel
weixin_4723394616 小时前
python批量解析提取word内容到excel
python·word·excel
1 天前
Unity与Excel表格交互热更方案
unity·游戏引擎·excel
金融小白数据分析之路1 天前
Excel高级函数使用FILTER、UNIQUE、INDEX
excel
未来之窗软件服务1 天前
Excel表格批量下载 CyberWin Excel Doenlaoder 智能编程-——玄武芯辰
excel·批量下载·仙盟创梦ide·东方仙盟
阿斯加德的IT1 天前
Power Automate: 从Excel 选择列,每200条生成一个CSV文件并保存在sharepoint文档库
低代码·excel
步达硬件1 天前
【转bin】EXCEL数据转bin
excel
wtsolutions1 天前
JSON to Excel 3.0.0 版本发布 - 从Excel插件到Web应用的转变
json·excel·json-to-excel·wtsolutions