zoukankan      html  css  js  c++  java
  • POI修改Excel

    Java code
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    public class ChangeCell {
    
        @SuppressWarnings("deprecation")
        public static void main(String[] args) {
            String fileToBeRead = "C:\\exp.xls"; // excel位置
            int coloum = 1; // 比如你要获取第1列
            try {
                HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
                        fileToBeRead));
                HSSFSheet sheet = workbook.getSheet("Sheet1");
    
                for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                    HSSFRow row = sheet.getRow((short) i);
                    if (null == row) {
                        continue;
                    } else {
                        HSSFCell cell = row.getCell((short) coloum);
                        if (null == cell) {
                            continue;
                        } else {
                            System.out.println(cell.getNumericCellValue());
                            int temp = (int) cell.getNumericCellValue();
                            cell.setCellValue(temp + 1);
                        }
                    }
                }
                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(fileToBeRead);
                    workbook.write(out);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
    }
    
    
  • 相关阅读:
    Java创建和解析Json对象
    Tyche 2191 WYF的递推式
    Tyche 2147 旅行
    Tyche 2317 Color
    洛谷 P1092 虫食算
    洛谷 P3951 小凯的疑惑
    BZOJ 1800 [Ahoi2009]fly 飞行棋
    BZOJ 1034 [ZJOI2008]泡泡堂BNB
    洛谷 P2151 [SDOI2009]HH去散步
    Android开发环境配置
  • 原文地址:https://www.cnblogs.com/ivan0626/p/2937832.html
Copyright © 2011-2022 走看看