java 如何使用 POI 设置空白 Excel 单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31155036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:11:54  来源:igfitidea点击:

How to set Blank excel cell using POI

javaexcelapache-poi

提问by user2810293

I have a condition where in I need to check for the Delimiter in a string. If found then I need to set excel cell to blank. I have this code:

我有一个条件,我需要检查字符串中的分隔符。如果找到,那么我需要将excel单元格设置为空白。我有这个代码:

                row = sheet.createRow(newRow);
            for(String fieldValues: fieldList)
            {
                if(fieldValues.contains(","))
                {
                    XSSFCell cell = row.createCell(cellNo);
                    cell.setCellValue(Cell.CELL_TYPE_BLANK);
                    cellNo++;
                }
                else
                {
                    XSSFCell cell = row.createCell(cellNo);
                    cell.setCellValue(fieldValues);
                    cellNo++;
                }
            }

Cell.CELL_TYPE_BLANK is not setting up the Blank Cell even I tried with cell.setCellValue("") but again it doesn't set. Does it try to skip that cell if we want to set a Blank Cell? if not then can anyone let me know how I can do that.

Cell.CELL_TYPE_BLANK 没有设置空白单元格,即使我尝试使用 cell.setCellValue("") 但它也没有设置。如果我们想设置一个空白单元格,它是否会尝试跳过该单元格?如果没有,那么任何人都可以让我知道我如何做到这一点。

Appreciate your help!!

感谢你的帮助!!

回答by Kai

According to the Documentation and POI implementation I'd expect that

根据文档和 POI 实施,我希望

cell.setCellType(Cell.CELL_TYPE_BLANK);

should do the trick and is all you need.

应该可以解决问题,这就是您所需要的。

回答by Alan

I am a little late but,

我有点晚了但是,

dont think about setting the cell to be empty. Think about leaving the cell empty. When you create a Cell, it is empty by default until you set it's value.

不要考虑将单元格设置为空。考虑将单元格留空。创建单元格时,默认情况下它是空的,直到您设置它的值。

try something like this:

尝试这样的事情:

            row = sheet.createRow(newRow);
        for(String fieldValues: fieldList)
        {
            if(fieldValues.contains(","))
            {
                XSSFCell cell = row.createCell(cellNo); //it's already blank here
                cellNo++;
            }
            else
            {
                XSSFCell cell = row.createCell(cellNo);
                cell.setCellValue(fieldValues);
                cellNo++;
            }
        }

回答by blackbishop

You are using setCellValue. Instead use this method : setCellStyle

您正在使用setCellValue. 而是使用此方法:setCellStyle

row = sheet.createRow(newRow);
for(String fieldValues: fieldList)
{
  if(fieldValues.contains(","))
  {
    XSSFCell cell = row.createCell(cellNo);
    cell.setCellStyle(Cell.CELL_TYPE_BLANK);
    cellNo++;
  }
  else
  {
    XSSFCell cell = row.createCell(cellNo);
    cell.setCellValue(fieldValues);
    cellNo++;
  }
}