Pregunta

I am using apache poi api to generate Excel sheet in my application in java. Data that are set in excel are coming dynamically with type string. For column1, values are alphanumeric. When I generate Excel, it will give me green indication with warning "Number Stored as Text" or "Text date with 2-digit year" on cell.

I want to remove that warning. I found that from excel we can flag a cell as 'ignore error', to ignore the warning.

How to do it programmatically or is there any other alternative to achieve this task ?

I also have attached screenshot that shows warning with green mark.

enter image description here

Code :

if (cellValue != null && (shouldBeRightAlign))
{
 cellType = Cell.CELL_TYPE_NUMERIC;
}
else if (cellValue != null)
{
 cellType = Cell.CELL_TYPE_STRING;
}
cell.setCellValue(cellValue);
¿Fue útil?

Solución

The API now provides a corresponding method

public void addIgnoredErrors(CellRangeAddress region,
                    IgnoredErrorType... ignoredErrorTypes)

Otros consejos

Apparently this is not currently possible with Apache POI. There are a number of bug reports / RFEs on the subject:

https://issues.apache.org/bugzilla/show_bug.cgi?id=58641

https://issues.apache.org/bugzilla/show_bug.cgi?id=54868

https://issues.apache.org/bugzilla/show_bug.cgi?id=46136

The actual attribute (the attribute that says to ignore such warning) exists in the Excel persistent format and is recorded on a per cell basis. It is just that Apache POI doesn't expose it, as far as I can tell from the bug reports.

To give you an idea of what this looks like in Excel's xlsx format:

</worksheet>
    ...
    <ignoredErrors>
        <ignoredError sqref="K192 E181 E186" numberStoredAsText="1"/>
    </ignoredErrors>    
</worksheet>

I've read somewhere that this is persisted only in xlsx (i.e. OOXML), not in xls format which means that if and when the Apache POI team implements this it will be in the XSSF package.

For me, none of the solutions above worked, but at the end got rid of the warnings with a bit convoluted workaround: writting the values as formulas like this.

String asFormula = "\"" + value + "\"";
cell.setCellType(SXSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula(asFormula);

Don't think it is always a good solution, but in my case the values of the output file are never modified. But it might help somebody.

I'm using version 3.14 of apache POI, in case it makes a difference (I've tried a few different versions before the workaround, with no effect).

Set the cell type like :

HSSFRow row = sheet.createRow(sheet.getLastRowNum());
HSSFCell cell = row.createCell(0);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);

For alphanumeric and Character cell type will be Cell.CELL_TYPE_STRING for number, cell type will be CELL_TYPE_NUMERIC

You can get more on cell type from Cell and HSSFCell documentation.

Old one But Still it This will help Some one from .NET who use NPOI

I tried with

cell.SetCellType(NPOI.SS.UserModel.CellType.NUMERIC); 

which didn't helped at all. but the below code will work

dCell.setCellValue(Convert.ToDouble(112.45)); //.NET syntax

dCell.setCellValue(new Double("123"));  // Java Syntax

I was facing the same issue. I resolved it through checking the data if it is an Integer/Float and putting the condition(CELL_TYPE) accordingly. Please find the code Snippet:

if(!isInteger(data))
            {
            cell.setCellValue(data);
            }
            else
            {
                cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                cell.setCellValue(Float.parseFloat(data));//.parseInt(data));   
            }
public static boolean isInteger(String s) {
    try { 
        java.lang.Float.parseFloat(s); 
    } catch(NumberFormatException e) { 
        return false; 
    }
    return true;
}

Here data is a String from the array of list from the data source(excel/csv) This resolved the green warning.

I know it has been a long time since the question was asked, but still... Did you try this:

DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("@"));

Old but still relevant.

You should use "." in text as decimal separator and to format cell as NUMERIC.

Use also DataFormat with de pattern "#.00" and voila! No warning and everything gones fine.

cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue(Double.parseDouble(data));
DataFormat format = hwb.createDataFormat();
CellStyle numbercellStyle = hwb.createCellStyle();
numbercellStyle.setDataFormat(format.getFormat("#.00"));
cell.setCellStyle(numbercellStyle);

Simple, You can also use below code in poi 4.1.1

cell.setCellValue(Double.parseDouble(yourvalue));

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top