• 【POI】修改Excel内容


     

     
      1 package com.what21.test;
      2  
      3 import java.io.File;
      4 import java.io.FileInputStream;
      5 import java.io.FileNotFoundException;
      6 import java.io.FileOutputStream;
      7 import java.io.IOException;
      8  
      9 import org.apache.poi.hssf.usermodel.HSSFCell;
     10 import org.apache.poi.hssf.usermodel.HSSFRow;
     11 import org.apache.poi.hssf.usermodel.HSSFSheet;
     12 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
     13 import org.apache.poi.xssf.usermodel.XSSFCell;
     14 import org.apache.poi.xssf.usermodel.XSSFSheet;
     15 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
     16  
     17 public class TestPoi {
     18     public static void updateExcel(File exlFile, String sheetName, int col,
     19             int row, String value) throws Exception {
     20         FileInputStream fis = new FileInputStream(exlFile);
     21         HSSFWorkbook workbook = new HSSFWorkbook(fis);
     22         // workbook.
     23         HSSFSheet sheet = workbook.getSheet(sheetName);
     24         HSSFCell mycell = sheet.createRow(row).createCell(col);
     25         mycell.setCellValue(value);
     26         HSSFRow r = sheet.getRow(row);
     27         HSSFCell cell = r.getCell(col);
     28         // int type=cell.getCellType();
     29         String str1 = cell.getStringCellValue();
     30         // 这里假设对应单元格原来的类型也是String类型
     31         cell.setCellValue(value);
     32         System.out.println("单元格原来值为" + str1);
     33         System.out.println("单元格值被更新为" + value);
     34  
     35         fis.close();// 关闭文件输入流
     36  
     37         FileOutputStream fos = new FileOutputStream(exlFile);
     38         workbook.write(fos);
     39         fos.close();// 关闭文件输出流
     40     }
     41  
     42     public static void update2(String url) {
     43         int coloum = 2; // 比如你要获取第1列
     44         try {
     45             HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(url));
     46             HSSFSheet sheet = workbook.getSheet("Sheet1");
     47  
     48             for (int i = 0; i <= sheet.getLastRowNum(); i++) {
     49                 HSSFRow row = sheet.getRow((short) i);
     50                 if (null == row) {
     51                     continue;
     52                 } else {
     53                     HSSFCell cell = row.getCell((short) coloum);
     54                     if (null == cell) {
     55                         continue;
     56                     } else {
     57  
     58                         cell.setCellValue("he1");
     59                     }
     60                 }
     61             }
     62             FileOutputStream out = null;
     63             try {
     64                 out = new FileOutputStream(url);
     65                 workbook.write(out);
     66             } catch (IOException e) {
     67                 e.printStackTrace();
     68             } finally {
     69                 try {
     70                     out.close();
     71                 } catch (IOException e) {
     72                     e.printStackTrace();
     73                 }
     74             }
     75         } catch (FileNotFoundException e) {
     76             e.printStackTrace();
     77         } catch (IOException e) {
     78             e.printStackTrace();
     79         }
     80     }
     81  
     82     public static boolean writeXlsx(String fileName, int row, int column,
     83             String content) {
     84         boolean flag = false;
     85         FileOutputStream out = null;
     86         XSSFWorkbook xwb;
     87         try {
     88             xwb = new XSSFWorkbook(new FileInputStream(fileName));
     89             XSSFSheet xSheet = xwb.getSheetAt(0);
     90             XSSFCell xCell = xSheet.createRow(row).createCell(column);
     91             xCell.setCellValue(content);
     92             out = new FileOutputStream(fileName);
     93             xwb.write(out);
     94             out.close();
     95             flag = true;
     96         } catch (IOException e) {
     97             e.printStackTrace();
     98         } catch (RuntimeException e) {
     99             e.printStackTrace();
    100         }
    101         return flag;
    102     }
    103  
    104     public static void main(String[] args) throws Exception {
    105         // TODO Auto-generated method stub
    106         // 下面改成你自己的xls文件进行测试,2003格式的,不能2007
    107         File file = new File("C:\\Users\\Administrator\\Desktop\\test.xls");
    108         // 下面尝试更改第一行第一列的单元格的值
    109         updateExcel(file, "Sheet1", 0, 0, "hehe");
    110         update2("C:\\Users\\Administrator\\Desktop\\test.xls");
    111         File file1 = new File(
    112                 "C:\\Users\\Administrator\\Desktop\\test - 副本.xlsx");
    113         writeXlsx("C:\\Users\\Administrator\\Desktop\\test - 副本.xlsx", 0, 0,
    114                 "1");
    115     }
    116 }
    View Code

      注意事项:如果修改的坐标对应的单元格是空,会报错。

      建议这么写:

      HSSFCell mycell = sheet.createRow(row).createCell(col);
      mycell.setCellValue(value);

      这样写有一个问题:如果定位错误,会带来不必要的麻烦。

      通常在生成原始Excel时,最好能对空单元格赋空(强制添加空字符串),这样在使用SAXPOI或者SAX解析较大文本的Excel的时候,可以

    有效的避免因为空单元格而导致列数据错位的问题。

      上面的代码是很常见的,在网上能找到一堆。根据不同的场景可以传入一组需要修改的坐标,通过循环来修改。这样可以减少打开文件的次数,提高

    效率。

  • 相关阅读:
    vue-cli项目npm run build后,index.html无法在浏览器打开
    前端工具
    关于 IIS 中 iconfont.woff 报 404(Not Found) 的原因即解决方法
    表格行上下移动、置顶的Javascript代码
    纯js国际化(使用技术:i18n)
    【整理】前端国际化小结
    js比较两个数字的大小不能直接比
    input事件中文触发多次问题研究
    linux(centos8):使用tree命令查看目录结构
    nginx安全:配置网站图片防盗链
  • 原文地址:https://www.cnblogs.com/flydkPocketMagic/p/5998455.html
Copyright © 2020-2023  润新知