poi版本4.1.2
生成excel后,单元格输入数字,过长的话变成这样
解决:生成的时候设置单元格格式为文本格式
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import java.io.FileOutputStream;
import java.io.IOException;public class ExcelWriter {public static void main(String[] args) {try (Workbook workbook = new XSSFWorkbook()) {Sheet sheet = workbook.createSheet("Sheet1");// 设置单元格样式为文本格式CellStyle textStyle = workbook.createCellStyle();DataFormat format = workbook.createDataFormat();textStyle.setDataFormat(format.getFormat("@"));int columnIndex = 0; // 要设置样式的列索引,例如第一列for (int i = 0; i < 10; i++) { // 假设有10行数据Row row = sheet.createRow(i);Cell cell = row.createCell(columnIndex);cell.setCellStyle(textStyle);}// 保存Excel文件try (FileOutputStream fileOut = new FileOutputStream("output.xlsx")) {workbook.write(fileOut);}} catch (IOException e) {e.printStackTrace();}}
}
设置了10行的格式,11行没设置