如何在Apache POI中将xslm文件另存为xslx

时间:2018-03-06 20:01:00

标签: apache-poi

所有

我是Excel和APCH POI的新手,我想知道如何阅读Excel .xlsm文件(Macro Enable excel)并使用Apache POI将其保存为.xlsx文件?

任何例子都将受到赞赏

1 个答案:

答案 0 :(得分:2)

从我的评论中回答。希望它会更清楚。

以下代码确实从XSSFWorkbook模板创建了Workbook.xlsm,该模板保持不变。

然后在此工作簿中进行更改,在将副本保存为*.xlsx之前,当前状态将保存为WorkbookNew.xlsm文件。所以宏保持不变。

然后,VBA将被删除,内容类型将设置为XLSX,并且将进行进一步的更改。然后,此副本将保存为WorkbookNew.xlsx文件。

之后,将通过从先前保存的WorkbookNew.xlsm文件再次创建工作簿来恢复上一个工作簿状态。然后将进一步改变,然后写出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();

 }
}