Forgot to mention that I'm on POI 3.7.

Thanks.

On 12/5/2010 10:38 PM, Aram Mirzadeh wrote:

Hi,

I'm reading in a docx (2007 xml based [actually it's from word 2010]) file which has a Excel chart embedded in it (suprisingly it isn't xlsx, it's binary xls file format).

I'm trying to update the values for the excel chart. I can read the docx, get the embeded object, and read and modify the cells that I need. My question is how do you save the embedded excel file? The way I normally do it is to do a Stream out to a file, however in this case that just generates a new file and does not update the embedded file.

Here is what I have so far:

final FileInputStream fis = new FileInputStream(new File("c:\\test.docx"));
        final XWPFDocument doc = new XWPFDocument(fis);

        for (final PackagePart pPart : doc.getAllEmbedds()) {
            final String contentType = pPart.getContentType();
            System.out.println(contentType + "\n");
            if (contentType.equals("application/vnd.ms-excel")) {
final HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());

for (int sheet = 0 ; sheet < embeddedWorkbook.getNumberOfSheets(); sheet++) { final HSSFSheet activeSheet = embeddedWorkbook.getSheetAt(sheet); if (activeSheet.getSheetName().equalsIgnoreCase("Sheet1")) { for (int rowIndex = activeSheet.getFirstRowNum() ; rowIndex <= activeSheet.getLastRowNum(); rowIndex++) { final HSSFRow row = activeSheet.getRow(rowIndex); for (int cellIndex = row.getFirstCellNum() ; cellIndex <= row.getLastCellNum(); cellIndex++) { final HSSFCell cell = row.getCell(cellIndex);
                                if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) System.out.println("Row:" + rowIndex + " Cell:" + cellIndex + " = " + cell.getStringCellValue()); if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { System.out.println("Row:" + rowIndex + " Cell:" + cellIndex + " = " + cell.getNumericCellValue()); cell.setCellValue(cell.getNumericCellValue() * 2); // update the value
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to