2015-06-02 348 views
0

在我的項目中,我使用下面的代碼添加了在Spring項目中使用Apache Poi庫生成Excel文件的功能。setCellValue已棄用

public class ExcelView extends AbstractExcelView { 
    @Override 
    protected void buildExcelDocument(Map model, HSSFWorkbook workbook, 
     HttpServletRequest request, HttpServletResponse response) 
     throws Exception { 

     List<Employee> employees = (List) model.get("employees"); 
     HSSFSheet sheet = workbook.createSheet("Employee Report"); 

     HSSFRow header = sheet.createRow(0); 
     header.createCell(0).setCellValue("Employee Id"); 
     header.createCell(1).setCellValue("First Name"); 
     header.createCell(2).setCellValue("Last Name"); 
     header.createCell(3).setCellValue("Salary"); 

     int counter = 1; 
     for (Employee e : employees) { 
     HSSFRow row = sheet.createRow(counter++); 
     row.createCell(0).setCellValue(e.getEmployeeId()); 
     row.createCell(1).setCellValue(e.getFirstName()); 
     row.createCell(2).setCellValue(e.getLastName()); 
     row.createCell(3).setCellValue(e.getSalary()); 
     } 
    } 
} 

我的問題是row.createCell(0).setCellValue();已被棄用。什麼是替代這種方法?

+1

我想你正在調用'createCell(short columnIndex)'這是棄用,而不是你需要調用'createCell(int column)'。 – Prashant

+0

您是否嘗試升級到最新版本的Apache POI,目前是3.12? – Gagravarr

回答

0

使用setCellValue(new HSSFRichTextString("your string"));代替

或升級poi.jar到最新版本。

+1

我不認爲'setCellValue(String arg)'已被棄用! – Prashant

+0

謝謝@霍華德,它刪除了我的警告信息。我使用了最新的poi-3.4最終jar – Madhesh

+0

我認爲POI javadoc和代碼不同步。官方javadoc不棄用setCellValue(String) –