使用Java将.csv或.xlsx文件导入到mysql数据库中?

是的,这听起来像是重复的。

我在Intellij上练习Java的升技,并试图编写一个程序来将.xls excel文件导入到mysql数据库中。 重复的问题,是的,但网上拖网没有多less收益。

我下面的代码目前完成导入任何xls文件的工作。 不幸的是,它没有为.csv文件和xlsx文件做任何事情。

当我尝试使用.csv文件时,会引发以下错误:

Invalid header signature; read 0x6972702C74786574, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document 

当使用xlsx文件时,下面的代码被抛出为错误:

 Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:152) at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:302) at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:85) at FileExport.main(FileExport.java:21) 

我的代码:

 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.*; public class FileExport { 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:3306/test?useSSL=false", "root", "root"); con.setAutoCommit(false); PreparedStatement pstm = null; FileInputStream input = new FileInputStream("/Users/User/Desktop/Email/Test.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 text = row.getCell(0).getStringCellValue(); int price = (int) row.getCell(1).getNumericCellValue(); String sql = "INSERT INTO testtable (text, price) VALUES('" + text + "','" + price + "')"; pstm = (PreparedStatement) con.prepareStatement(sql); pstm.setString(1, text); pstm.setInt(2, price); 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) { } } 

}

任何build议如何调整这个代码来导入.csv或xlsx文件非常感谢。

您应该使用PreparedStatement,因为它的目的是:

 String sql = "INSERT INTO testtable (text, price) VALUES(?, ?)"; pstm = (PreparedStatement) con.prepareStatement(sql); pstm.setString(1, text); pstm.setInteger(2, text) pstm.execute(); 

我想这是行不通的,因为在你的text有一些标点符号。 它也很容易SQL注入。

你也应该用try-with-resourcesclosures你的可closures的对象,或者finally (如果你坚持java版本的早期版本7),就像在这里做的那样:

https://stackoverflow.com/a/26516076/3323777

编辑 :啊,是的,亚历克斯说,空的catch块。 不要那样做。

编辑2

Apache POI从来没有被devise为调用CSV文件。

https://stackoverflow.com/a/1087984/3323777

Apache还有另一个CSV项目:

http://commons.apache.org/proper/commons-csv/

你看到“导入行”的消息,certificate你实际上有一些行要导入?

也没有看到任何错误的原因可能是一个“捕捉”块什么都不做。 在里面添加任何输出并观察新的结果,即

  System.out.println(e.getMessage()); 

调整从.csv读取

 public class FileExport { 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:3306/test?useSSL=false", "root", "root"); con.setAutoCommit(false); PreparedStatement pstm = null; FileInputStream input = new FileInputStream("/Users/User/Desktop/Email/Test.csv"); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String row = reader.readLine(); String text = row.split(";")[0]; String price = row.split(";")[1]; reader.close(); String sql = "INSERT INTO testtable (text, price) VALUES('" + text + "','" + price + "')"; 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) { } 

}

尝试使用'poi-ooxml'创buildWorkbook对象,其gradle依赖签名提供如下:

 compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.14' 

下面的代码可能会帮助你

 InputStream inputStream = new FileInputStream("/Users/User/Desktop/Email/Test.xls"); XSSFWorkbook wb = new XSSFWorkbook(inputStream); XSSFSheet sheet = wb.getSheetAt(0);