Search in sources :

Example 6 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class TestSXSSFWorkbookWithCustomZipEntrySource method customZipEntrySourceForWriteAndRead.

// write an encrypted workbook to disk, and encrypt any temporary files as well
@Test
public void customZipEntrySourceForWriteAndRead() throws IOException, GeneralSecurityException, InvalidFormatException {
    SXSSFWorkbookWithCustomZipEntrySource workbook = new SXSSFWorkbookWithCustomZipEntrySource();
    SXSSFSheet sheet1 = workbook.createSheet(sheetName);
    SXSSFRow row1 = sheet1.createRow(1);
    SXSSFCell cell1 = row1.createCell(1);
    cell1.setCellValue(cellValue);
    EncryptedTempData tempData = new EncryptedTempData();
    workbook.write(tempData.getOutputStream());
    workbook.close();
    workbook.dispose();
    ZipEntrySource zipEntrySource = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream());
    tempData.dispose();
    OPCPackage opc = OPCPackage.open(zipEntrySource);
    XSSFWorkbook xwb = new XSSFWorkbook(opc);
    zipEntrySource.close();
    XSSFSheet xs1 = xwb.getSheetAt(0);
    assertEquals(sheetName, xs1.getSheetName());
    XSSFRow xr1 = xs1.getRow(1);
    XSSFCell xc1 = xr1.getCell(1);
    assertEquals(cellValue, xc1.getStringCellValue());
    xwb.close();
    opc.close();
}
Also used : EncryptedTempData(org.apache.poi.poifs.crypt.temp.EncryptedTempData) SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) ZipEntrySource(org.apache.poi.openxml4j.util.ZipEntrySource) AesZipFileZipEntrySource(org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource) SXSSFWorkbookWithCustomZipEntrySource(org.apache.poi.poifs.crypt.temp.SXSSFWorkbookWithCustomZipEntrySource) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 7 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class PieChartDemo method main.

public static void main(String[] args) throws Exception {
    if (args.length < 2) {
        usage();
        return;
    }
    BufferedReader modelReader = new BufferedReader(new FileReader(args[1]));
    XMLSlideShow pptx = null;
    try {
        // first line is chart title
        String chartTitle = modelReader.readLine();
        pptx = new XMLSlideShow(new FileInputStream(args[0]));
        XSLFSlide slide = pptx.getSlides().get(0);
        // find chart in the slide
        XSLFChart chart = null;
        for (POIXMLDocumentPart part : slide.getRelations()) {
            if (part instanceof XSLFChart) {
                chart = (XSLFChart) part;
                break;
            }
        }
        if (chart == null)
            throw new IllegalStateException("chart not found in the template");
        // embedded Excel workbook that holds the chart data
        POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
        XSSFWorkbook wb = new XSSFWorkbook();
        try {
            XSSFSheet sheet = wb.createSheet();
            CTChart ctChart = chart.getCTChart();
            CTPlotArea plotArea = ctChart.getPlotArea();
            CTPieChart pieChart = plotArea.getPieChartArray(0);
            //Pie Chart Series
            CTPieSer ser = pieChart.getSerArray(0);
            // Series Text
            CTSerTx tx = ser.getTx();
            tx.getStrRef().getStrCache().getPtArray(0).setV(chartTitle);
            sheet.createRow(0).createCell(1).setCellValue(chartTitle);
            String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
            tx.getStrRef().setF(titleRef);
            // Category Axis Data
            CTAxDataSource cat = ser.getCat();
            CTStrData strData = cat.getStrRef().getStrCache();
            // Values
            CTNumDataSource val = ser.getVal();
            CTNumData numData = val.getNumRef().getNumCache();
            // unset old axis text
            strData.setPtArray(null);
            // unset old values
            numData.setPtArray(null);
            // set model
            int idx = 0;
            int rownum = 1;
            String ln;
            while ((ln = modelReader.readLine()) != null) {
                String[] vals = ln.split("\\s+");
                CTNumVal numVal = numData.addNewPt();
                numVal.setIdx(idx);
                numVal.setV(vals[1]);
                CTStrVal sVal = strData.addNewPt();
                sVal.setIdx(idx);
                sVal.setV(vals[0]);
                idx++;
                XSSFRow row = sheet.createRow(rownum++);
                row.createCell(0).setCellValue(vals[0]);
                row.createCell(1).setCellValue(Double.valueOf(vals[1]));
            }
            numData.getPtCount().setVal(idx);
            strData.getPtCount().setVal(idx);
            String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
            val.getNumRef().setF(numDataRange);
            String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
            cat.getStrRef().setF(axisDataRange);
            // updated the embedded workbook with the data
            OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream();
            try {
                wb.write(xlsOut);
            } finally {
                xlsOut.close();
            }
            // save the result
            OutputStream out = new FileOutputStream("pie-chart-demo-output.pptx");
            try {
                pptx.write(out);
            } finally {
                out.close();
            }
        } finally {
            wb.close();
        }
    } finally {
        if (pptx != null)
            pptx.close();
        modelReader.close();
    }
}
Also used : CTStrData(org.openxmlformats.schemas.drawingml.x2006.chart.CTStrData) CTNumDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CTPlotArea(org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea) CTSerTx(org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx) CellReference(org.apache.poi.ss.util.CellReference) CTAxDataSource(org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) CTChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTChart) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) FileReader(java.io.FileReader) CTNumData(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumData) FileInputStream(java.io.FileInputStream) CTPieChart(org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart) CTPieSer(org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer) CTStrVal(org.openxmlformats.schemas.drawingml.x2006.chart.CTStrVal) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) CTNumVal(org.openxmlformats.schemas.drawingml.x2006.chart.CTNumVal) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 8 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class XSSFRowShifter method updateSheetFormulas.

