Considerations for reading Excel files using Java

Source: Internet
Author: User

Recently, I am working on a Java Excel Reading Project. The ultimate goal is to create a general component for reading an Excel file and importing it to a formal table in the database. It can be read in no matter what business scenario or the content of an Excel file. It must be displayed on the page and imported to the formal database table, you can also provide information about whether the import result is successful or not.

Three methods are taken into account when reading the Excel display:

1. Use the page contenttype attribute of JSP to implement:

Tomcat/CONF/Web. XML is added below:
<Mime-mapping>
<Extension> XLS </extension>
<Mime-type> application/MSExcel </mime-type>
</Mime-mapping>
Add the following to the file header to be exported:
<% @ PageContenttype= "Application/vnd. MS-excel" %>
Similar to the following code: Show. jsp

<%
Filename = request. getparameter ("FILENAME ");
Response. sendredirect ("../xxxx/" + filename + ". xls"); // "../xxxx/" file path
%>

2. Use the Microsoft ODBC driver that comes with JDK to directly read Excel files

String Tx = "";
Try {
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");

String url = "JDBC: ODBC: Driver = {Microsoft Excel Driver (*. xls)}; DBQ = D: // javatest // test.xls ";

Connection conn = drivermanager. getconnection (URL );

Statement stmt = conn. createstatement ();

Resultset rs = stmt.exe cutequery ("select * from [sheet1 $]");

// Display a row at a time. The total number of records in the RS record set is the number of objects. By default, the first row is not read as the title.
While (Rs. Next ()){
Tx + = Rs. getstring (1) + "/n ";
Tx + = Rs. getstring (2) + "/n ";
Tx + = Rs. getstring (3) + "/n ";
}

System. Out. println (TX );

} Catch (exception e ){
E. printstacktrace ();
}

3. Use jxl. jar, a third-party component, to read files row by row and save them to the memory. A two-dimensional array linked list is used to store read data. A two-dimensional array stores one row of data, and the number of elements in the linked list is the total number of lines in the file. This method may occupy memory and needs further test and verification.

After the display is complete, if you are allowed to modify the data on the page, it is also important to consider how to save the modified value. You are welcome to provide better suggestions! Haha ~~~~~~~~~~~~~

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.