济南网站建设手机,畅想网络网站建设推广,怎么做招聘网站,h5自响应式网站模版使用 Apache POI 更新特定的单元格 一. 需求二. 实现三. 效果 一. 需求
将以下表中第4行#xff0c;第4列的单元格由“张宇”更新为“汤家凤”#xff0c;并将更行后的结果写入新的Excel文件中#xff1b; 二. 实现
使用Apache POI#xff0c;可以精确定位到需要更改的单… 使用 Apache POI 更新特定的单元格 一. 需求二. 实现三. 效果 一. 需求
将以下表中第4行第4列的单元格由“张宇”更新为“汤家凤”并将更行后的结果写入新的Excel文件中 二. 实现
使用Apache POI可以精确定位到需要更改的单元格高定制化的场景有时可能不适合用easyExcel
步骤
由 file 依次 获取 workbook、sheet、row、cell更新 cell关闭 输入流用新文件的path创建输出流将更改后的 workbook 通过输出流 写入 新文件关闭 workbook和输出流。
import org.apache.poi.xssf.usermodel.*;
import org.junit.Test;
import java.io.*;public class poiTest {Testpublic void update() throws Exception{String sourceFile C:\\Users\\liziq\\Desktop\\student.xlsx; // 原文件String newFile C:\\Users\\liziq\\Desktop\\student-new.xlsx; // 更新后的新文件// 创建输入流FileInputStream fileInputStream new FileInputStream(sourceFile);// 获取 workbookXSSFWorkbook wb new XSSFWorkbook(fileInputStream);// 获取 sheetXSSFSheet sheet wb.getSheetAt(0);// 获取单元格index是从0开始XSSFRow row sheet.getRow(3);XSSFCell cell row.getCell(3);// 更新单元格cell.setCellValue(汤家凤);// 关闭输入流fileInputStream.close();// 创建输出流FileOutputStream fileOutputStreamnew FileOutputStream(newFile);// 将 workbook 写入 newFilewb.write(fileOutputStream);// 关闭workbook和输出流wb.close();fileOutputStream.close();}
}三. 效果
生成“student-new.xlsx”教高数的变成了“汤家凤” 参考 https://blog.csdn.net/zouxiongqqq/article/details/78478298