Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取excel文件(.xlsx)并使用Java修改某些单元格的最佳方法是什么?_Java_Eclipse_Excel - Fatal编程技术网

读取excel文件(.xlsx)并使用Java修改某些单元格的最佳方法是什么?

读取excel文件(.xlsx)并使用Java修改某些单元格的最佳方法是什么?,java,eclipse,excel,Java,Eclipse,Excel,所以我需要读入一个excel文件(.xlsx),该文件大约有5200行和11列。我需要读取D列到M列,并找到所有包含8位数字(ID)的单元格 然后我需要用一个名称替换包含ID的单元格。我将通过运行一个简单的SQL查询来获取名称 然后,我想用更新的更改编写excel文件。 我使用了POI,但我对如何更新单元格感到困惑 我想我的问题是,在Java中实现这一点的最佳方式是什么? 任何意见都是有价值的 谢谢。步骤,使用Apache POI: try { FileInputStream file = n

所以我需要读入一个excel文件(.xlsx),该文件大约有5200行和11列。我需要读取D列到M列,并找到所有包含8位数字(ID)的单元格

然后我需要用一个名称替换包含ID的单元格。我将通过运行一个简单的SQL查询来获取名称

然后,我想用更新的更改编写excel文件。 我使用了POI,但我对如何更新单元格感到困惑

我想我的问题是,在Java中实现这一点的最佳方式是什么? 任何意见都是有价值的


谢谢。

步骤,使用Apache POI:

try {

FileInputStream file = new FileInputStream(new File("C:\\update.xls"));

HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
Cell cell = null;

//Update the value of cell
cell = sheet.getRow(1).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
cell = sheet.getRow(2).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);
cell = sheet.getRow(3).getCell(2);
cell.setCellValue(cell.getNumericCellValue() * 2);

file.close();

FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls"));
workbook.write(outFile);
outFile.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
  • 以输入模式打开excel文件(inputstream)

  • 使用POI API并阅读excel内容

  • 使用不同的setCellValue方法更新单元格的值

  • 关闭excel输入文件(inputstream)

  • 在输出模式下打开相同的excel文件(outputstream)

  • 在输出文件中写入已更新工作簿的内容

  • 关闭输出excel文件

  • 示例:

    try {
    
    FileInputStream file = new FileInputStream(new File("C:\\update.xls"));
    
    HSSFWorkbook workbook = new HSSFWorkbook(file);
    HSSFSheet sheet = workbook.getSheetAt(0);
    Cell cell = null;
    
    //Update the value of cell
    cell = sheet.getRow(1).getCell(2);
    cell.setCellValue(cell.getNumericCellValue() * 2);
    cell = sheet.getRow(2).getCell(2);
    cell.setCellValue(cell.getNumericCellValue() * 2);
    cell = sheet.getRow(3).getCell(2);
    cell.setCellValue(cell.getNumericCellValue() * 2);
    
    file.close();
    
    FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls"));
    workbook.write(outFile);
    outFile.close();
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    备选案文1:POI。选项2:读取为XML并使用XML库修改它。我倾向于选项1。你能将其保存为csv并进行处理吗?这可能是最简单的方法。@hankd是的,我可以将其保存为csv。我会试试这个,看看它是如何工作的。