Some time ago, the customer asked our system to exchange data with ms excel and import and export the data in the system to excel. our system is a B/s structure MIS system made of java. to solve this problem
I found a jxl open-source project dedicated to excel, and found a good article about how to use the jxl development kit:
Http://www-900.cn.ibm.com/developerWorks/cn/java/l-javaExcel/#2
Based on Customer requirements, jxl is used to implement the following functions:
1. read data from excel files on the client;
2: Export System data to an excel template file.
For the first problem, I started to consider writing an applet that runs on the client. later, I realized that for the sake of security, the applet cannot access the client's file system at Will (low-level error! Do not despise me ).
So I thought of uploading an excel file to the server and then parsing the data using jxl. I will not talk about how to upload files. Anyone who has done jsp (the first choice for SUN Enterprise Applications) should know it. this section describes how to resolve the issue. because the uploaded data is both
There is a fixed format, so we made an excel template, the customer must fill in the data according to the template format, the program can be normal resolution.
Resolution is actually very simple, but in the process of doing it, we found that the time format data in excel requires special attention: If you read the time format data according to the text format, the read time is incorrect (you can try it ).
To solve the problem of incorrect reading time, I carefully studied the jxl api and found a good stuff: DateCell class, the getDate method can be used to obtain the date object of a cell. however, because
For internationalization, the date object obtains the Greenwich Mean Time. You need to handle it.
As for export, it is almost the same as import. It is nothing more than accessing the database first, writing the obtained information into an excel file, and then downloading it.
The following is the java class I wrote for importing and exporting the weekly production plan. The first few lines are commented out as examples of downloading and uploading.
(In fact, the template is also very important. The following program is written based on the template. However, I believe you can understand the template after reading the following program. It should be noted that, in the template, the time field must be set
Format ----- in excel, right-click the menu to easily solve the problem)
/*
* Create Date:
* Create By: Li chunlei
* Purpose: import and export operations related to weekly plans
*/
/********************* Weekly production download example ************* ****
ExcelZJHXM myzxm = new ExcelZJHXM ();
MyZJH. download ("mydataZjh", "20041018003 ");
**************************************** ************/
/********************* Weekly scheduled upload example ************* ****
ExcelZJHXM test = new ExcelZJHXM ();
Test. upload ("zscjhtest ");
If (test. getFlag ()){
System. out. println ("begin test --------------");
ArrayList a = test. getData ();
For (int I = 0; I <a. size (); I ++ ){
ExcelZJHXM. dataZJHXM mydata = (ExcelZJHXM. dataZJHXM) a. get (I );
System. out. println (mydata. XH + "" + mydata. XMMC + "" + mydata. GZNR + "" + mydata. SFTD + "" + mydata. TDFW + "" + mydata. JHLRFLBH + "" + mydata. JHLRBH + "" + mydata. ZXDWMC + "" + mydata. PHDWMC + "" + mydata. DBRMC + "" + mydata. JHKSSJ + "" + mydata. JHWCSJ + "" + mydata. BZ );
}