Java 使用Apache POI获取/修改一个或多个Excel工作表中的单元格值
我有一个用Java编写的小应用程序,它使用ApachePOI读取/修改Excel文档中的值。我使用工作表名称引用单元格,就像工作表“Sheet1”中的单元格A1一样,我使用“Sheet1!A1” 应用程序从命令行运行,并带有三个参数:文档名、包含要替换的值的单元格以及要从中获取输出的单元格 示例:ReadExcel test.xls Sheet1!B2=10;第一张!B3=20张纸1!B7 上面的例子很好用 问题是当我想修改单元格或从其他工作表获取输出时 示例:ReadExcel test.xls Sheet1!B2=10;第一张!B3=20张纸2!B2 我的代码如下:Java 使用Apache POI获取/修改一个或多个Excel工作表中的单元格值,java,excel,apache-poi,Java,Excel,Apache Poi,我有一个用Java编写的小应用程序,它使用ApachePOI读取/修改Excel文档中的值。我使用工作表名称引用单元格,就像工作表“Sheet1”中的单元格A1一样,我使用“Sheet1!A1” 应用程序从命令行运行,并带有三个参数:文档名、包含要替换的值的单元格以及要从中获取输出的单元格 示例:ReadExcel test.xls Sheet1!B2=10;第一张!B3=20张纸1!B7 上面的例子很好用 问题是当我想修改单元格或从其他工作表获取输出时 示例:ReadExcel test.xl
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();
}
}
封装测试;
导入java.util.List;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Iterator;
导入java.util.Map;
导入java.util.Map.Entry;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.hssformulaevaluator;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hssf.util.CellReference;
导入org.apache.poi.ss.usermodel.*;
公共类ReadExcel{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
//将包含输入单元格的单元格名称/值对
Map inputCellsMap=新的HashMap();
//将包含输出单元格的单元格名称
List outputCells=new ArrayList();
//打开Excel文件
FileInputStream文件=新FileInputStream(新文件(args[0]);
//获取当前工作簿
HSSF工作簿=新的HSSF工作簿(文件);
//获取工作簿的第一页
HSSFSheet sheet=工作簿。getSheetAt(0);
//获取需要修改的输入单元格并
//将其名称和值存储在inputCellsMap中
对于(字符串元素:args[1]。拆分(“;”){
inputCellsMap.put(element.split(“”[0],element.split(“”[1]);
}
//获取将被访问以获取结果值的输出单元格
对于(字符串元素:args[2]。拆分(“;”){
添加(元素);
}
//循环遍历需要修改的单元格,然后
//在Excel文档中设置新值
迭代器inputIterator=inputCellsMap.entrySet().Iterator();
while(inputIterator.hasNext()){
Map.Entry inpuntery=(Map.Entry)inputIterator.next();
CellReference cellReferenceInput=新的CellReference(inpuntery.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\u NULL\u作为\u BLANK);
setCellValue(Integer.parseInt(inpuntery.getValue());
}
//更改单元格值后应用所有公式
hssformulaevaluator.evaluateAllFormulaCells(工作簿);
//从输出单元格中获取结果
对于(int i=0;i
如果查看的是最长的构造函数,您会注意到引用由5个属性组成:
- 字符串sheetName(可以为空)
- 整数行
- 整数列
- 布尔行绝对
- 布尔绝对
HSSFSheet sheet=workbook.getSheetAt(0)代码>
相反,您需要在创建CellReference
之后立即使用按名称查找工作表:
HSSFSheet sheet = workbook.getSheet(cellReferenceInput.getSheetName());
HSSFSheet sheet = workbook.getSheet(cellReferenceOutput.getSheetName());
是的,明白了。非常感谢。