使用Apache POI将电话号码存储在Microsoft Excel XSSF中

我有一个电话号码作为string存储在Excel中,Excel文件已成功创build,数据没有错误,但每个电话号码旁边都有一个“存储为文本的数字”错误。

我已经在网上阅读过,我应该使用excel或自定义000-000-0000格式附带的特殊电话号码格式。 我可以设置这些使用Excel程序,但不是从我的Java代码。


我已经查找周围的setCellType和DataFormat的信息,但我认为CellType必须是string,我不明白如何使用DataFormat除了date之外的任何东西。

我也看了DataFormatter,但我不明白如何使用它来存储数据。 它看起来只是为了帮助阅读数据。 http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html


我怎样才能做到以下任一情况?

1)将单元格标记为“忽略错误”,忽略“存储为文本的数字”错误
2)利用内置的Excel单元格格式“Special> Phone Number”

对于1)看起来有一个标志,通过保存和closures文件持续存在,我不知道如何编辑或查看与POI。 有一个关于它的文章:

excel文档的页面669和670包含FeatFormulaErr2,这是FeatRecord共享function,理论上允许您存储的事实,即“数字作为文本”应忽略单元格范围

我们还得到了两个testing文件,一个是警告,一个是closures的 – 46136-NoWarnings.xls和46136-WithWarnings.xls。 但是我没有创build它们!

Nick http://mail-archives.apache.org/mod_mbox/poi-user/201003.mbox/%3C27823222.post@talk.nabble.com%3E

看来这可以在VBA中完成cell.Errors.Item(xlNumberAsText).Ignore = True但似乎没有POI

我想出了如何实现#2)利用内置的Excel单元格格式“Special> Phone Number”

Excel中的电话号码存储为CELL_TYPE_NUMERIC ,而不是CELL_TYPE_STRING

 try { row.createCell(0).setCellValue(Long.parseLong(aNumericOnlyPhoneNumberString)); } catch (NumberFormatException e) { row.createCell(0); } CellStyle phoneNumberStyle = wb.createCellStyle(); phoneNumberStyle.setDataFormat(wb.createDataFormat().getFormat("(000) 000-0000")); row.getCell(0).setCellStyle(phoneNumberStyle); 

在写入excel之前,我不确定是否正在进行数据types转换。 可能你可以使用这样的东西:

 if(cellData.getCellType() == 1) cell.setCellValue((String)cellData.getData()); else if(cellData.getCellType() == 0) cell.setCellValue(((Integer)cellData.getData()).intValue()); 

嘿尝试下面给出的代码 –

 @Test public void writeToExcel() { //Blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create a blank sheet XSSFSheet sheet = workbook.createSheet("Employee Data"); //This data needs to be written (Object[]) Map<String, Object[]> data = new TreeMap<String, Object[]>(); data.put("1", new Object[] {"ID", "NAME", "PHONE"}); data.put("2", new Object[] {1, "Amit", "9865321425"}); data.put("3", new Object[] {2, "Lokesh","9562264578"}); data.put("4", new Object[] {3, "John", "9458262145"}); //Iterate over data and write to sheet Set<String> keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object [] objArr = data.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); if(obj instanceof String) cell.setCellValue((String)obj); else if(obj instanceof Integer) cell.setCellValue((Integer)obj); } } try { //Write the workbook in file system FileOutputStream out = new FileOutputStream(new File("D:/writeTest.xlsx")); workbook.write(out); out.close(); System.out.println("writeTest.xlsx written successfully on disk."); } catch (Exception e) { e.printStackTrace(); } } 

默认情况下,excel的单元格types是String。 如果您想以数字格式存储电话号码,则必须将单元格types设置为数字。

  if(obj instanceof String) cell.setCellValue((String)obj); else if(obj instanceof Integer){ // set cell format to numeric cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue((Integer)obj); } 

在读取excel文件时,请记住您正在读取的stringtypes单元格数据或数字types单元格数据。

读取stringtypes的单元格数据使用代码as-

 cell.getStringCellValue(); 

并读取数字单元格types数据 –

 cell.getNumericCellValue(); 

尝试完美的工作。 这是用于读取excel xlsx格式的函数,并将所有数据存储在数组列表中..

  public ArrayList Xreadexcel(String file) { boolean f = false; ArrayList arraycontainer = new ArrayList(); try { FileInputStream myInput = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(file); XSSFSheet sheet = workbook.getSheetAt(0); int rowStart = sheet.getFirstRowNum(); int rowEnd = sheet.getLastRowNum() + 1; int count = workbook.getNumberOfSheets(); if (count > 1) { System.out.println("Only one Sheet Allowed"); } else { for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) { Row row = sheet.getRow(rowNum); int lastColumn = row.getLastCellNum(); ArrayList arraylist = new ArrayList(); int cn = 0; for (cn = 0; cn < lastColumn + 1; cn++) { Cell cell = row.getCell(cn, Row.RETURN_NULL_AND_BLANK); if ((cell == null) || (cell.equals("")) || (cell.getCellType() == cell.CELL_TYPE_BLANK)) { arraylist.add(""); } else { cell.setCellType(Cell.CELL_TYPE_STRING); arraylist.add(cell); } } arraycontainer.add(arraylist); } } } catch (Exception e) { e.printStackTrace(); } return arraycontainer; }