无法从文本单元格中获取数值

时间:2016-07-20 06:03:21

标签: java excel apache-poi

我正在尝试将数据从Excel导入MySQL。

    java.util.ArrayList;
    import java.util.Iterator;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;

    import moduleobject.Summary;

    public class SummaryReader implements SheetReader<ArrayList<Summary>> {

        public ArrayList<Summary> read(Sheet sheet) {

            Iterator<Row> rowiterator = sheet.iterator();
            rowiterator.next();
            ArrayList<Summary> list = new ArrayList<>();
            Summary obj = new Summary();
            while (rowiterator.hasNext()) {

                Row nextRow = rowiterator.next();
                Iterator<Cell> celliterator = nextRow.cellIterator();

                while (celliterator.hasNext()) {

                    obj.setProcessor(celliterator.next().getStringCellValue());
                    obj.setProcessName(celliterator.next().getStringCellValue());

                    obj.setLevel(celliterator.next().getNumericCellValue());
                    obj.setPA11(celliterator.next().getNumericCellValue());
                    obj.setPA21(celliterator.next().getNumericCellValue());
                    obj.setPA22(celliterator.next().getNumericCellValue());

                    // obj.setPA31(celliterator.next().getNumericCellValue());
                    // obj.setPA32(celliterator.next().getNumericCellValue());
                }
            }

            list.add(obj);

            return list;
        }

    }

我得到的错误是

Exception in thread "main" java.lang.IllegalStateException: Cannot get a numeric value from a text cell
    at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:994)
    at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:305)
    at reader.SummaryReader.read(SummaryReader.java:32)
    at reader.SummaryReader.read(SummaryReader.java:1)
    at controller.JobController.startJob(JobController.java:69) at job.JobLauncher.main(JobLauncher.java:21)

为什么我会收到此异常?

2 个答案:

答案 0 :(得分:2)

请使用:

DataFormatter formatter = new DataFormatter();
Cell cell = sheet.getRow(i).getCell(0);
String var_name = formatter.formatCellValue(cell);

答案 1 :(得分:-1)

也许,其中一种方法是您可以将单元格类型设置为

Sub datascrub()

On Error Resume Next
    Set SourceSheet = ActiveSheet
    Set TargetSheet = ActiveWorkbook.Sheets("Results")
    If Err = 0 Then
        Worksheets("Results").Delete
    End If

    Worksheets.Add
    ActiveSheet.Name = "Results"
    Set TargetSheet = ActiveSheet
    Cells(1, 1).Value = "Found Codes"
    Cells(1, 1).Font.Bold = True
    iTargetRow = 2

    SourceSheet.Select
    Selection.SpecialCells(xlCellTypeLastCell).Select
    Range(Selection, Cells(1)).Select

    For Each c In Selection.Cells
        If c.Value Like "?#+@" Then
            sRaw = c.Value
            iPos = InStr(sRaw, "?#+@")
            Do While iPos > 0
                If iPos < 4 Then
                    sRaw = "  " & sRaw
                    iPos = iPos + 4
                End If
                sTemp = Mid(sRaw, iPos, 4)
                sRaw = Mid(sRaw, iPos + 4, 24)
                If sTemp Like "?#+@" Then
                    TargetSheet.Cells(iTargetRow, 1) = sTemp
                    iTargetRow = iTargetRow + 1
                Else
                    sRaw = Mid(sTemp, 4, 5) & sRaw
                End If
                iPos = InStr(sRaw, "?#+@")
            Loop
        End If
    Next c
End Sub

在读取单元格值并将单元格值读取为字符串之前。然后,如果需要,可以将字符串值解析为数字。