Java 如何使用ApachePOI为相邻列设置单元格值?

Java 如何使用ApachePOI为相邻列设置单元格值?,java,apache-poi,Java,Apache Poi,我的目标是迭代一个已有的Excel电子表格,其中包含两列。一个称为制造商,另一个称为DNC或不联系 我想循环浏览我拥有的制造商列表,用红色标记那些不应该联系的制造商,并在制造商名单旁边的空白栏中注明为什么不能联系他们。我在下面附上了我的代码 我将每个制造商对象的字段存储在一个名为“mu”的链接列表中,它们是“name”和“DNC_Reason” Iterator roweiterator2=电子表格.Iterator(); while(rowIterator2.hasNext()){ 行row2

我的目标是迭代一个已有的Excel电子表格,其中包含两列。一个称为制造商,另一个称为DNC或不联系

我想循环浏览我拥有的制造商列表,用红色标记那些不应该联系的制造商,并在制造商名单旁边的空白栏中注明为什么不能联系他们。我在下面附上了我的代码

我将每个制造商对象的字段存储在一个名为“mu”的链接列表中,它们是“name”和“DNC_Reason”

Iterator roweiterator2=电子表格.Iterator();
while(rowIterator2.hasNext()){
行row2=rowIterator2.next();
单元格DNC_Reason=row2.getCell(1);
if(row2.getCell(1)==null){
row2.createCell(1);
}
迭代器cellIterator2=row2.cellIterator();
while(cellIterator2.hasNext()){
Cell Cell=cellIterator2.next();
Pattern p=Pattern.compile(“[\.$,[124; |]\\ s |-][124;]\\ b(LLC | Company | Incorporated | Co | Manufacturer | The | Limited | Ltd | Inc)\\b”,Pattern;
Matcher m=p.Matcher(cell.getStringCellValue());
字符串s=m.replaceAll(“”);
用于(制造商mu:mfgs){
如果(cell.getColumnIndex()==0&&mu.getName().equals)){
cell.setCellStyle(style);
DNC_Reason.setCellValue(mu.getDNCReason());
}
}
}
}
  • 您应该为您的
    创建一个地图,不与制造商联系
    ,名称为键,制造商为值。然后,您可以使用
    containsKey
    ,而不是在
    mfgs列表中不断迭代
  • 您应该使用
    rowditerator
    的结果对行进行迭代。您不需要另一个迭代器
  • 局部变量不应以大写字母开头(
    DNC\U Reason
    -更好的名称应该是
    dncReasonCell
  • 示例代码假设制造商单元格已填充(
    getStringValue()
    如果未给出正确的值,可能会导致
    NullPointerException
    ),则初始化样式变量,并且您有一个不可联系的制造商映射:

    Iterator<Row> rowIterator = spreadsheet.rowIterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Cell dncReasonCell = row.getCell(1);
        if (dncReasonCell == null) {
            dncReasonCell = row.createCell(1, CellType.STRING);
        }
        Cell manufacturerCell = row.getCell(0);
        String manufacturerNameForDncTest = Pattern
                .compile("[\\.$|,|;|'|\\s|-]|\\b(LLC|Company|Incorporated|Co|Manufacturer|The|Limited|Ltd|Inc)\\b", Pattern.CASE_INSENSITIVE)
                .matcher(manufacturerCell.getStringCellValue()).replaceAll("");
        if (notToBeContactedManufacturers.containsKey(manufacturerNameForDncTest)) {
            manufacturerCell.setCellStyle(style);
            dncReasonCell.setCellValue(notToBeContactedManufacturers.get(manufacturerNameForDncTest).getDNCReason());
        } else {
            dncReasonCell.setCellValue("");
        }
    }
    
    Iterator rowditerator=电子表格.rowditerator();
    while(roweiterator.hasNext()){
    行=行迭代器。下一步();
    Cell dncReasonCell=row.getCell(1);
    if(dncReasonCell==null){
    dncReasonCell=row.createCell(1,CellType.STRING);
    }
    Cell-manufacturerCell=row.getCell(0);
    字符串制造商rnamefordnctest=模式
    .compile(“[\.$,[124; |]\\ s |-][124;]\\ b(LLC | Company | Incorporated | Co | Manufacturer | The | Limited | Ltd | Inc)\\b”,模式。不区分大小写)
    .matcher(manufacturerCell.getStringCellValue()).replaceAll(“”);
    if(不得与制造商联系)(制造商名称用于NCTEST){
    manufacturerCell.setCellStyle(样式);
    dncReasonCell.setCellValue(notToBeContactedManufacturers.get(manufacturerNameForDncTest.getDNCReason());
    }否则{
    dncReasonCell.setCellValue(“”);
    }
    }
    
  • 您应该为您的
    创建一个地图,不与制造商联系
    ,名称为键,制造商为值。然后,您可以使用
    containsKey
    ,而不是在
    mfgs列表中不断迭代
  • 您应该使用
    rowditerator
    的结果对行进行迭代。您不需要另一个迭代器
  • 局部变量不应以大写字母开头(
    DNC\U Reason
    -更好的名称应该是
    dncReasonCell
  • 示例代码假设制造商单元格已填充(
    getStringValue()
    如果未给出正确的值,可能会导致
    NullPointerException
    ),则初始化样式变量,并且您有一个不可联系的制造商映射:

    Iterator<Row> rowIterator = spreadsheet.rowIterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Cell dncReasonCell = row.getCell(1);
        if (dncReasonCell == null) {
            dncReasonCell = row.createCell(1, CellType.STRING);
        }
        Cell manufacturerCell = row.getCell(0);
        String manufacturerNameForDncTest = Pattern
                .compile("[\\.$|,|;|'|\\s|-]|\\b(LLC|Company|Incorporated|Co|Manufacturer|The|Limited|Ltd|Inc)\\b", Pattern.CASE_INSENSITIVE)
                .matcher(manufacturerCell.getStringCellValue()).replaceAll("");
        if (notToBeContactedManufacturers.containsKey(manufacturerNameForDncTest)) {
            manufacturerCell.setCellStyle(style);
            dncReasonCell.setCellValue(notToBeContactedManufacturers.get(manufacturerNameForDncTest).getDNCReason());
        } else {
            dncReasonCell.setCellValue("");
        }
    }
    
    Iterator rowditerator=电子表格.rowditerator();
    while(roweiterator.hasNext()){
    行=行迭代器。下一步();
    Cell dncReasonCell=row.getCell(1);
    if(dncReasonCell==null){
    dncReasonCell=row.createCell(1,CellType.STRING);
    }
    Cell-manufacturerCell=row.getCell(0);
    字符串制造商rnamefordnctest=模式
    .compile(“[\.$,[124; |]\\ s |-][124;]\\ b(LLC | Company | Incorporated | Co | Manufacturer | The | Limited | Ltd | Inc)\\b”,模式。不区分大小写)
    .matcher(manufacturerCell.getStringCellValue()).replaceAll(“”);
    if(不得与制造商联系)(制造商名称用于NCTEST){
    manufacturerCell.setCellStyle(样式);
    dncReasonCell.setCellValue(notToBeContactedManufacturers.get(manufacturerNameForDncTest.getDNCReason());
    }否则{
    dncReasonCell.setCellValue(“”);
    }
    }
    
    感谢您介绍您的项目。你遇到了什么问题需要帮助?嗨,Michael。基本上,我有两个专栏,我正在迭代——一个已经有了制造商列表,另一个我计划自动填充名为DNC Reason的数据。我遇到的主要问题是,当我试图运行该程序时,可能是因为制造商名称右侧的单元格为空,因此出现空指针异常。Michiel,很抱歉拼错了您的名称。现在有点晚了。谢谢你介绍你的项目。你遇到了什么问题需要帮助?嗨,Michael。基本上,我有两个专栏,我正在迭代——一个已经有了制造商列表,另一个我计划自动填充名为DNC Reason的数据。我遇到的主要问题是,当我试图运行该程序时,可能是因为制造商名称右侧的单元格为空,因此出现空指针异常。Michiel,很抱歉拼错了您的名称。我现在有点晚了。谢谢你,我试试这个!谢谢,我试试这个!