When exporting excel with POI, the solution for long numbers that do not want to be automatically changed to scientific notation

When exporting excel with POI, the solution for long numbers that do not want to be automatically changed to scientific notation

I have exported to excel many times. I have encountered a problem. If the content contains a relatively long number, such as the order number "2546541656596", excel will automatically become scientific notation. . .

I've tried it several times and haven't solved it. Recently, I have to export excel again. I'm determined to find a solution.

I have tested it in excel. Longer numbers will automatically become scientific notation unless we set the cell format to "text"

Haha, I seem to have found an idea: use poi to set the cell to "text type" first.

I found this method HSSFCell.setCellType(int type) from the documentation. How to see this method can set the cell format.

Adding cell.setCellType(HSSFCell.CELL_TYPE_STRING) to the code, there is still no change. . .

Helpless again

Searched from google for a long time and found this article

http://javacrazyer.iteye.com/blog/894758, the blogger wrote very detailed, from the principle to the solution

The idea is still the same, set the cell format to "text"

The real solution is explained directly below:

copy code
1   // Create workBook 2 HSSFWorkbook wb  = new HSSFWorkbook  (); 3 // Create a style 4 HSSFCellStyle cellStyle  =  wb.createCellStyle(); 5
     
   
   
  // Create a DataFormat object 6
  HSSFDataFormat format  =  wb.createDataFormat();
7  //In this way, we can truly control the cell format. @ refers to the text type. For the definition of the specific format, please refer to the original text above. 8
  cellStyle.setDataFormat(format.getFormat( " @ " ));
9  10
  / / How to create a cell is omitted, and finally set the format of the cell to write 11
  cell.setCellStyle(cellStyle);
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324625822&siteId=291194637