JavaSpringApachePOI,在excel中存储长数字,不带指数
我试图在excel中插入BigDecimal值,但它存储为指数值 该值存储为指数值,代码如下: i/p:1234567891012.12 o/p:1.23457E+12JavaSpringApachePOI,在excel中存储长数字,不带指数,java,excel,spring,apache-poi,Java,Excel,Spring,Apache Poi,我试图在excel中插入BigDecimal值,但它存储为指数值 该值存储为指数值,代码如下: i/p:1234567891012.12 o/p:1.23457E+12 cell = rowhead.createCell(cellnumberno++); cell.setCellStyle(wrapRightDataNoBottomCellStyle); cell.setCellValue(Double.parseDouble(cellValue)); 当我试图
cell = rowhead.createCell(cellnumberno++);
cell.setCellStyle(wrapRightDataNoBottomCellStyle);
cell.setCellValue(Double.parseDouble(cellValue));
当我试图在excel中插入值时,会出现如下错误:“数字被选为字符串”:
i/p:1234567891012.12
o/p:1234567891012.12,带感叹号
cell = rowhead.createCell(cellnumberno++);
cell.setCellStyle(wrapRightDataNoBottomCellStyle);
cell.setCellValue(cellValue);
在输入值之前,有没有办法从java代码中删除这个存储为文本错误的数字
下面的格式很好,但我不想限制小数点后的数字
XSSFDataFormat format=wb.createDataFormat();
wrapRightDataNoBottomCellStyle.setDataFormat(format.getFormat("0.00"));
cell.setCellStyle(wrapRightDataNoBottomCellStyle);
cell.setCellValue(Double.parseDouble(cellValue));
我不想使用类似“0.00”的格式设置单元格,是否有一种方法可以将单元格格式设置为文本,然后输入值,并且仍然避免数字作为文本错误
我希望通过避免“数字存储为文本”错误,在excel中以字符串形式插入长十进制,如下所示。
输入:1234567891012.12222
预计o/p:1234567891012.122222
请帮帮我,任何指针都会很有帮助。我永远不会将数值存储为文本。使用数字格式
0.0##################
可以满足数字单元格内容具有所需小数位数且不切换为指数的要求。每个#
表示:如果需要,请使用此数字。如果需要,始终显示小数点之前的数字。因此,只需要一个0
。该格式的整体含义是:小数点前至少有一位,小数点后至少有一位,但如果需要,最多还有15位Excel
始终仅存储精度高达15位的浮点值。这就是为什么超过15个小数位是没有用的
但如果要求将数值存储为文本,那么也可以使用数字格式@
。这是“文本”数字格式。为避免“数字存储为文本”警告,如果Excel
类型为XSSF
,则可以使用
例如:
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
public class ExcelStoreBigNumbers {
public static void main(String[] args) throws Exception {
String valueInput = "123456789012.12345";
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
CellStyle style;
DataFormat format = workbook.createDataFormat();
Row row;
Cell cell;
//numeric cell value formatted having as much decimal places as needed
style = workbook.createCellStyle();
style.setDataFormat(format.getFormat("0.0##############"));
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue(Double.parseDouble(valueInput)); //precision only up to 15 significant digits
//string cell value formatted as text
style = workbook.createCellStyle();
style.setDataFormat(format.getFormat("@"));
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue(valueInput);
//avoiding "number stored as text" warning
if (sheet instanceof XSSFSheet) {
((XSSFSheet)sheet).addIgnoredErrors(new CellReference(cell), IgnoredErrorType.NUMBER_STORED_AS_TEXT);
}
sheet.autoSizeColumn(0);
workbook.write(new FileOutputStream("ExcelStoreBigNumbers.xlsx"));
workbook.close();
}
}
您将在Excel中应用哪种格式的单元格,以使您的数字显示为所需的格式?如果你在POI中应用与样式相同的格式会发生什么?嗨,我已经用格式描述“0.00”更新了我的票据。请您帮助我如何将单元格格式化为字符串并在excel中插入长小数点。还有,我可以从JAVA代码中删除excel中的“数字存储为文本”错误标记。非常感谢阿克塞尔·里希特,你的回答对我很有帮助。