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 ~~~~~~~~~~~~~