use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.
the class XSSFFileHandler method handleFile.
@Override
public void handleFile(InputStream stream, String path) throws Exception {
// ignore password protected files
if (POIXMLDocumentHandler.isEncrypted(stream))
return;
final XSSFWorkbook wb;
// make sure the potentially large byte-array is freed up quickly again
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(stream, out);
final byte[] bytes = out.toByteArray();
checkXSSFReader(OPCPackage.open(new ByteArrayInputStream(bytes)));
wb = new XSSFWorkbook(new ByteArrayInputStream(bytes));
}
// use the combined handler for HSSF/XSSF
handleWorkbook(wb);
// TODO: some documents fail currently...
//XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(wb);
//evaluator.evaluateAll();
// also verify general POIFS-stuff
new POIXMLDocumentHandler().handlePOIXMLDocument(wb);
// and finally ensure that exporting to XML works
exportToXML(wb);
// this allows to trigger a heap-dump at this point to see which memory is still allocated
//HeapDump.dumpHeap("/tmp/poi.hprof", false);
wb.close();
}
use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.
the class XSSFSave method main.
public static void main(String[] args) throws Exception {
for (int i = 0; i < args.length; i++) {
OPCPackage pkg = OPCPackage.open(args[i]);
XSSFWorkbook wb = new XSSFWorkbook(pkg);
int sep = args[i].lastIndexOf('.');
String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x");
FileOutputStream out = new FileOutputStream(outfile);
wb.write(out);
out.close();
pkg.close();
}
}
use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.
the class WorkingWithPictures method main.
public static void main(String[] args) throws IOException {
//create a new workbook
//or new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
try {
CreationHelper helper = wb.getCreationHelper();
//add a picture in this workbook.
InputStream is = new FileInputStream(args[0]);
byte[] bytes = IOUtils.toByteArray(is);
is.close();
int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
//create sheet
Sheet sheet = wb.createSheet();
//create drawing
Drawing<?> drawing = sheet.createDrawingPatriarch();
//add a picture shape
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(1);
anchor.setRow1(1);
Picture pict = drawing.createPicture(anchor, pictureIdx);
//auto-size picture
pict.resize(2);
//save workbook
String file = "picture.xls";
if (wb instanceof XSSFWorkbook) {
// NOSONAR
file += "x";
}
OutputStream fileOut = new FileOutputStream(file);
try {
wb.write(fileOut);
} finally {
fileOut.close();
}
} finally {
wb.close();
}
}
use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.
the class LoadEmbedded method loadEmbedded.
public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException {
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
if (contentType.equals("application/vnd.ms-excel")) {
// Excel Workbook - either binary or OpenXML
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
embeddedWorkbook.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
// Excel Workbook - OpenXML file format
XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
embeddedWorkbook.close();
} else if (contentType.equals("application/msword")) {
// Word Document - binary (OLE2CDF) file format
HWPFDocument document = new HWPFDocument(pPart.getInputStream());
document.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
// Word Document - OpenXML file format
XWPFDocument document = new XWPFDocument(pPart.getInputStream());
document.close();
} else if (contentType.equals("application/vnd.ms-powerpoint")) {
// PowerPoint Document - binary file format
HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream());
slideShow.close();
} else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
// PowerPoint Document - OpenXML file format
XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream());
slideShow.close();
} else {
// Any other type of embedded object.
System.out.println("Unknown Embedded Document: " + contentType);
InputStream inputStream = pPart.getInputStream();
inputStream.close();
}
}
}
use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.
the class HybridStreaming method main.
public static void main(String[] args) throws IOException, SAXException {
InputStream sourceBytes = new FileInputStream("workbook.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(sourceBytes) {
/** Avoid DOM parse of large sheet */
@Override
public void parseSheet(java.util.Map<String, XSSFSheet> shIdMap, CTSheet ctSheet) {
if (!SHEET_TO_STREAM.equals(ctSheet.getName())) {
super.parseSheet(shIdMap, ctSheet);
}
}
};
// Having avoided a DOM-based parse of the sheet, we can stream it instead.
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(workbook.getPackage());
new XSSFSheetXMLHandler(workbook.getStylesSource(), strings, createSheetContentsHandler(), false);
workbook.close();
sourceBytes.close();
}
Aggregations