The problem is as follows: Long numbers will be tampered with if the cell format is not set to text when you set the template
Solution 1: Step by step format individual cells as text and paste code directly onto them
@test public void testWrite07() throws Exception {// Create Workbook Workbook = new XSSFWorkbook(); // Create a sheet in the workbook and name it Sheet = workbook.createsheet (" Vaccination statistics "); Row Row = sheet.createrow (0); Cell cell11 = row.createcell (0); Cell cell11 = row.createcell (0); // write the text cell11.setcellValue (" Today's vaccination statistics "); Cell cell12 = row.createcell (1); Cell cell12 = row.createcell (1); // write seat cell12.setCellValue("666 people "); // Set the cell format to text format CellStyle CellStyle = workbook.createcellStyle (); DataFormat dataFormat = workbook.createDataFormat(); cellStyle.setDataFormat(dataFormat.getFormat("@")); // Set the first text format cell11.setCellStyle(cellStyle); // Set the format of the cells at position 1-1 to text}Copy the code
Solution 2: Directly set the cell format of the entire column to “text” and paste the code directly
@test public void testWrite07() throws Exception {// Create Workbook Workbook = new XSSFWorkbook(); // Create a sheet in the workbook and name it Sheet = workbook.createsheet (" Vaccination statistics "); CellStyle cellStyle = workbook.createCellStyle(); DataFormat dataFormat = workbook.createDataFormat(); cellStyle.setDataFormat(dataFormat.getFormat("@")); / / this style represents the text format sheet. SetDefaultColumnStyle (1, cellStyle); / / set the column 1, the corresponding format sheet setDefaultColumnStyle (2, cellStyle); / / set the second column, the corresponding format sheet setDefaultColumnStyle (3, cellStyle); Row Row = sheet.createrow (0); Row = sheet.createrow (0); Cell cell11 = row.createcell (0); Cell cell11 = row.createcell (0); // write the text cell11.setcellValue (" Today's vaccination statistics "); Cell cell12 = row.createcell (1); Cell cell12 = row.createcell (1); // write seat cell12.setCellValue("666 people "); }Copy the code
Columns 0 and 4 are still in the normal format. Columns 1, 2 and 3 are text format. The following figure