Spring 我想使用apache POI从excel中读取自定义格式数据。从excel中读取自定义文本时会出现错误&引用&引用;
错误:java.lang.IllegalStateException:无法获取数值 从位于的文本单元格 org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:888) 在 org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:221) 在 org.apache.poi.xssf.usermodel.XSSFCell.getDateCellValue(XSSFCell.java:621) 在 com.bvm.controller.MainController.ExcelProcessing(MainController.java:117) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处` 代码示例:Spring 我想使用apache POI从excel中读取自定义格式数据。从excel中读取自定义文本时会出现错误&引用&引用;,spring,apache,model-view-controller,apache-poi,Spring,Apache,Model View Controller,Apache Poi,错误:java.lang.IllegalStateException:无法获取数值 从位于的文本单元格 org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:888) 在 org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:221) 在 org.apache.poi.xssf.usermodel.XSSFCell.getDat
@RequestMapping(value="processExcel", method = RequestMethod.POST)
public String ExcelProcessing(Model model, @RequestParam("excelfile") MultipartFile excelfile)
{
try {
List<ExcelHeatModel> lstheatdata = new ArrayList<>();
int i = 1;
// Creates a workbook object from the uploaded excelfile
XSSFWorkbook workbook = new XSSFWorkbook(excelfile.getInputStream());
// Creates a worksheet object representing the first sheet
XSSFSheet worksheet = workbook.getSheetAt(0);
// Reads the data in excel file until last row is encountered
while (i <= worksheet.getLastRowNum()) {
// Creates an object for the Excel Heat Model
ExcelHeatModel heatdata = new ExcelHeatModel();
// Creates an object representing a single row in excel
XSSFRow row = worksheet.getRow(i++);
// Sets the Read data to the model class
heatdata.setTaskid((int)(row.getCell(0).getNumericCellValue())); //reading numeric value from excel
System.out.println(heatdata.getTaskid());
heatdata.setCustomername(row.getCell(1).getStringCellValue()); //reading string value
System.out.println(heatdata.getCustomername());
//in the below code i am receiving error.
DataFormatter formatter = new DataFormatter();
String cellAsStr = formatter.formatCellValue((Cell) row.getCell(14).getDateCellValue());
heatdata.setAcceptedon(cellAsStr);
System.out.println(heatdata.getAcceptedon());
@RequestMapping(value=“processExcel”,method=RequestMethod.POST)
公共字符串ExcelProcessing(模型模型@RequestParam(“excelfile”)多部分文件excelfile)
{
试一试{
List lstheatdata=new ArrayList();
int i=1;
//从上载的Excel文件创建工作簿对象
XSSF工作簿=新的XSSF工作簿(excelfile.getInputStream());
//创建表示第一张图纸的工作表对象
XSSFSheet工作表=工作簿。getSheetAt(0);
//读取excel文件中的数据,直到遇到最后一行
while(i)哪一行是MainController.java中的第117行?如果是String cellAsStr=formatter.formatCellValue((Cell)row.getCell(14.getDateCellValue());
然后在当前行的O
列中不是日期而是文本内容。顺便说一句:即使有日期,您也会得到ClassCastException
,因为您无法将date
强制转换为Cell
。它应该是String cellAsStr=formatter.formatCellValue(row.getCell(14))
。感谢Alex的回复。我的excel工作表单元格“10/5/2016 9:48”中有日期和时间,但其格式在excel工作表中是自定义的。我尝试了您的代码,但它给了我相同的错误“无法从文本单元格中获取数值”,错误是从哪一行引发的?它不能是字符串cellAsStr=formatter.formatCellValue(row.getCell(14));
。实际上excel单元格的格式与apache poi获取的格式不匹配,因此我得到了错误。这就是我在117行得到错误的原因。您能告诉我从excel解析自定义格式值的方法吗。其中单元格的值是日期时间格式m/d/yyyy h:mm