Java does not rely on reading Excel files

Source: Internet
Author: User

When it comes to Java reading Excel files, with a lot of of course is poi or jxls, but today in reading a book, which refers to the use of Jdbcodbcdriver this driver class without relying on third-party libraries can also complete the Excel file read operations, Search the Internet for a reason and then wrote an example of running through, in this record.

Java read database Everyone is familiar with the need for a data source and the corresponding driver, the developer through the JDBC operation driver, the driver to operate the database, then Java read the Excel file is similar, in the Windows system, You can register an Excel file as an ODBC data source, and the registration process is:
A. Control Panel > Administrative Tools > Data Sources (ODBC)

B. In the User DSN tab, click Add, select Driver do Microsoft Excel (*.xls)


c. Click Finish, fill in the data source name, and finally select the book, that is, select the Excel file address as the data source
Where the data source name is most important, it acts as part of the URL that gets the connection.

After registering the data source, you can write the code, an example is as follows:

public class Excelreader {private String ENTRY;//ODBC data source name public Excelreader (String entry) {this.entry = entry;} SheetName is the sheet name public String read (string sheetname) throws Exception {StringBuilder builder = new StringBuilder (); Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver"). newinstance (); Connection Connection = Drivermanager.getconnection ("JDBC:ODBC:" + entry); Statement Statement = Connection.createstatement (); ResultSet rs = statement.executequery ("SELECT * FROM [" + SheetName + "$]"), while (Rs.next ()) {Builder.append (rs.getobject (1)); /schematic read only one column of}rs.close (); Statement.close (); Connection.close (); return builder.tostring (); public static void Main (string[] args) throws Exception {Excelreader reader = new Excelreader ("ETL"); System.out.println (Reader.read ("Test"));}}

the principle is that the development all through the JDBC Operation Jdbcodbcdriver Drive, the Jdbcodbcdriver drive connects the ODBC data source, then the ODBC operation connects the Excel file (Windows completes), The purpose of reading Excel files is to complete the step by step adaptation.

Java does not rely on reading Excel files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.