我需要写一个非常大的数字的excel单元格(> 91430000000000000000
)
问题是单元格的最大值是9143018315613270000
,所有更大的值 - 将被最大值替换。
如果将撇号添加到数字中,则只需手动解决此问题,例如'9143018315313276189
但是如何通过apache POI获得相同的技巧?我有以下代码:
attrId.setCellValue(new XSSFRichTextString('\'' + value.getId().toString()));
但它不起作用:
这里第一行根本没有任何撇号,第二行是用手写的,这是我正在寻找的结果。第三是我的代码的结果。我也尝试使用带有double和String的setCellValue,它们都不能帮助我。
所以,这里提出了一个问题:如何通过apache POI在Excel中写入非常大的数字?
答案 0 :(得分:5)
首先设置单元格样式
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);
答案 1 :(得分:0)
您可以设置Cell类型并查看会发生什么。或者,如果您已经设置了那么,请发布您的代码,以便其他人查看它。
cell.setCellType(Cell.CELL_TYPE_STRING);
有关如何将字符串值设置为单元格How can I read numeric strings in Excel cells as string (not numbers) with 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();
}
}
}