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: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>

--
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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to