1. Import the Excel file to the database during project development and apply it to jxl. jar for parsing.
2. as follows
- Package test;
- Import java. Io. file;
- Import java. Io. ioexception;
- Import java. Io. unsupportedencodingexception;
- Import javax. servlet. http. httpservletresponse;
- Import jxl. sheet;
- Import jxl. workbook;
- Import jxl. workbooksettings;
- Import jxl. Read. Biff. biffexception;
- Import jxl. Write. label;
- Import jxl. Write. writablesheet;
- Import jxl. Write. writableworkbook;
- Import jxl. Write. writeexception;
- Public class jxltest {
- Public static void main (string [] ARGs ){
- (New jxltest (). excelread ();
- }
- Public void excelread (){
- File file = new file ("D: // cicc825.xls ");
- Try {
- Workbooksettings = new workbooksettings ();
- Workbooksettings. setencoding ("ISO-8859-1"); // key code to address Chinese garbled characters
- Workbook book = Workbook. getworkbook (file, workbooksettings );
- // Workbook book = Workbook. getworkbook (File );
- Sheet sheet = book. getsheet (0 );
- Int ROW = sheet. getrows ();
- Int column = sheet. getcolumns ();
- For (INT I = 0; I <row; I ++ ){
- For (Int J = 0; j <column; j ++ ){
- System. Out. println (sheet. getcell (J, I). getcontents ());
- }
- }
- } Catch (biffexception e ){
- E. printstacktrace ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- }
- Public void excelwrite (httpservletresponse response ){
- Long time = system. currenttimemillis (); // use the current system time as the default table name
- String name = string. valueof (time );
- Name = "position ";
- String pathname = Name + ". xls ";
- Try {
- Pathname = new string (pathname. getbytes (), "ISO-8859-1 ");
- } Catch (unsupportedencodingexception e ){
- E. printstacktrace ();
- }
- // Obtain the Save path and table name in the pop-up save box.
- Response. setcontenttype ("application/OCTET-STREAM ");
- Response. setheader ("content-disposition", "attachment; filename =/" "+ pathname + "/"");
- Writableworkbook workbook;
- Workbooksettings = new workbooksettings ();
- Workbooksettings. setencoding ("gb2312 ");
- Try {
- Workbook = Workbook. createworkbook (response. getoutputstream (), workbooksettings );
- Writablesheet Ws = Workbook. createsheet ("Sheet 1", 0 );
- WS. setcolumnview (0, 22 );
- Label cell = NULL;
- For (INT I = 0; I <2; I ++ ){
- Cell = new label (0, I + 1, "ID ");
- WS. addcell (cell );
- Cell = new label (1, I + 1, "name ");
- WS. addcell (cell );
- }
- Workbook. Write ();
- Workbook. Close (); // be sure to close it; otherwise, no Excel is saved;
- } Catch (ioexception e ){
- E. printstacktrace ();
- } Catch (writeexception e ){
- E. printstacktrace ();
- }
- }
- }
3. The parsing process is relatively simple.