java.lang.IllegalStateException:无法从数字公式单元格中获取文本值

我正在使用POIFSFileSystem和HSSFWorkbook来读取我的excel并将其上传到我的db.i已经将我的pgm中的所有var声明为字符串,并且我已经将我的excel单元格格式化为text..Even然后我得到“java.lang。 IllegalStateException:无法从数字公式单元格中获取文本值“。 单元格类型显示为数字,但我将单元格格式化为text.Any解决方案的家伙?

这是我的代码:

/* * FarmerQueryMigration.java * Copyright (c) 2014-2015, SourceTrace Systems, All Rights Reserved. * * This software is the confidential and proprietary information of SourceTrace Systems * ("Confidential Information"). You shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement you entered into with * SourceTrace Systems. */ package com.ese.view.profile; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import com.ese.util.DateUtil; import com.ese.util.ObjectUtil; import com.ese.util.StringUtil; public class FarmerQueryMigration { private static final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("dd-MM-yyyy"); /** * The main method. * @param args the arguments * @throws IOException Signals that an I/O exception has occurred. */ @SuppressWarnings({ "deprecation", "deprecation" }) public static void main(String args[]) throws IOException { String fileName = "E:\\viji\\proj docs\\aditi upload files\\Bajolga Provisional Approved Farmers List.XLS"; FileInputStream myInput = new FileInputStream(fileName); FileOutputStream myOutput = new FileOutputStream( "E:\\viji\\proj docs\\aditi upload files\\FarmerInsertQuery_" + fileNameDateFormat.format(new Date()) + ".sql"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); HSSFSheet mySheet = myWorkBook.getSheetAt(1); StringBuilder sb = new StringBuilder(); String initialQuery = "INSERT INTO FARMER VALUES(null,\""; String accountInsertQuery = "INSERT INTO ESE_ACCOUNT VALUES (NULL,\"22"; String contractQuery = "INSERT INTO CONTRACT VALUES (NULL,\""; String cardInsertQuery = "INSERT INTO ESE_CARD VALUES (NULL,\"12"; String contarctPricePatternQuery = "INSERT INTO CONTRACT_PRICEPATTERN_MAP VALUES ('"; String nullString = null; int farmerSeq = 100; int accountSeq = 103; int contractSeq = 103; int contractPriceSeq = 103; int cardSeq = 103; int i = 3; int rowCount = mySheet.getLastRowNum(); /* String test = null; String test1 = null; while (i <=0) { HSSFRow myRow = mySheet.getRow(i); // if(i==rowCount){ System.out.println("----------ROW_NO:" + i + "-----------"); String firstName = !StringUtil.isEmpty(myRow.getCell(0).getStringCellValue()) ? String .valueOf((int) myRow.getCell(0).getNumericCellValue()) : nullString; test = !ObjectUtil.isEmpty(myRow.getCell(1)) ? String .valueOf((int) myRow.getCell(1).getNumericCellValue()) : nullString; test1 = !ObjectUtil.isEmpty(myRow.getCell(2)) ? String .valueOf((int) myRow.getCell(2).getNumericCellValue()) : nullString; //} System.out.println("---------" + String .valueOf(myRow.getCell(0).getStringCellValue()) + "-----------"); if(!ObjectUtil.isEmpty(myRow.getCell(1))&&!ObjectUtil.isEmpty(myRow.getCell(2))){ System.out.println(myRow.getCell(1).getStringCellValue()); System.out.println(myRow.getCell(2).getStringCellValue()); } else{ System.out.println("----------Else" + i + "-----------"); } //System.out.println("----------ROW_NO:" + i + "-----------"); i++; } */ String customerProjectQuery="INSERT INTO `customer_project` VALUES ('22', '00320002', 'Chitradurga', '1', '1', '1', '1', 'Chitradurga', '0', '0', null, '1', '1', '32', '5', '0', '20150210184539');"; sb.append(customerProjectQuery+"\n"); try { while (i  count) ? (value.substring(0, count)) : (getEmptyLength(count - value.length())) + value); } /** * Gets the empty length. * @param count the count * @return the empty length */ public static String getEmptyLength(int count) { StringBuffer data = new StringBuffer(); for (int i = 0; i < count; i++) data.append("0"); return data.toString(); } } 

  switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cell.getBooleanCellValue() break; case Cell.CELL_TYPE_NUMERIC: cell.getNumericCellValue() break; case Cell.CELL_TYPE_STRING: cell.getStringCellValue() break; case Cell.CELL_TYPE_BLANK: 
  break; default: cell.toString() } 

尝试上面一个

excel表中的所有数值/数据必须具有前导单引号:’

例如 :

“07543234337

“0051698435638

“10005