Here's my code :
Using java Syntax Highlighting
File tplfile = null;
OpcPackage tpl = null;
SpreadsheetMLPackage pkg = null;
FileOutputStream os = null;
InputStream is = null;
Logger.getLogger(ExcelWriter.class.getName()).info("Getting template : template.xslm");
try
{
is = ExcelWriter.class.getResource("template.xlsm").openStream();
tplfile = File.createTempFile("tpl", ".xslm");
tplfile.deleteOnExit();
os = new FileOutputStream(tplfile);
byte[] temp = new byte[32768];
int rc;
while((rc = is.read(temp)) > 0)
{
os.write(temp, 0, rc);
}
is.close();
os.close();
}
catch(IOException ex)
{
Logger.getLogger(WordWriter.class.getName()).error("IO Exception");
}
try
{
tpl = OpcPackage.load(tplfile);
}
catch(Docx4JException ex)
{
Logger.getLogger(ExcelWriter.class.getName()).error("Error opening Excel template. Creating empty document");
}
WorksheetPart sheet = null;
if(tpl != null)
{
RelationshipsPart rp = tpl.getRelationshipsPart();
for(Relationship r : rp.getRelationships().getRelationship())
{
Part part = rp.getPart(r);
if(part instanceof WorksheetPart)
{
sheet = (WorksheetPart) part;
}
}
if(sheet == null)
{
Logger.getLogger(ExcelWriter.class.getName()).error("Could not find the template's sheet. Creating empty document");
}
}
if(sheet == null)
{
pkg = SpreadsheetMLPackage.createPackage();
sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Test Dossier", 1);
}
OpcPackage tpl = null;
SpreadsheetMLPackage pkg = null;
FileOutputStream os = null;
InputStream is = null;
Logger.getLogger(ExcelWriter.class.getName()).info("Getting template : template.xslm");
try
{
is = ExcelWriter.class.getResource("template.xlsm").openStream();
tplfile = File.createTempFile("tpl", ".xslm");
tplfile.deleteOnExit();
os = new FileOutputStream(tplfile);
byte[] temp = new byte[32768];
int rc;
while((rc = is.read(temp)) > 0)
{
os.write(temp, 0, rc);
}
is.close();
os.close();
}
catch(IOException ex)
{
Logger.getLogger(WordWriter.class.getName()).error("IO Exception");
}
try
{
tpl = OpcPackage.load(tplfile);
}
catch(Docx4JException ex)
{
Logger.getLogger(ExcelWriter.class.getName()).error("Error opening Excel template. Creating empty document");
}
WorksheetPart sheet = null;
if(tpl != null)
{
RelationshipsPart rp = tpl.getRelationshipsPart();
for(Relationship r : rp.getRelationships().getRelationship())
{
Part part = rp.getPart(r);
if(part instanceof WorksheetPart)
{
sheet = (WorksheetPart) part;
}
}
if(sheet == null)
{
Logger.getLogger(ExcelWriter.class.getName()).error("Could not find the template's sheet. Creating empty document");
}
}
if(sheet == null)
{
pkg = SpreadsheetMLPackage.createPackage();
sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Test Dossier", 1);
}
Parsed in 0.019 seconds, using GeSHi 1.0.8.4
But ... (yes there's always a but) this doesn't work !!!
It always goes in my last if, meaning I cannot find a WorkSheetPart in my file. Except I have one...
Any idea on what's wrong here ?
Thanks