You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by "Frehmel, Sebastian Rule" <se...@student.kit.edu> on 2010/03/25 10:22:42 UTC

Reading as XLS, writing as XLSX

Good morning poi-list!

I am reading an XLS file which will be modified (using POI 3.6).
How  can I write it back to the file system as an XLSX file?
The thought of it seems easy, yet, I can't find a possibility to do so...

I read the file as follows:
File inFile = new File("template.xls");
InputStream bIn = new BufferedInputStream(new FileInputStream(inFile));
Workbook wb = WorkbookFactory.create(bIn);

[...do something...]

I write as follows:
File outFile = "out.xlsx";
BufferedOutputStream bOut = new BufferedOutputStream(new FileOutputStream(outFile));
wb.write(bOut);

Excel then fails loading the document stating that it encountered an unexpected file format (The XLSX is probably simply an XLS file)

Thank you!
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Reading as XLS, writing as XLSX

Posted by Pablo Dejuan Calzolari <pa...@gmail.com>.
Hi,  I had a similar problem some time ago, I did a small program to convert
data from one XLSX to XLS, hope the idea helps:

public class ExcelConvertHelper {
    public static void convertFromXLSXtoXLS(String XSLX, String XLS){
            InputStream inp;
            try {
                inp = new FileInputStream(XSLX);
                Workbook wb = WorkbookFactory.create(inp);
                OutputStream out = new FileOutputStream(XLS);
                HSSFWorkbook newWb = new HSSFWorkbook();
                Sheet copia = newWb.createSheet();
                Sheet sheet = wb.getSheetAt(0);
                Iterator<Row> rows = sheet.iterator();
                while(rows.hasNext()){
                    Row row = rows.next();
                    Row newRow = copia.createRow(row.getRowNum());
                    Iterator<Cell> cells = row.cellIterator();
                    while( cells.hasNext()){
                        Cell cell = cells.next();
                        Cell newCell =
newRow.createCell(cell.getColumnIndex());
                        int type = cell.getCellType();
                        switch(type){
                        case Cell.CELL_TYPE_BLANK:
                            break;
                        case Cell.CELL_TYPE_NUMERIC:

//System.out.print(cell.getNumericCellValue());

newCell.setCellValue(cell.getNumericCellValue());
                            break;
                        case Cell.CELL_TYPE_STRING:
                            //System.out.print(cell.getStringCellValue() + "
");
                            newCell.setCellValue(cell.getStringCellValue());
                            break;
                        case Cell.CELL_TYPE_ERROR:

newCell.setCellErrorValue(cell.getErrorCellValue());
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            newCell.setCellValue( cell.getBooleanCellValue()
);
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            //System.out.print(cell.getCellFormula());

                            newCell.setCellFormula(cell.getCellFormula());
                            break;
                        }
                    }
                    System.out.println();
                }
                newWb.write(out);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}


Frehmel, Sebastian Rule wrote:
>
> Good morning poi-list!
>
> I am reading an XLS file which will be modified (using POI 3.6).
> How  can I write it back to the file system as an XLSX file?
> The thought of it seems easy, yet, I can't find a possibility to do so...
>
> I read the file as follows:
> File inFile = new File("template.xls");
> InputStream bIn = new BufferedInputStream(new FileInputStream(inFile));
> Workbook wb = WorkbookFactory.create(bIn);
>
> [...do something...]
>
> I write as follows:
> File outFile = "out.xlsx";
> BufferedOutputStream bOut = new BufferedOutputStream(new
> FileOutputStream(outFile));
> wb.write(bOut);
>
> Excel then fails loading the document stating that it encountered an
> unexpected file format (The XLSX is probably simply an XLS file)
>
> Thank you!
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>
>

--
View this message in context:
http://old.nabble.com/Reading-as-XLS%2C-writing-as-XLSX-tp28026334p28031144.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org

Re: Reading as XLS, writing as XLSX

Posted by MSB <ma...@tiscali.co.uk>.
Sorry to say that there is no easy, in-built way to use POI to convert from
one file format to the other. You could however write the code to perform
the conversion; you would need to recover all of the details about each
element of the (HSSF) binary workbook - just for the sake of discussion -
and then create elements in the (XSSF) xml based workbook to mirror them.

If you simply want to perform file conversion, there are other options
including OLE/COM if you are running on a Windows platform, OpenOffice if
you are converting from xml to binary format as I do not think that OO yet
supports writing the Microsoft xml file formats and, finally, there are
on-line conversion 'programs' that you could use but these all carry the
risk of uploading your files to the internet!

Yours

Mark B


Frehmel, Sebastian Rule wrote:
> 
> Good morning poi-list!
> 
> I am reading an XLS file which will be modified (using POI 3.6).
> How  can I write it back to the file system as an XLSX file?
> The thought of it seems easy, yet, I can't find a possibility to do so...
> 
> I read the file as follows:
> File inFile = new File("template.xls");
> InputStream bIn = new BufferedInputStream(new FileInputStream(inFile));
> Workbook wb = WorkbookFactory.create(bIn);
> 
> [...do something...]
> 
> I write as follows:
> File outFile = "out.xlsx";
> BufferedOutputStream bOut = new BufferedOutputStream(new
> FileOutputStream(outFile));
> wb.write(bOut);
> 
> Excel then fails loading the document stating that it encountered an
> unexpected file format (The XLSX is probably simply an XLS file)
> 
> Thank you!
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Reading-as-XLS%2C-writing-as-XLSX-tp28026334p28031144.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org