Excel工作表删除以零开头的数字的零

时间:2019-07-31 13:08:37

标签: java apache-poi xls

我需要写CSV / Xls文件,并在其列中添加类似00078的数字,但是即使在将数字写为字符串之后,excel工作表也会将其显示为{{1} }。

我尝试过 78库,并尝试将单元格类型设置为String。

apache poi

我希望该列将HSSFWorkbook workbook = new HSSFWorkbook(); // Create a blank sheet HSSFSheet sheet = workbook.createSheet("student Details"); // This data needs to be written (Object[]) Map<String, Object[]> data = new TreeMap<String, Object[]>(); data.put("1", new Object[]{ "ID", "NAME", "LASTNAME" }); data.put("2", new Object[]{ 00078, "Pankaj", "Kumar" }); // Iterate over data and write to sheet Set<String> keyset = data.keySet(); int rownum = 0; for (String key : keyset) { // this creates a new row in the sheet Row row = sheet.createRow(rownum++); Object[] objArr = data.get(key); int cellnum = 0; for (Object obj : objArr) { // this line creates a cell in the next column of that row Cell cell = row.createCell(cellnum++); if (obj instanceof String) cell.setCellValue((String)obj); else if (obj instanceof Integer){ cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue(String.valueOf(obj)); } } } 视为字符串并显示相同的字符串。

2 个答案:

答案 0 :(得分:0)

我猜写这会导致编译错误-

  

整数太大

data.put("2", new Object[]{ 00078, "Pankaj", "Kumar" }); 

在评论中Adder正确地说了

data.put("2", new Object[]{ "00078", "Pankaj", "Kumar" });

这应该可以解决问题。

答案 1 :(得分:0)

如果ID必须具有前导零,则此ID的数据类型必须为String。数字没有前导零。数学上的数字000123与123完全相同。

Excel的情况下,包含此ID值的单元格应具有Text格式的单元格样式。为了防止通过编辑单元格将单元格样式更改为数字样式,应在附加的单元格样式前面加上引号。其他编辑单元格可能导致000123再次更改为123的情况。

以下代码显示了一个完整的示例,其中ID列受到这种保护。

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.GregorianCalendar;

class CreateExcelSheetFromDataObjectArray {

 public static void main(String[] args) throws Exception {

  Object[][] data = new Object[][]{
   new Object[]{"ID", "NAME", "LASTNAME", "AMOUNT", "DATE"},
   new Object[]{"000123", "John", "Doe", 1234.56, new GregorianCalendar(2019, 0, 1) },
   new Object[]{"000456", "Jane", "Stiles", 7890.12, new GregorianCalendar(2019, 1, 11) },
   new Object[]{"000789", "Mary", "Major", 3456.78, new GregorianCalendar(2019, 2, 22) }
  };

  String filePath = "./Excel";

  String wantedXLFormat = 
   //"XSSF";
   "HSSF";

  try (Workbook workbook = ("XSSF".equals(wantedXLFormat))?new XSSFWorkbook():new HSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream(filePath + (("XSSF".equals(wantedXLFormat))?".xlsx":".xls")) ) {

   DataFormat dataFormat = workbook.createDataFormat();
   CellStyle dateStyle = workbook.createCellStyle();
   dateStyle.setDataFormat(dataFormat.getFormat("DDDD, MMMM, DD, YYYY"));
   CellStyle numberStyle = workbook.createCellStyle();
   numberStyle.setDataFormat(dataFormat.getFormat("#,##0.00 \" Coins\""));

   // the cell style for the ID column has Text format and is quote prefixed
   CellStyle idStyle = workbook.createCellStyle();
   idStyle.setDataFormat(dataFormat.getFormat("@"));
   idStyle.setQuotePrefixed(true);

   Sheet sheet = workbook.createSheet(); 

   for (int r = 0; r < data.length; r++) {
    Row row = sheet.createRow(r);
    for (int c = 0; c < data[0].length; c++) {
     Cell cell = row.createCell(c);

     if (r == 0) cell.setCellValue((String)data[r][c]); // the header row, all columns are strings

     if (r > 0 && c == 0) { // the ID column
      cell.setCellValue((String)data[r][c]);
      cell.setCellStyle(idStyle);
     } else if (r > 0 && c == 3) { // the number column
      cell.setCellValue((Double)data[r][c]);
      cell.setCellStyle(numberStyle);
     } else if (r > 0 && c == 4) { // the date column
      cell.setCellValue((GregorianCalendar)data[r][c]);
      cell.setCellStyle(dateStyle);
     } else if (r > 0) { // all other columns are strings
      cell.setCellValue((String)data[r][c]);
     }
    }
   }

   for (int c = 0; c < data[0].length; c++) {
    sheet.autoSizeColumn(c);
   }

   workbook.write(fileout);
  }
 }
}

该代码可以使用apache poi 4.1.0进行测试。