use of org.apache.poi.ss.usermodel.Workbook in project OpenRefine by OpenRefine.
the class ExcelImporter method createParserUIInitializationData.
@Override
public JSONObject createParserUIInitializationData(ImportingJob job, List<JSONObject> fileRecords, String format) {
JSONObject options = super.createParserUIInitializationData(job, fileRecords, format);
JSONArray sheetRecords = new JSONArray();
JSONUtilities.safePut(options, "sheetRecords", sheetRecords);
try {
if (fileRecords.size() > 0) {
JSONObject firstFileRecord = fileRecords.get(0);
File file = ImportingUtilities.getFile(job, firstFileRecord);
InputStream is = new FileInputStream(file);
if (!is.markSupported()) {
is = new PushbackInputStream(is, 8);
}
try {
Workbook wb = POIXMLDocument.hasOOXMLHeader(is) ? new XSSFWorkbook(is) : new HSSFWorkbook(new POIFSFileSystem(is));
int sheetCount = wb.getNumberOfSheets();
boolean hasData = false;
for (int i = 0; i < sheetCount; i++) {
Sheet sheet = wb.getSheetAt(i);
int rows = sheet.getLastRowNum() - sheet.getFirstRowNum() + 1;
JSONObject sheetRecord = new JSONObject();
JSONUtilities.safePut(sheetRecord, "name", sheet.getSheetName());
JSONUtilities.safePut(sheetRecord, "rows", rows);
if (hasData) {
JSONUtilities.safePut(sheetRecord, "selected", false);
} else if (rows > 1) {
JSONUtilities.safePut(sheetRecord, "selected", true);
hasData = true;
}
JSONUtilities.append(sheetRecords, sheetRecord);
}
} finally {
is.close();
}
}
} catch (IOException e) {
logger.error("Error generating parser UI initialization data for Excel file", e);
} catch (IllegalArgumentException e) {
logger.error("Error generating parser UI initialization data for Excel file (only Excel 97 & later supported)", e);
} catch (POIXMLException e) {
logger.error("Error generating parser UI initialization data for Excel file - invalid XML", e);
}
return options;
}
use of org.apache.poi.ss.usermodel.Workbook in project spring-framework by spring-projects.
the class XlsViewTests method testXlsxView.
@Test
public void testXlsxView() throws Exception {
View excelView = new AbstractXlsxView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), request, response);
Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertEquals("Test Sheet", wb.getSheetName(0));
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertEquals("Test Value", cell.getStringCellValue());
}
use of org.apache.poi.ss.usermodel.Workbook in project spring-framework by spring-projects.
the class XlsViewTests method testXls.
@Test
public void testXls() throws Exception {
View excelView = new AbstractXlsView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), request, response);
Workbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertEquals("Test Sheet", wb.getSheetName(0));
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertEquals("Test Value", cell.getStringCellValue());
}
use of org.apache.poi.ss.usermodel.Workbook in project Robot-Scouter by SUPERCILEX.
the class SpreadsheetExporter method writeFile.
@Nullable
private File writeFile(File rsFolder) {
FileOutputStream stream = null;
File absoluteFile = new File(rsFolder, getFullyQualifiedFileName(null));
try {
for (int i = 1; true; i++) {
if (absoluteFile.exists()) {
absoluteFile = new File(rsFolder, getFullyQualifiedFileName(" (" + i + ")"));
} else {
absoluteFile = new File(absoluteFile.getParentFile(), hideFile(absoluteFile.getName()));
if (!absoluteFile.createNewFile() && // Attempt deleting existing hidden file (occurs when RS crashes while exporting)
(!absoluteFile.delete() || !absoluteFile.createNewFile())) {
throw new IOException("Failed to create file");
}
break;
}
}
stream = new FileOutputStream(absoluteFile);
Workbook workbook;
try {
workbook = getWorkbook();
} catch (Exception e) {
// NOPMD
absoluteFile.delete();
throw e;
}
workbook.write(stream);
return unhideFile(absoluteFile);
} catch (IOException e) {
showError(this, e);
absoluteFile.delete();
} finally {
if (stream != null)
try {
stream.close();
} catch (IOException e) {
FirebaseCrash.report(e);
}
}
return null;
}
use of org.apache.poi.ss.usermodel.Workbook in project Robot-Scouter by SUPERCILEX.
the class SpreadsheetExporter method buildTeamAveragesSheet.
@AddTrace(name = "buildTeamAveragesSheet")
private void buildTeamAveragesSheet(Sheet averageSheet) {
Workbook workbook = averageSheet.getWorkbook();
Row headerRow = averageSheet.createRow(0);
headerRow.createCell(0);
List<Sheet> scoutSheets = getAdjustedList(workbook);
for (int i = 0; i < scoutSheets.size(); i++) {
Sheet scoutSheet = scoutSheets.get(i);
Row row = averageSheet.createRow(i + 1);
Cell rowHeaderCell = row.createCell(0);
rowHeaderCell.setCellValue(scoutSheet.getSheetName());
rowHeaderCell.setCellStyle(mCache.getRowHeaderStyle());
List<Row> metricsRows = getAdjustedList(scoutSheet);
rowIterator: for (Row averageRow : metricsRows) {
Cell averageCell = averageRow.getCell(averageRow.getLastCellNum() - 1);
Metric<?> keyMetric = mCache.getKeyMetric(averageRow.getCell(0));
if (TextUtils.isEmpty(getStringForCell(averageCell)) || keyMetric.getType() == TEXT) {
continue;
}
for (Cell keyCell : getAdjustedList(headerRow)) {
if (TextUtils.equals(keyMetric.getRef().getKey(), mCache.getMetricKey(keyCell))) {
setAverageFormula(scoutSheet, row.createCell(keyCell.getColumnIndex()), averageCell);
continue rowIterator;
}
}
Cell keyCell = headerRow.createCell(headerRow.getLastCellNum());
keyCell.setCellValue(averageRow.getCell(0).getStringCellValue());
keyCell.setCellStyle(mCache.getColumnHeaderStyle());
mCache.putKeyMetric(keyCell, keyMetric);
setAverageFormula(scoutSheet, row.createCell(keyCell.getColumnIndex()), averageCell);
}
}
buildAverageCharts(averageSheet);
}
Aggregations