Improved .xlsm support - still not quite there though
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@680857 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2eb8c41181
commit
c090d99907
@ -80,6 +80,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||||||
"/xl/workbook.xml",
|
"/xl/workbook.xml",
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
public static final XSSFRelation MACROS_WORKBOOK = new XSSFRelation(
|
||||||
|
"application/vnd.ms-excel.sheet.macroEnabled.main+xml",
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",
|
||||||
|
"/xl/workbook.xml",
|
||||||
|
null
|
||||||
|
);
|
||||||
public static final XSSFRelation WORKSHEET = new XSSFRelation(
|
public static final XSSFRelation WORKSHEET = new XSSFRelation(
|
||||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
|
||||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
|
||||||
@ -255,6 +261,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Are we a normal workbook, or a macro enabled one? */
|
||||||
|
private boolean isMacroEnabled = false;
|
||||||
|
|
||||||
private CTWorkbook workbook;
|
private CTWorkbook workbook;
|
||||||
|
|
||||||
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
|
||||||
@ -288,6 +297,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||||||
WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream());
|
WorkbookDocument doc = WorkbookDocument.Factory.parse(getCorePart().getInputStream());
|
||||||
this.workbook = doc.getWorkbook();
|
this.workbook = doc.getWorkbook();
|
||||||
|
|
||||||
|
// Are we macro enabled, or just normal?
|
||||||
|
isMacroEnabled =
|
||||||
|
getCorePart().getContentType().equals(MACROS_WORKBOOK.getContentType());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Load shared strings
|
// Load shared strings
|
||||||
this.sharedStringSource = (SharedStringSource)
|
this.sharedStringSource = (SharedStringSource)
|
||||||
@ -660,6 +673,14 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||||||
return sr;
|
return sr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Are we a normal workbook (.xlsx), or a
|
||||||
|
* macro enabled workbook (.xlsm)?
|
||||||
|
*/
|
||||||
|
public boolean isMacroEnabled() {
|
||||||
|
return isMacroEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
public void insertChartRecord() {
|
public void insertChartRecord() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
@ -764,16 +785,21 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void write(OutputStream stream) throws IOException {
|
public void write(OutputStream stream) throws IOException {
|
||||||
|
// What kind of workbook are we?
|
||||||
|
XSSFRelation workbookRelation = WORKBOOK;
|
||||||
|
if(isMacroEnabled) {
|
||||||
|
workbookRelation = MACROS_WORKBOOK;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create a package referring the temp file.
|
// Create a package referring the temp file.
|
||||||
Package pkg = Package.create(stream);
|
Package pkg = Package.create(stream);
|
||||||
// Main part
|
// Main part
|
||||||
PackagePartName corePartName = PackagingURIHelper.createPartName("/xl/workbook.xml");
|
PackagePartName corePartName = PackagingURIHelper.createPartName(workbookRelation.getDefaultFileName());
|
||||||
// Create main part relationship
|
// Create main part relationship
|
||||||
pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
|
pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
|
||||||
// Create main document part
|
// Create main document part
|
||||||
PackagePart corePart = pkg.createPart(corePartName, WORKBOOK.getContentType());
|
PackagePart corePart = pkg.createPart(corePartName, workbookRelation.getContentType());
|
||||||
OutputStream out;
|
OutputStream out;
|
||||||
|
|
||||||
XmlOptions xmlOptions = new XmlOptions();
|
XmlOptions xmlOptions = new XmlOptions();
|
||||||
@ -840,9 +866,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// VBA Macros
|
// Macro related bits
|
||||||
|
if(isMacroEnabled) {
|
||||||
|
// Copy VBA Macros if present
|
||||||
if(VBA_MACROS.exists( getCorePart() )) {
|
if(VBA_MACROS.exists( getCorePart() )) {
|
||||||
// Copy over
|
|
||||||
try {
|
try {
|
||||||
XSSFModel vba = VBA_MACROS.load(getCorePart());
|
XSSFModel vba = VBA_MACROS.load(getCorePart());
|
||||||
VBA_MACROS.save(vba, corePart);
|
VBA_MACROS.save(vba, corePart);
|
||||||
@ -850,6 +877,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
|
|||||||
throw new RuntimeException("Unable to copy vba macros over", e);
|
throw new RuntimeException("Unable to copy vba macros over", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Now we can write out the main Workbook, with
|
// Now we can write out the main Workbook, with
|
||||||
// the correct references to the other parts
|
// the correct references to the other parts
|
||||||
|
@ -20,6 +20,7 @@ package org.apache.poi.xssf.usermodel;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
@ -85,6 +86,7 @@ public class TestXSSFBugs extends TestCase {
|
|||||||
Package pkg = Package.open(getFilePath("45431.xlsm"));
|
Package pkg = Package.open(getFilePath("45431.xlsm"));
|
||||||
XSSFWorkbook wb = new XSSFWorkbook(pkg);
|
XSSFWorkbook wb = new XSSFWorkbook(pkg);
|
||||||
|
|
||||||
|
// Check the various macro related bits can be found
|
||||||
PackagePart vba = pkg.getPart(
|
PackagePart vba = pkg.getPart(
|
||||||
PackagingURIHelper.createPartName("/xl/vbaProject.bin")
|
PackagingURIHelper.createPartName("/xl/vbaProject.bin")
|
||||||
);
|
);
|
||||||
@ -106,6 +108,10 @@ public class TestXSSFBugs extends TestCase {
|
|||||||
);
|
);
|
||||||
assertNotNull(vba);
|
assertNotNull(vba);
|
||||||
|
|
||||||
|
FileOutputStream fout = new FileOutputStream("/tmp/foo.xlsm");
|
||||||
|
nwb.write(fout);
|
||||||
|
fout.close();
|
||||||
|
|
||||||
// For testing with excel
|
// For testing with excel
|
||||||
// FileOutputStream fout = new FileOutputStream("/tmp/foo.xlsm");
|
// FileOutputStream fout = new FileOutputStream("/tmp/foo.xlsm");
|
||||||
// nwb.write(fout);
|
// nwb.write(fout);
|
||||||
|
Loading…
Reference in New Issue
Block a user