简体   繁体   English

Java Spring Apache POI,将长号存储在excel中,没有指数

[英]Java Spring apache POI, store long number in excel without exponents

I'm trying to insert the BigDecimal value in excel but it is storing as exponential value. 我正在尝试在Excel中插入BigDecimal值,但它以指数值形式存储。

The value is storing as exponential value with below code: i/p: 1234567891012.12 o/p: 1.23457E+12 该值使用以下代码存储为指数值:i / p:1234567891012.12 o / p:1.23457E + 12

cell = rowhead.createCell(cellnumberno++);
        cell.setCellStyle(wrapRightDataNoBottomCellStyle);
        cell.setCellValue(Double.parseDouble(cellValue));

when i'm trying to insert value in excel with below, then i'm getting error like "Number is strored as string": i/p: 1234567891012.12 o/p: 1234567891012.12 with exclamation mark 当我尝试在excel中使用以下代码插入值时,则出现类似“数字作为字符串存储”的错误:i / p:1234567891012.12 o / p:1234567891012.12,带有感叹号

cell = rowhead.createCell(cellnumberno++);
        cell.setCellStyle(wrapRightDataNoBottomCellStyle);
        cell.setCellValue(cellValue);

Is there a way to remove this number is stored as text error from java code before entering value. 有没有办法在输入值之前从Java代码中删除此数字作为文本错误存储的方法。

Formatting as below is working fine but i dont want to restrict the number after decimal. 格式化如下工作正常,但我不想限制小数点后的数字。

   XSSFDataFormat format=wb.createDataFormat();
   wrapRightDataNoBottomCellStyle.setDataFormat(format.getFormat("0.00"));
   cell.setCellStyle(wrapRightDataNoBottomCellStyle);
   cell.setCellValue(Double.parseDouble(cellValue)); 

I dont want to format the cell with something like this "0.00", is there a way where i can set cell format to text and then enter the value and still avoid the number as text error? 我不想使用“ 0.00”之类的格式设置单元格,有没有一种方法可以将单元格格式设置为文本,然后输入值,仍然避免将数字作为文本错误?

I want insert the long decimal like below in excel as a string by avoiding "number stored as text" error. 我想通过避免“数字存储为文本”错误,在excel中将如下所示的长十进制作为字符串插入。 Input : 1234567891012.12222 Expected o/p: 1234567891012.122222 输入:1234567891012.12222预期的输出数:1234567891012.122222

Please help me, any pointers would be of great help. 请帮助我,任何指针都会有很大帮助。

I would never storing numeric values as text. 我永远不会将数值存储为文本。 The requirement having numeric cell content with as much decimal places as needed and not switching to exponential would be fulfilled using the number format 0.0############## . 使用数字格式0.0##############可以满足要求数字单元格内容具有所需的小数位数并且不切换到指数的要求。 Each # means: use this digit if needed. 每个#表示:如果需要,请使用此数字。 Digits before the decimal point are always showed if needed. 如果需要,总是显示小数点前的数字。 So there only one 0 is necessary. 因此,仅需要一个0 The format means in whole: at least one digit before the decimal point and at least one decimal place but 15 further decimal places maximal if needed. 格式整体上表示:小数点前至少一位数字和至少一位小数位,但如果需要,则最大为另外15位小数。 Excel always stores floating point values only with up to 15 significant digits precision. Excel始终只存储浮点值,最高精度为15位有效数字。 Thats why more than 15 decimal places are not useful. 那就是为什么超过15个小数位没有用的原因。

But if the requirement really is, storing numeric values as text, then this also is possible using the number format @ . 但是,如果确实需要将数字值存储为文本,则也可以使用数字格式@ This is the "Text" number format. 这是“文本”数字格式。 To avoiding "number stored as text" warning, the usage of XSSFSheet.addIgnoredErrors is possible if the Excel is of type XSSF . 为避免出现“数字存储为文本”警告,如果Excel类型为XSSF,则可以使用XSSF

Example: 例:

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(); 
 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM