2017-07-31 113 views
0

我想取代使用现有的单元格值从其他工作表单元格值(在同一工作簿)如何更换一个单元格的值 - Apache的POI

我的代码:

public static void update_sheet(XSSFWorkbook w) 
    { 
    XSSFSheet sheet,sheet_overview; 
    sheet_overview = w.getSheetAt(0); 
    int lastRowNum,latest_partition_date; 
    latest_partition_date = 3; 
    XSSFRow row_old, row_new; 
    XSSFCell cell_old, cell_new; 


    for(int i=1;i<=10;i++) 
     { 
      sheet = w.getSheetAt(i); 

      lastRowNum = sheet.getLastRowNum(); 

      row_old = sheet.getRow(lastRowNum); 
      cell_old = row_old.getCell(0); //getting cell value from a sheet 

      row_new = sheet_overview.getRow(latest_partition_date); 
      cell_new = row_new.getCell(5); 
      ***cell_new.setCellValue(cell_old)***;//trying to overwrite cellvalue 

      latest_partition_date++; 
     } 

    } 

的'类型' 的值,我试图复制

7/10/2017 
7/11/2017 
7/12/2017 
7/13/2017 
2017-07-14 
2017-07-15 

错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method setCellValue(boolean) in the type XSSFCell is not applicable for the arguments (XSSFCell) 

at Sample2.update_overview_sheet(Sample2.java:78) 
at Sample2.main(Sample2.java:26) 

任何帮助或建议表示赞赏。

+0

你可以看看帖子https://stackoverflow.com/questions/24574490/changing-value-of-cell-in-excel-via-apache-poi – Akash

回答

2

问题是getCell()返回Cell类型的值。你实际上并没有检索那个单元的值,而是单元对象本身。为了设置setCellValue的值,你需要为它提供一个值,这是一个日期,布尔值,字符串,富文本字符串等,这是listed here在Cell的apache POI文档中的一个方法。

+0

嘿蒂姆,我试过这个** s = row_old。 getCell(0)的ToString(); cell_new = sheet_overview.getRow(2).getCell(5); \t \t \t \t cell_new.setCellValue(s); \t \t \t \t latest_partition_date ++; **我得到空指针异常 – Chid

+0

这需要一个单元对象并将其转换为字符串。您应该使用[getStringValue()](https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/CellValue.html#getStringValue())方法。 –

+0

感谢您的帮助添 – Chid

相关问题