use of org.apache.poi.ss.usermodel.Sheet 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.Sheet 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.Sheet 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.Sheet 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);
}
use of org.apache.poi.ss.usermodel.Sheet in project textdb by TextDB.
the class ExcelSink method open.
@Override
public void open() throws TextDBException {
if (cursor != CLOSED) {
return;
}
inputOperator.open();
inputSchema = inputOperator.getOutputSchema();
outputSchema = new Schema(inputSchema.getAttributes().stream().filter(attr -> !attr.getAttributeName().equalsIgnoreCase(SchemaConstants._ID)).filter(attr -> !attr.getAttributeName().equalsIgnoreCase(SchemaConstants.PAYLOAD)).filter(attr -> !attr.getAttributeType().equals(AttributeType.LIST)).toArray(Attribute[]::new));
wb = new XSSFWorkbook();
DateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
fileName = df.format(new Date()) + ".xlsx";
try {
if (Files.notExists(Paths.get(excelIndexDirectory))) {
Files.createDirectories(Paths.get(excelIndexDirectory));
}
fileOut = new FileOutputStream(Paths.get(excelIndexDirectory, fileName).toString());
} catch (IOException e) {
throw new DataFlowException(e);
}
sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(0);
List<String> attributeNames = outputSchema.getAttributeNames();
for (int i = 0; i < attributeNames.size(); i++) {
String attributeName = attributeNames.get(i);
row.createCell(i).setCellValue(attributeName);
}
cursor = OPENED;
}
Aggregations