List of usage examples for org.apache.poi.poifs.filesystem POIFSFileSystem POIFSFileSystem
public POIFSFileSystem(InputStream stream) throws IOException
From source file:org.springframework.web.servlet.view.document.AbstractExcelView.java
License:Apache License
/** * Creates the workbook from an existing XLS document. * @param url the URL of the Excel template without localization part nor extension * @param request current HTTP request/*from ww w.jav a 2s . co m*/ * @return the HSSFWorkbook * @throws Exception in case of failure */ protected HSSFWorkbook getTemplateSource(String url, HttpServletRequest request) throws Exception { LocalizedResourceHelper helper = new LocalizedResourceHelper(getApplicationContext()); Locale userLocale = RequestContextUtils.getLocale(request); Resource inputFile = helper.findLocalizedResource(url, EXTENSION, userLocale); // Create the Excel document from the source. if (logger.isDebugEnabled()) { logger.debug("Loading Excel workbook from " + inputFile); } POIFSFileSystem fs = new POIFSFileSystem(inputFile.getInputStream()); return new HSSFWorkbook(fs); }
From source file:org.springframework.web.servlet.view.document.ExcelTestSuite.java
License:Apache License
public void testExcel() throws Exception { AbstractExcelView excelView = new AbstractExcelView() { protected void buildExcelDocument(Map model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.createSheet(); wb.setSheetName(0, "Test Sheet"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); }/*from w w w.ja va 2 s . co m*/ }; excelView.render(new HashMap(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); assertEquals("Test Sheet", wb.getSheetName(0)); HSSFSheet sheet = wb.getSheet("Test Sheet"); HSSFRow row = sheet.getRow(2); HSSFCell cell = row.getCell((short) 4); assertEquals("Test Value", cell.getStringCellValue()); }
From source file:org.springframework.web.servlet.view.document.ExcelTestSuite.java
License:Apache License
public void testExcelWithTemplateNoLoc() throws Exception { request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("nl", "nl")); AbstractExcelView excelView = new AbstractExcelView() { protected void buildExcelDocument(Map model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); }//w ww .j a v a 2 s . co m }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); HSSFSheet sheet = wb.getSheet("Sheet1"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell((short) 0); assertEquals("Test Template", cell.getStringCellValue()); }
From source file:org.springframework.web.servlet.view.document.ExcelTestSuite.java
License:Apache License
public void testExcelWithTemplateAndCountryAndLanguage() throws Exception { request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("en", "US")); AbstractExcelView excelView = new AbstractExcelView() { protected void buildExcelDocument(Map model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); }//from ww w . ja va 2 s . c om }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); HSSFSheet sheet = wb.getSheet("Sheet1"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell((short) 0); assertEquals("Test Template American English", cell.getStringCellValue()); }
From source file:org.springframework.web.servlet.view.document.ExcelTestSuite.java
License:Apache License
public void testExcelWithTemplateAndLanguage() throws Exception { request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("de", "")); AbstractExcelView excelView = new AbstractExcelView() { protected void buildExcelDocument(Map model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); }//w w w . j av a2s. c o m }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); HSSFSheet sheet = wb.getSheet("Sheet1"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell((short) 0); assertEquals("Test Template auf Deutsch", cell.getStringCellValue()); }
From source file:org.springframework.web.servlet.view.document.ExcelViewTests.java
License:Apache License
@Test public void testExcel() throws Exception { AbstractExcelView excelView = new AbstractExcelView() { @Override/*ww w .jav a 2s . c o m*/ protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.createSheet("Test Sheet"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); } }; excelView.render(new HashMap<String, Object>(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); assertEquals("Test Sheet", wb.getSheetName(0)); HSSFSheet sheet = wb.getSheet("Test Sheet"); HSSFRow row = sheet.getRow(2); HSSFCell cell = row.getCell(4); assertEquals("Test Value", cell.getStringCellValue()); }
From source file:org.springframework.web.servlet.view.document.ExcelViewTests.java
License:Apache License
@Test public void testExcelWithTemplateNoLoc() throws Exception { request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("nl", "nl")); AbstractExcelView excelView = new AbstractExcelView() { @Override/*from w w w .ja v a 2s . c om*/ protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); } }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap<String, Object>(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); HSSFSheet sheet = wb.getSheet("Sheet1"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell(0); assertEquals("Test Template", cell.getStringCellValue()); }
From source file:org.springframework.web.servlet.view.document.ExcelViewTests.java
License:Apache License
@Test public void testExcelWithTemplateAndCountryAndLanguage() throws Exception { request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("en", "US")); AbstractExcelView excelView = new AbstractExcelView() { @Override/* w w w .java 2 s. co m*/ protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); } }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap<String, Object>(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); HSSFSheet sheet = wb.getSheet("Sheet1"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell(0); assertEquals("Test Template American English", cell.getStringCellValue()); }
From source file:org.springframework.web.servlet.view.document.ExcelViewTests.java
License:Apache License
@Test public void testExcelWithTemplateAndLanguage() throws Exception { request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, newDummyLocaleResolver("de", "")); AbstractExcelView excelView = new AbstractExcelView() { @Override/* w ww . ja v a2s.c o m*/ protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFSheet sheet = wb.getSheet("Sheet1"); // test all possible permutation of row or column not existing HSSFCell cell = getCell(sheet, 2, 4); cell.setCellValue("Test Value"); cell = getCell(sheet, 2, 3); setText(cell, "Test Value"); cell = getCell(sheet, 3, 4); setText(cell, "Test Value"); cell = getCell(sheet, 2, 4); setText(cell, "Test Value"); } }; excelView.setApplicationContext(webAppCtx); excelView.setUrl("template"); excelView.render(new HashMap<String, Object>(), request, response); POIFSFileSystem poiFs = new POIFSFileSystem(new ByteArrayInputStream(response.getContentAsByteArray())); HSSFWorkbook wb = new HSSFWorkbook(poiFs); HSSFSheet sheet = wb.getSheet("Sheet1"); HSSFRow row = sheet.getRow(0); HSSFCell cell = row.getCell(0); assertEquals("Test Template auf Deutsch", cell.getStringCellValue()); }
From source file:org.talend.dataprep.schema.xls.streaming.StreamingWorkbookReader.java
License:Open Source License
public void init(File f) { try {//ww w . ja v a2 s.co m if (builder.getPassword() != null) { // Based on: https://poi.apache.org/encryption.html POIFSFileSystem poifs = new POIFSFileSystem(f); EncryptionInfo info = new EncryptionInfo(poifs); Decryptor d = Decryptor.getInstance(info); d.verifyPassword(builder.getPassword()); pkg = OPCPackage.open(d.getDataStream(poifs)); } else { pkg = OPCPackage.open(f); } XSSFReader reader = new XSSFReader(pkg); SharedStringsTable sst = reader.getSharedStringsTable(); StylesTable styles = reader.getStylesTable(); loadSheets(reader, sst, styles, builder.getRowCacheSize()); } catch (IOException e) { throw new OpenException("Failed to open file", e); } catch (OpenXML4JException | XMLStreamException e) { throw new ReadException("Unable to read workbook", e); } catch (GeneralSecurityException e) { throw new ReadException("Unable to read workbook - Decryption failed", e); } }