How to write a very large number in xlsx via apache poi

I need to write very large numbers to the excel cell (> 91430000000000000000) The problem is that the maximum value for the cell 9143018315613270000, and all values ​​that are greater than - will be replaced with the maximum value. This problem will be simply solved by hand if the apostrophe is added to the number, for example, '9143018315313276189 But what about the trick through the apache POI? I have a code:

    attrId.setCellValue(new XSSFRichTextString('\'' + value.getId().toString()));

But this does not work:

Example

Here the first line has no apostrophe at all, the second is handwritten, and this is the result I'm looking for. Thirdly, this is the result of my code. I also tried using setCellValue, which takes double and String, both of which do not help me on the air.

So here the question arises: how to write very large numbers in excel via apache POI?

+4
2

DataFormat format = workbook.createDataFormat();
CellStyle testStyle = workbook.createCellStyle();
testStyle.setDataFormat(format.getFormat("@"));
String bigNumber = "9143018315313276189";
row.createCell(40).setCellStyle(testStyle);
row.getCell(40).setCellValue(bigNumber);
+5

, . , , , , .

cell.setCellType(Cell.CELL_TYPE_STRING);

, . . Excel ( ) Apache POI?

(poi-3.1.3)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;


public class WriteToExcel {

    public static void main(String[] args) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Sample sheet");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue("91430183153132761893333");

        try {
            FileOutputStream out = 
                    new FileOutputStream(new File("C:\\test_stackoverflow\\new.xls"));
            workbook.write(out);
            out.close();
            System.out.println("Excel written successfully..");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
0

Source: https://habr.com/ru/post/1615759/


All Articles