Search in sources :

Example 1 with Workbook

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;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) FileInputStream(java.io.FileInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) JSONObject(org.json.JSONObject) PushbackInputStream(java.io.PushbackInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 2 with Workbook

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) View(org.springframework.web.servlet.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 3 with Workbook

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Row(org.apache.poi.ss.usermodel.Row) View(org.springframework.web.servlet.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 4 with Workbook

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IoUtilsKt.unhideFile(com.supercilex.robotscouter.util.IoUtilsKt.unhideFile) File(java.io.File) IoUtilsKt.hideFile(com.supercilex.robotscouter.util.IoUtilsKt.hideFile) PreferencesUtilsKt.setShouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint) PreferencesUtilsKt.shouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Nullable(android.support.annotation.Nullable)

Example 5 with Workbook

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);
}
Also used : Metric(com.supercilex.robotscouter.data.model.Metric) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) SpreadsheetUtils.getStringForCell(com.supercilex.robotscouter.data.client.spreadsheet.SpreadsheetUtils.getStringForCell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) PreferencesUtilsKt.setShouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.setShouldShowExportHint) PreferencesUtilsKt.shouldShowExportHint(com.supercilex.robotscouter.util.PreferencesUtilsKt.shouldShowExportHint) AddTrace(com.google.firebase.perf.metrics.AddTrace)

Aggregations

Workbook (org.apache.poi.ss.usermodel.Workbook)296 Sheet (org.apache.poi.ss.usermodel.Sheet)209 Test (org.junit.Test)187 Cell (org.apache.poi.ss.usermodel.Cell)139 Row (org.apache.poi.ss.usermodel.Row)121 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)95 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)90 FileOutputStream (java.io.FileOutputStream)33 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)30 CellStyle (org.apache.poi.ss.usermodel.CellStyle)25 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)24 File (java.io.File)22 IOException (java.io.IOException)22 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)17 InternalSheet (org.apache.poi.hssf.model.InternalSheet)15 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)15 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)15 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)14