可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
Code :
if (cellValue != null && (shouldBeRightAlign))
{
cellType = Cell.CELL_TYPE_NUMERIC;
}
else if (cellValue != null)
{
cellType = Cell.CELL_TYPE_STRING;
}
cell.setCellValue(cellValue);
回答1:
The API now provides a corresponding method
public void addIgnoredErrors(CellRangeAddress region,
IgnoredErrorType... ignoredErrorTypes)
回答2:
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.
回答3:
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).
回答4:
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.
回答5:
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
回答6:
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.
回答7:
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("@"));