28 Temmuz 2020 Salı

POI Excel OPCPackage Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.apache.poi.openxml4j.opc.OPCPackage
XSSFReader nesnesi yaratmak için kullanılır.

open metodu - InputStream
Örnek - XSSFWorkbook 
Şöyle yaparız
import org.apache.poi.ss.usermodel.*;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

File file = new File("./inspection.xlsx");

//OPCPackage pkg = OPCPackage.open(file);
OPCPackage pkg = OPCPackage.open(new FileInputStream(file));
XSSFWorkbook wb = new XSSFWorkbook(pkg);
    
//XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(file));
//Workbook wb = WorkbookFactory.create(new FileInputStream(file));

//wb -> sheets -> rows -> cols
int finding = 445;

DataFormatter formatter = new DataFormatter();
boolean write = false;
for(Sheet sheet : wb) {
  for(Row row : sheet) {
    if(row.getCell(0)!=null && !formatter.formatCellValue(row.getCell(0)).equals("")) {
      Cell cell = row.getCell(0);
      String text = formatter.formatCellValue(cell);
      if('0'<=text.charAt(0) && text.charAt(0)<='9') {
        int id = Integer.parseInt(text);
        if (id == finding) {
          System.out.println(sheet.getSheetName());
          System.out.println(sheet.getRow(row.getRowNum()).getCell(1));
          Cell cellCurrent = row.getCell(2);
          if (cellCurrent == null) {
            cellCurrent = row.createCell(2);
          }
          cellCurrent.setCellValue("X");
          write = true;
        }
      }
    }
  }
}
if (write) {
  FileOutputStream outputStream = new FileOutputStream(file);
  wb.write(outputStream);
  outputStream.close();
  wb.close();
}
Örnek - XSSReader
Şöyle yaparız
def pkg = OPCPackage.open(request.getBody(InputStream.class))
def xssfReader = new XSSFReader(pkg)

Hiç yorum yok:

Yorum Gönder