简体   繁体   English

从.xls文件中提取数据

[英]extract data from .xls file

我想从Excel文件中提取数据并将其存储到Access数据库中,该怎么做?

The Apache POI library provides access to Microsoft Office formats, including Excel. Apache POI库提供对Microsoft Office格式(包括Excel)的访问。

To insert into a database, you'll need JDBC (and possibly additional frameworks if you want an ORM). 要插入数据库,您将需要JDBC(如果需要ORM,可能还需要其他框架)。 Here's a tutorial on JDBC to get you started. 这里有一个教程上JDBC让你开始。 You may want to check out Hibernate as a useful Java ORM. 您可能想将Hibernate视作有用的Java ORM。

You can use the Java Excel API to extract the data. 您可以使用Java Excel API提取数据。 You can find the javadoc here . 您可以在此处找到javadoc。 There is also a programmer's guide on how to use JExcel 还有关于如何使用JExcel程序员指南

First, do you really need Java for this? 首先,您真的需要Java吗? The MSAccess software itself provides functionality to import data from an Excel file. MSAccess软件本身提供了从Excel文件导入数据的功能。 It's much easier to do so. 这样做要容易得多。

If you really need Java for this, then you need to realize that this is in fact a two-step task: 如果您确实需要Java,那么您需要意识到这实际上是一个两步任务:

  1. Extract data from Excel into Java objects (eg List<List<String>> or List<Data> ). 将数据从Excel提取到Java对象(例如List<List<String>>List<Data> )。
  2. Save data from those Java objects into MSAccess. 将这些Java对象中的数据保存到MSAccess中。

For step 1 you need a Java API which is capable to extract data from an Excel file. 对于步骤1,您需要一个Java API,该API能够从Excel文件中提取数据。 Which one to use depends on the actual file format. 使用哪种格式取决于实际的文件格式。

If it is a .xls file, you have in general two options: the aforementioned Apache POI HSSF and JExcelAPI. 如果它是.xls文件,则通常有两个选择:前面提到的Apache POI HSSF和JExcelAPI。 The POI HSSF is known to be memory hogging and its API is a bit opaque in use. 已知POI HSSF占用内存,并且其API在使用中有点不透明。 Andy Khan's JExcelAPI is the better choice. Andy Khan的JExcelAPI是更好的选择。

If it is a .xlsx file (the last x indicates that it's in OpenXML format instead of binary format), then you have next to the aforementioned Apache POI XSSF also the choice to to use OpenXML4J , which is -again- generally a better choice than the POI XSSF for the same reasons as the POI HSSF. 如果它是.xlsx文件(最后一个x表示它是OpenXML格式而不是二进制格式),则在前面提到的Apache POI XSSF旁边还有使用OpenXML4J的选择,这通常也是一个更好的选择的原因与POI HSSF相同。

Now, for step 2 you need a Java API which can save data into a MSAccess file. 现在,对于第2步,您需要一个Java API,该API可以将数据保存到MSAccess文件中。 You can use the JDBC API for this in combination with the builtin JDBC-ODBC bridge driver. 可以将JDBC API与内置JDBC-ODBC桥驱动程序结合使用。 A good starting point is the JDBC tutorial . JDBC教程是一个很好的起点。

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

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