Cannot get a text value from a numeric cell “Poi”

前端 未结 11 2022
余生分开走
余生分开走 2020-12-07 22:17

I\'m trying to consume data from a spreadsheet in Excel, but always of this error, already tried formatting the worksheet to text and number and still the error persists.

11条回答
  •  既然无缘
    2020-12-07 23:04

    Use that code it definitely works and I modified it.

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    //import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.*;
    
    public class TestApp {
    
        public static void main(String[] args) throws Exception {
    
            try {
    
                Class forName = Class.forName("com.mysql.jdbc.Driver");
                Connection con = null;
                con = DriverManager.getConnection("jdbc:mysql://localhost/tables", "root", "root");
                con.setAutoCommit(false);
                PreparedStatement pstm = null;
                FileInputStream input = new FileInputStream("C:\\Users\\Desktop\\a1.xls");
                POIFSFileSystem fs = new POIFSFileSystem(input);
                Workbook workbook;
                workbook = WorkbookFactory.create(fs);
                Sheet sheet = workbook.getSheetAt(0);
                Row row;
                for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                    row = (Row) sheet.getRow(i);
                    String name = row.getCell(0).getStringCellValue();
                    String add = row.getCell(1).getStringCellValue();
    
                    int  contact = (int) row.getCell(2).getNumericCellValue();
    
                    String email = row.getCell(3).getStringCellValue();
    
                    String sql = "INSERT INTO employee (name, address, contactNo, email) VALUES('" + name + "','" + add + "'," + contact + ",'" + email + "')";
                    pstm = (PreparedStatement) con.prepareStatement(sql);
                    pstm.execute();
                    System.out.println("Import rows " + i);
                }
                con.commit();
                pstm.close();
                con.close();
                input.close();
                System.out.println("Success import excel to mysql table");
            } catch (IOException e) {
            }
        }
    
    }
    

提交回复
热议问题