private void updateSheetFormulas(Sheet sh, FormulaShifter shifter) {
    for (Row r : sh) {
        XSSFRow row = (XSSFRow) r;
        updateRowFormulas(row, shifter);
    }
}
Also used : XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) Row(org.apache.poi.ss.usermodel.Row)

Example 9 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class TestXSSFImportFromXML method testImportFromXML.

@Test
public void testImportFromXML() throws IOException, XPathExpressionException, SAXException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
    try {
        String name = "name";
        String teacher = "teacher";
        String tutor = "tutor";
        String cdl = "cdl";
        String duration = "duration";
        String topic = "topic";
        String project = "project";
        String credits = "credits";
        String testXML = "<CORSO>" + "<NOME>" + name + "</NOME>" + "<DOCENTE>" + teacher + "</DOCENTE>" + "<TUTOR>" + tutor + "</TUTOR>" + "<CDL>" + cdl + "</CDL>" + "<DURATA>" + duration + "</DURATA>" + "<ARGOMENTO>" + topic + "</ARGOMENTO>" + "<PROGETTO>" + project + "</PROGETTO>" + "<CREDITI>" + credits + "</CREDITI>" + "</CORSO>";
        XSSFMap map = wb.getMapInfo().getXSSFMapByName("CORSO_mapping");
        assertNotNull(map);
        XSSFImportFromXML importer = new XSSFImportFromXML(map);
        importer.importFromXML(testXML);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row = sheet.getRow(0);
        assertTrue(row.getCell(0).getStringCellValue().equals(name));
        assertTrue(row.getCell(1).getStringCellValue().equals(teacher));
        assertTrue(row.getCell(2).getStringCellValue().equals(tutor));
        assertTrue(row.getCell(3).getStringCellValue().equals(cdl));
        assertTrue(row.getCell(4).getStringCellValue().equals(duration));
        assertTrue(row.getCell(5).getStringCellValue().equals(topic));
        assertTrue(row.getCell(6).getStringCellValue().equals(project));
        assertTrue(row.getCell(7).getStringCellValue().equals(credits));
    } finally {
        wb.close();
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Example 10 with XSSFRow

use of org.apache.poi.xssf.usermodel.XSSFRow in project poi by apache.

the class TestXSSFImportFromXML method testOptionalFields_Bugzilla_57890.

@Test
public void testOptionalFields_Bugzilla_57890() throws IOException, ParseException, XPathExpressionException, SAXException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57890.xlsx");
    String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<TestInfoRoot>" + "<TestData>" + "<Int>" + Integer.MIN_VALUE + "</Int>" + "<UnsignedInt>12345</UnsignedInt>" + "<double>1.0000123</double>" + "<Date>1991-03-14</Date>" + "</TestData>" + "</TestInfoRoot>";
    XSSFMap map = wb.getMapInfo().getXSSFMapByName("TestInfoRoot_Map");
    assertNotNull(map);
    XSSFImportFromXML importer = new XSSFImportFromXML(map);
    importer.importFromXML(testXML);
    XSSFSheet sheet = wb.getSheetAt(0);
    XSSFRow rowHeadings = sheet.getRow(0);
    XSSFRow rowData = sheet.getRow(1);
    assertEquals("Date", rowHeadings.getCell(0).getStringCellValue());
    Date date = new SimpleDateFormat("yyyy-MM-dd", DateFormatSymbols.getInstance(Locale.ROOT)).parse("1991-3-14");
    assertEquals(date, rowData.getCell(0).getDateCellValue());
    assertEquals("Amount Int", rowHeadings.getCell(1).getStringCellValue());
    assertEquals(new Double(Integer.MIN_VALUE), rowData.getCell(1).getNumericCellValue(), 0);
    assertEquals("Amount Double", rowHeadings.getCell(2).getStringCellValue());
    assertEquals(1.0000123, rowData.getCell(2).getNumericCellValue(), 0);
    assertEquals("Amount UnsignedInt", rowHeadings.getCell(3).getStringCellValue());
    assertEquals(new Double(12345), rowData.getCell(3).getNumericCellValue(), 0);
    wb.close();
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Aggregations

XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)59 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)39 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)39 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)35 FileOutputStream (java.io.FileOutputStream)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 FileInputStream (java.io.FileInputStream)14 BufferedReader (java.io.BufferedReader)10 FileNotFoundException (java.io.FileNotFoundException)10 Test (org.junit.Test)10 InputStreamReader (java.io.InputStreamReader)9 Reader (java.io.Reader)9 File (java.io.File)8 HashMap (java.util.HashMap)8 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)8 BufferedWriter (java.io.BufferedWriter)6 FileWriter (java.io.FileWriter)6 HashSet (java.util.HashSet)5 Map (java.util.Map)5