使用 Apache POI 在一个或多个 Excel 工作表中 get/modify 单元格值
Using Apache POI to get/modify cell values in one or more Excel sheets
我有一个用 Java 编写的小应用程序,它使用 Apache POI 来 Excel 文档中的 read/modify 值。我使用 sheet 名称引用单元格,例如 sheet "Sheet1" 中的单元格 A1,我使用 "Sheet1!A1"。
该应用程序使用三个参数从命令行运行:文档名称、具有我要替换的值的单元格、我想要从中获取输出的单元格。
示例:阅读Excel test.xls Sheet1!B2=10;Sheet1!B3=20 Sheet1!B7
上面的例子没问题。
问题是当我想修改单元格或从另一个 sheet 获取输出时。
示例:阅读Excel 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
的最长构造函数,您会注意到引用由 5 个属性组成:
- 字符串sheet名称(可以为空)
- 整数行
- 整数列
- 布尔行绝对值
- 布尔colAbsolute
您的命令行参数包括 sheet 名称,但您没有使用它。
首先,从您的代码中删除以下行:HSSFSheet sheet = workbook.getSheetAt(0);
相反,您需要在创建 CellReference
:
后立即使用 getSheet(String)
按名称查找 sheet
HSSFSheet sheet = workbook.getSheet(cellReferenceInput.getSheetName());
HSSFSheet sheet = workbook.getSheet(cellReferenceOutput.getSheetName());
我有一个用 Java 编写的小应用程序,它使用 Apache POI 来 Excel 文档中的 read/modify 值。我使用 sheet 名称引用单元格,例如 sheet "Sheet1" 中的单元格 A1,我使用 "Sheet1!A1"。
该应用程序使用三个参数从命令行运行:文档名称、具有我要替换的值的单元格、我想要从中获取输出的单元格。
示例:阅读Excel test.xls Sheet1!B2=10;Sheet1!B3=20 Sheet1!B7
上面的例子没问题。
问题是当我想修改单元格或从另一个 sheet 获取输出时。
示例:阅读Excel 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
的最长构造函数,您会注意到引用由 5 个属性组成:
- 字符串sheet名称(可以为空)
- 整数行
- 整数列
- 布尔行绝对值
- 布尔colAbsolute
您的命令行参数包括 sheet 名称,但您没有使用它。
首先,从您的代码中删除以下行:HSSFSheet sheet = workbook.getSheetAt(0);
相反,您需要在创建 CellReference
:
getSheet(String)
按名称查找 sheet
HSSFSheet sheet = workbook.getSheet(cellReferenceInput.getSheetName());
HSSFSheet sheet = workbook.getSheet(cellReferenceOutput.getSheetName());