apache poi重写

本文概述

  • Apache POI重写示例
要将数据重写到现有的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("srcmini.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("srcmini.xls")) { wb.write(fileOut); } }catch(Exception e) { System.out.println(e); } } }

输出:
apache poi重写

文章图片
【apache poi重写】之后在202处重写102。
apache poi重写

文章图片

    推荐阅读