https://issues.apache.org/bugzilla/show_bug.cgi?id=56574

--- Comment #3 from Ranganath <[email protected]> ---
Below is the entire code I used for above bug

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelFormulaDrag {
    public static Workbook workbook07;
    public static Map<String, Object[]> data;
    public void setData() {
        //This data needs to be written (Object[])
        data = new TreeMap<String, Object[]>();
        data.put("1", new Object[] {"ID", "NAME", "LASTNAME"});
        data.put("2", new Object[] {2, "Amit", "Shukla"});
        data.put("3", new Object[] {1, "Lokesh", "Gupta"});
        data.put("4", new Object[] {4, "John", "Adwards"});
        data.put("5", new Object[] {2, "Brian", "Schultz"});
    }

    public void format2007() throws IOException {
        XSSFSheet sheet = (XSSFSheet) workbook07.getSheet("Emp");
        if(sheet == null){
           sheet = (XSSFSheet) workbook07.createSheet("Emp");
        }
        Set<String> keyset = data.keySet();
        int rownum = 1;
        for (String key : keyset)
        {
            Row row = sheet.createRow(rownum++);
            this.writeToSheet(row, key);
        }
    }



    public void writeToSheet(Row row, String key){
            Object [] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr)
            {
                Cell cell = row.getCell(cellnum);
                if(cell == null){
                    cell = row.createCell(cellnum);
                } else {
                    if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                        if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                            cell.setCellFormula(null);
                            cell.getCellStyle().setDataFormat((short) 0);
                        }
                    }
                }
               if(obj instanceof String) {
                    cell.setCellValue((String)obj);
               } else if(obj instanceof Integer) {
                    cell.setCellValue((Integer)obj);
               }
                XSSFFormulaEvaluator.evaluateAllFormulaCells((XSSFWorkbook)
workbook07);
               
workbook07.getCreationHelper().createFormulaEvaluator().evaluateAll();
               cellnum++;
            }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            ExcelFormulaDrag excelFormulaDrag = new ExcelFormulaDrag();
            excelFormulaDrag.setData();
            workbook07 = new XSSFWorkbook(new
FileInputStream("D:\\testing7.xlsx"));
            excelFormulaDrag.format2007();
            FileOutputStream out = new FileOutputStream(new
File("D:\\testing4.xlsx"));
            workbook07.write(out);
            out.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}


In .xlsx file in cell A1 type '=TODAY()' and drag up to A20, and refer same to
above code and execute.

-- 
You are receiving this mail because:
You are the assignee for the bug.

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

Reply via email to