如何保存xslm文件中的Apache POI XSLX(How to save an xslm fi

2019-10-29 08:22发布

所有:

我非常新的Excel和apche的POI,我不知道如何在Excel中.XLSM文件读取(宏使Excel),并将其保存为使用Apache POI .xlsx文件?

任何例子可以理解

Answer 1:

从制作我的意见的答复。 希望这将是更清晰即可。

下面的代码并创建XSSFWorkbookWorkbook.xlsm将保持不变模板。

它changings在此工作簿,然后和保存副本之前*.xlsx ,当前状态将被保存为WorkbookNew.xlsm文件。 因此,宏撑保存。

然后, VBA将被删除,内容类型将被设定为XLSX和进一步changings将会作出修改。 那么这个副本将被保存为WorkbookNew.xlsx文件。

在此之后,以前的工作簿状态会从先前保存的再次创建工作簿中回来WorkbookNew.xlsm文件。 然后进一步changings将作出后所有的最终状态WorkbookNew.xlsm文件将被写出。

因此,我们有不变的Workbook.xlsm模板时, WorkbookNew.xlsx WorkbookNew.xlsm然后。

例:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackageRelationship;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.util.regex.Pattern;

class ReadXSLMWriteXLSXWorkbook {

 public static void main(String[] args) throws Exception {

  XSSFWorkbook workbook;
  Sheet sheet;
  Row row;
  Cell cell;
  FileOutputStream out;

  //create workbook from XLSM template
  workbook = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("Workbook.xlsm"));

  //do changings
  sheet = workbook.getSheetAt(0);
  row = sheet.getRow(0);
  if (row == null) row = sheet.createRow(0);
  cell = row.getCell(0);
  if (cell == null) cell = row.createCell(0);
  cell.setCellValue("changed in XLSM before writing as XLSX");

  //write out the current state
  out = new FileOutputStream("WorkbookNew.xlsm");
  workbook.write(out);
  out.close();

  //save copy as XLSX ----------------START
  //remove VBA
  OPCPackage opcpackage = workbook.getPackage();
  //get and remove the vbaProject.bin part from the package
  PackagePart vbapart = opcpackage.getPartsByName(Pattern.compile("/xl/vbaProject.bin")).get(0);
  opcpackage.removePart(vbapart);
  //get and remove the relationship to the removed vbaProject.bin part from the package
  PackagePart wbpart = workbook.getPackagePart();
  PackageRelationshipCollection wbrelcollection = wbpart.getRelationshipsByType("http://schemas.microsoft.com/office/2006/relationships/vbaProject");
  for (PackageRelationship relship : wbrelcollection) {
   wbpart.removeRelationship(relship.getId());
  }
  //set content type to XLSX
  workbook.setWorkbookType(XSSFWorkbookType.XLSX);

  //do changings only in XLSX
  sheet = workbook.getSheetAt(0);
  row = sheet.getRow(1);
  if (row == null) row = sheet.createRow(1);
  cell = row.getCell(1);
  if (cell == null) cell = row.createCell(1);
  cell.setCellValue("changed before writing as XLSX");

  //write out the XLSX
  out = new FileOutputStream("WorkbookNew.xlsx");
  workbook.write(out);
  out.close();
  //save copy as XLSX ----------------END

  //get back the previous saved state
  workbook = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("WorkbookNew.xlsm"));

  //do changings
  sheet = workbook.getSheetAt(0);
  row = sheet.getRow(2);
  if (row == null) row = sheet.createRow(1);
  cell = row.getCell(2);
  if (cell == null) cell = row.createCell(1);
  cell.setCellValue("changed in XLSM after writing as XLSX");

  //write out the XLSM
  out = new FileOutputStream("WorkbookNew.xlsm");
  workbook.write(out);
  out.close();

  workbook.close();

 }
}


文章来源: How to save an xslm file as xslx in Apache POI
标签: apache-poi