使用Apache POI获取/修改一个或多个Excel工作表中的单元格值

我有一个用Java编写的小应用程序,它使用Apache POI来读取/修改Excel文档中的值。 我使用工作表名称来引用单元格,就像工作表“Sheet1”中的单元格A1一样,我使用“Sheet1!A1”。

应用程序从命令行运行三个参数:文档名称,具有要replace的值的单元格,以及要从中获取输出的单元格。

示例:ReadExcel test.xls Sheet1!B2 = 10; Sheet1!B3 = 20 Sheet1!B7

上面的例子工作正常。

问题是当我想修改单元格或从另一个工作表获得输出。

示例:ReadExcel test.xls Sheet1!B2 = 10; Sheet1!B3 = 20 Sheet2!B2

我的代码如下:

package poitest; import java.util.List; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellReference; import org.apache.poi.ss.usermodel.*; public class ReadExcel { public static void main(String[] args) throws FileNotFoundException, IOException { // Will contain cell name / value pair for input cells Map<String, String> inputCellsMap = new HashMap<String, String>(); // Will contain cell name for output cells List<String> outputCells = new ArrayList<String>(); // Open the Excel file FileInputStream file = new FileInputStream(new File(args[0])); // Get the current workbook HSSFWorkbook workbook = new HSSFWorkbook(file); // Get the first sheet of the workbook HSSFSheet sheet = workbook.getSheetAt(0); // Get the input cells that need to be modified and // store their name and value in the inputCellsMap for (String element : args[1].split(";")) { inputCellsMap.put(element.split("=")[0], element.split("=")[1]); } // Get the output cells that will be accessed for resulting values for (String element : args[2].split(";")) { outputCells.add(element); } // Loop through the cells that need to be modified and // set the new value in the Excel document Iterator<Entry<String,String>> inputIterator = inputCellsMap.entrySet().iterator(); while (inputIterator.hasNext()) { Map.Entry<String,String> inputEntry = (Map.Entry<String,String>) inputIterator.next(); CellReference cellReferenceInput = new CellReference(inputEntry.getKey()); int cellReferenceInputRow = cellReferenceInput.getRow(); int cellReferenceInputColumn = cellReferenceInput.getCol(); Row rowInput = sheet.getRow(cellReferenceInputRow); if (rowInput == null) rowInput = sheet.createRow(cellReferenceInputRow); Cell cellInput = rowInput.getCell(cellReferenceInputColumn, Row.CREATE_NULL_AS_BLANK); cellInput.setCellValue(Integer.parseInt(inputEntry.getValue())); } // Apply all formulas after altering cell values HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook); // Get the results from the output cells for (int i = 0; i < outputCells.size(); i++) { CellReference cellReferenceOutput = new CellReference(outputCells.get(i)); int cellReferenceOutputRow = cellReferenceOutput.getRow(); int cellReferenceOutputColumn = cellReferenceOutput.getCol(); Row rowOutput = sheet.getRow(cellReferenceOutputRow); Cell cellOutput = rowOutput.getCell(cellReferenceOutputColumn, Row.CREATE_NULL_AS_BLANK); // Display results System.out.println(cellOutput.getNumericCellValue()); } workbook.close(); } } 

如果你看一下CellReference最长的构造CellReference ,你会注意到一个引用由5个属性组成:

  • stringsheetName (可以为null)
  • int行
  • int col
  • 布尔rowAbsolute
  • 布尔colAbsolute

您的命令行参数包括表单名称,但是您没有使用它。

首先,从代码中删除以下行: HSSFSheet sheet = workbook.getSheetAt(0);

相反,您需要在创buildCellReference之后使用getSheet(String)按名称查找表单:

 HSSFSheet sheet = workbook.getSheet(cellReferenceInput.getSheetName()); HSSFSheet sheet = workbook.getSheet(cellReferenceOutput.getSheetName());