简体   繁体   English

将Excel日期数据保存到MySQL时Java Date Formatter不起作用

[英]Java Date Formatter not working while saving Excel date data into MySQL

My goal is to save every rows of data in excel into MySQL database. 我的目标是将Excel中的每一行数据保存到MySQL数据库中。 In my excel file, the date format is dd.MM.yyyy, whereas yyyy-MM-dd is the date format in MySQL. 在我的excel文件中,日期格式为dd.MM.yyyy,而yyyy-MM-dd是MySQL中的日期格式。

Here are my codes: 这是我的代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {

    public static void main(String[] args) throws Exception {

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/activity", "root", "password");
            con.setAutoCommit(false);
            PreparedStatement pstm = null ;
            FileInputStream input = new FileInputStream("testing.xlsx");
            //POIFSFileSystem fs = new POIFSFileSystem( input );
            XSSFWorkbook wb = new XSSFWorkbook(input);
            XSSFSheet sheet = wb.getSheetAt(0);
            Row row;
            for(int i=1; i<=sheet.getLastRowNum(); i++){
                row = sheet.getRow(i);
                String activityName= row.getCell(5).getStringCellValue();
                Integer activityAllocation = (int) row.getCell(8).getNumericCellValue();
                //DataFormatter dataFormatter = new DataFormatter();
                //String activityDate = dataFormatter.formatCellValue(row.getCell(10));
                Date activityDate = row.getCell(10).getDateCellValue();                
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String Date = sdf.format(activityDate);
                String activityPersonInCharge = row.getCell(11).getStringCellValue();

                String sql = "INSERT INTO activity(ACTIVITY_NAME,ACTIVITY_ALLOCATION,ACTIVITY_DATE,ACTIVITY_MADE_BY) VALUES('"+activityName+"','"+activityAllocation +"','"+activityDate +"','"+activityPersonInCharge +"')";
                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) {
            e.printStackTrace();
        }
    }

}

I tried to format the date of the excel cell but failed to work. 我试图格式化Excel单元格的日期,但无法正常工作。 The exception: 例外:

Exception in thread "main" java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell
    at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1062)
    at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:307)
    at org.apache.poi.xssf.usermodel.XSSFCell.getDateCellValue(XSSFCell.java:776)
    at ExcelReader.main(ExcelReader.java:35)

Which part of the codes that I did wrongly? 我错误地执行了代码的哪一部分? Also, if I use the commented line of codes for the date formatter, no exception shown but it save the wrong dates. 另外,如果我将注释的代码行用于日期格式化程序,则不会显示任何异常,但会保存错误的日期。 Please guide me. 请指导我。 Thanks in advance 提前致谢

Get the value as a String 以字符串形式获取值

String activityDate = row.getCell(10).getStringCellValue();                

According to you the format is 19.06.2014 根据您的格式是19.06.2014

so 所以

SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

then parse it into a Date 然后将其解析为一个日期

Date dt = sdf.parse(activityDate);

Use PreparedStatment to insert 使用PreparedStatment插入

Set the value (column 3 ?) 设置值(第3列)

ps.setDate (3, dt);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM