POI Excel - 数据重写

POI Excel - 数据重写 首页 / POI入门教程 / POI Excel - 数据重写

要将数据重写到现有的excel文档中,Apache POI提供了各种方法getRow(),getCell(),getSheet()等。

Apache POI 重写示例

package poiexample;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class RewritingExample {
	public static void main(String[] args) throws FileNotFoundException, IOException, EncryptedDocumentException, InvalidFormatException {
		try (InputStream inp = new FileInputStream("Learnfk.xls")) {
		        Workbook wb = WorkbookFactory.create(inp);
		        Sheet sheet = wb.getSheetAt(0);
		        Row row = sheet.getRow(2);
		        Cell cell = row.getCell(3);
		        if (cell == null)
		            cell = row.createCell(3);
		        cell.setCellType(CellType.STRING);
		        cell.setCellValue("101");	    
		        try (OutputStream fileOut = new FileOutputStream("Learnfk.xls")) {
		            wb.write(fileOut);
		        }
	    }catch(Exception e) {
	    	System.out.println(e);
	    }
	}
}

输出:

Apache POI Rewriting

之后在202处重写102。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/apache-poi-word/apache-poi-rewriting.html

来源:LearnFk无涯教程网

Apache POI Rewriting

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学架构 -〔李运华〕

Linux实战技能100讲 -〔尹会生〕

黄勇的OKR实战笔记 -〔黄勇〕

DevOps实战笔记 -〔石雪峰〕

SRE实战手册 -〔赵成〕

张汉东的Rust实战课 -〔张汉东〕

Spark核心原理与实战 -〔王磊〕

深入剖析Java新特性 -〔范学雷〕

Vue 3 企业级项目实战课 -〔杨文坚〕

好记忆不如烂笔头。留下您的足迹吧 :)