【问题标题】:Set new cell value by cell name in Apache POI在 Apache POI 中按单元格名称设置新单元格值
【发布时间】:2015-09-17 18:23:02
【问题描述】:

我是 Java 和 Apache POI 的新手,但我得到了这个任务,我必须从命令行读取 Excel 文件的名称以及需要修改的列的名称和值。

所以最终应用程序将像这样运行: java test -f test.xls -i B2=10;B3=20;B4=30

我创建了一个包含单元格名称及其值的地图,但我不知道如何使用此地图按名称访问单元格(例如:B2)并设置新值(例如。 : 10)。

到目前为止,我的代码如下:

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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 and output cells
        Map<String, String> inputCellsMap = new HashMap<String, String>();
        Map<String, String> outputCellsMap = new HashMap<String, String>();

        // Open the Excel file
        FileInputStream file = new FileInputStream(new File(args[1]));

        // 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[3].split(";")) {
            inputCellsMap.put(element.split("=")[0], element.split("=")[1]);
        }

        // Loop through the cells that need to be modified and 
        // set the new value in the Excel document
        Iterator<Entry<String,String>> iterator = inputCellsMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String,String> entry = (Map.Entry<String,String>) iterator.next();

            // TODO
            // Get cells by name and set their new value

            //System.out.println("Key : " + entry.getKey() + " Value :" + entry.getValue());
        }           

        workbook.close();       
    }
}

【问题讨论】:

  • 我想到的第一个想法是将 Map 值设置为 Cell 而不是 String。

标签: java excel apache-poi


【解决方案1】:

获得条目后,获取单元格引用文本,例如"B2",以及值。

使用文本创建CellReference。然后你可以获取从0开始的行和列索引,你可以使用它们来获取Row,然后是Cell对象,你可以在该对象上调用setCellValue

CellReference cr = new CellReference(entry.getKey());
int r = cr.getRow();
int c = cr.getCol();

Row row = sheet.getRow(r);
if (row == null)
    row = sheet.createRow(r);
Cell cell = row.getCell(c, Row.CREATE_NULL_AS_BLANK);
cell.setCellValue(entry.getValue());

【讨论】:

  • 感谢您的回答。我收到一条错误消息,提示“CREATE_NULL_AS_BLANK 无法解析或不是字段”。
  • 还有一件事。似乎它设置了值,但是当我稍后尝试获取它时,我不能。我认为它应该设置为数字。有可能吗?
  • 将值解析为int(或其他数字基元类型),然后调用Cell的重载setCellValue(int)
【解决方案2】:

您可以使用这些方法访问行和列。 (例如 E101)

sheet.getRow(100).getCell(5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多