如何替换单元格值 - Apache POI

时间:2017-07-31 17:38:18

标签: java 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++;
        }

    }

&#39;类型&#39;我试图复制的价值

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)

感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:2)

问题是getCell()返回Cell类型的值。您实际上并没有检索该单元格的值,而是检索单元格对象本身。为了使用setCellValue设置值,您需要为其提供一个值,即日期,布尔值,字符串,richtextstring等,这是apache POI文档中listed here的方法之一。细胞