Hi, I am using poi v 3.14. My program reads a spreadsheet (which has just 
two cells filled for the sake of this simple test) , fills rows and then 
needs to swap rows. It works fine when template is in "xls" format and so 
is the output. However, when template is saved as "xlsx", then resulting 
xlsx file turns out to be corrupted - Excel displays a message about 
unreadable contents, suggests, to recover and finally removes something 
from the spreadsheet. Any suggestions ?  Thanks

My simple test program that reproduces the problem is below.

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;


public class OpenTemplate {

        public static void main(String[] args) {
                
                Workbook wb;
                int indexRow = 10;
                int indexColumn = 3;
                
                try{
                        FileInputStream f = new FileInputStream
("/tmp/template.xlsx");
                        wb = WorkbookFactory.create(f);                 
                        CreationHelper createHelper = wb.getCreationHelper
();                     
                        Sheet sheet = wb.getSheetAt(0);                 
                        Row row = sheet.createRow(indexRow);
                        Cell cell = row.createCell(indexColumn);
                        cell.setCellValue(createHelper.createRichTextString
("FIRST"));                     
                        int newPosition = indexRow;                     
                        row = sheet.createRow(++indexRow);
                        cell = row.createCell(indexColumn);
                        cell.setCellValue(createHelper.createRichTextString
("SECOND"));
                        int currentRow = indexRow;                      
                        sheet.shiftRows(newPosition, currentRow, 1);
                        sheet.shiftRows(currentRow + 1, currentRow + 1, 
newPosition - currentRow - 1);

                        
                        FileOutputStream out = new FileOutputStream
("/tmp/spreadsheet.xlsx");                      
                        wb.write(out);
                        out.close();
                        
                }
                catch (Exception e) {
                        System.out.println("Exception: " + e.getMessage());
                        e.printStackTrace();
                }
        }
}


 


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

Reply via email to