Batchlook.jsp code: <form name= "Form1" action= "uploadtoserver.jsp" method= "post" enctype= "Multipart/form-data" >//must add Enctype= "Multipart/form-data" Upload excel<input type= "file" Name= "TEST" size= "" id= "Upfile" > < Input type= "submit" value= "upload" > </form>
uploadtoserver.jsp Code <%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> <%@ page import= " com.upload.jspsmart.upload.* "%> <jsp:usebean id=" mysmartupload "scope=" page "class=" Com.upload.jspsmart.upload.SmartUpload "/> <% @page import=" SMS.BATCH.FILEPREVIEWMSG "%> <% @page import=" Java.util.ArrayList "%> <% @page import=" Sms.batch.BatchLook "%> <% @page import=" Sms.batch.BatchLookDao "% > <% @page import= "java.util.Date"%> <% @page import= "java.util.List"%> <% @page import= " Java.text.SimpleDateFormat "%> <% @page import= java.io.PrintWriter"%> <% mysmartupload.initialize ( PageContext); Upload file Initialization mysmartupload.upload ()//upload file String path=pagecontext.getservletcontext (). Getrealpath (""); Path=path.substring (0,path.lastindexof ("//")); path=path+ "//data//sendfiles//"//Upload file path com.upload.jspsmart.upload.File myFile = Mysmartupload.getfiles (). GetFile (0)//Get uploaded file String filename=myfile.getfilename (); Java.text.SimpleDateFormat SimpleDateFormat =New Java.text.SimpleDateFormat ("Yyyymm");//date conversion, used to name the file String strdate = Simpledateformat.format (new date ());// Name the file with a date filename = filename.substring (Filename.lastindexof ("."), Filename.length ()); Filename=strdate+filename; Java.io.File File = new Java.io.File (path);//Create a newly created directory on the server if (!file.isdirectory ()) {file.mkdirs ();} path= path + filename;// The full path of the file if (file. Exists ()) {myfile.saveas (path);//Save the uploaded file to the server filepreviewmsg filepreviewmsg = new filepreviewmsg ();// JXL object pairs to parse Excel ArrayList list = filepreviewmsg.getpreviewmsglist (path);//parse Excel Batchlook batchlook;//each line is a Kanban object// Delete the Batchlook = (batchlook) list.get (0) that was imported before; Batchlookdao.save (Batchlook)//database operations, saving to database}%>
Filepreviewmsg.java code: Package sms.batch; Import java.io.*; Import Java.net.Authenticator; Import java.net.HttpURLConnection; Import Java.net.URL; Import Java.text.SimpleDateFormat; Import java.util.ArrayList; Import Java.util.Vector; Import Com.common.GlobalValueManager; Import jxl.*; Import jxl.format.Alignment; Import Jxl.format.Colour; Import Jxl.format.UnderlineStyle; Import jxl.format.VerticalAlignment; Import jxl.read.biff.BiffException; Import Jxl.write.Label; Import Jxl.write.WritableCellFormat; Import Jxl.write.WritableFont; Import Jxl.write.WritableSheet; Import Jxl.write.WritableWorkbook; Import jxl.write.WriteException; Import jxl.write.biff.RowsExceededException; * * Author Lzf by 2010-10-21 * JXL read upload Excel content */public class Filepreviewmsg {//Read Excel public ArrayList Getpreviewmsgli St (String filename) {System.out.println (filename); ArrayList list = new ArrayList (); SimpleDateFormat SDF = new SimpleDateFormat ("dd/mm/yyyy"); SimpleDateFormat sdf1 = new SimpleDateFormat ("yyYy-mm-dd "); try {Workbook book = Workbook.getworkbook (New File (FileName)); Sheet Sheet = book.getsheet (0); Get the first worksheet object int rows = Sheet.getrows (); Batchlook Batchlook; for (int i = 0; i < rows-1 i++) {cell[] Cell = Sheet.getrow (i); batchlook = new Batchlook (); if (cell.length = 0) {continue;} if (Sheet.getcell (0, 0). getcontents (). Trim (). Contains ("date")) {Batchlook.setbatch_look_date (Sdf1.format ( Sheet.getcell (0, i+1). getcontents (). Trim ())); } if (Sheet.getcell (1, 0). getcontents (). Trim (). CONTAINS ("forecast demand")) {Batchlook.setbefor_need ( Sheet.getcell (1, i+1). getcontents (). Trim ()); } if (Sheet.getcell (2, 0). getcontents (). Trim (). Contains ("undertake demand") {Batchlook.setcontinue_need ( Sheet.getcell (2, i+1). getcontents (). Trim ()); } if (Sheet.getcell (3, 0). getcontents (). Trim (). Contains ("ability to undertake") {batchlook.setcontinue_ability ( Sheet.getcell (3, i+1). getcontents (). Trim ()); } if (Sheet.getcell (4, 0). getcontents (). Trim (). Contains ("Remaining resources") {BatChlook.setleave_resource (Integer.parseint (Sheet.getcell (4, i+1). getcontents (). Trim ()); } list.add (Batchlook); The catch (Biffexception e) {System.out.println ("Excel does not conform to format"); E.printstacktrace ();} catch (IOException e) {System.out . println ("Excel does not conform to format"); E.printstacktrace (); }catch (Exception ex) {ex.printstacktrace ();} return list; public void Exportexcel (string filename,arraylist content) {//String fileName = ' D://myexcel2.xls '; Writableworkbook WWB; FileOutputStream Fos; try {fos = new FileOutputStream (fileName); WWB = Workbook.createworkbook (FOS); Writablesheet ws = Wwb.createsheet ("Sheet1", 100); Create a worksheet//set the text format for a cell writablefont wf = new Writablefont (Writablefont.arial,12,writablefont.no_bold,false, Underlinestyle.no_underline,colour.blue); Writablecellformat WCF = new Writablecellformat (WF); Wcf.setverticalalignment (Verticalalignment.centre); Wcf.setalignment (Alignment.centre); ArrayList list; for (int i=0;i<content.size (); i++) {list = (ArrayList) content.get (i); if(i = = 0) WCF = new Writablecellformat (); for (int j=0;j<list.size (); j + +) {Ws.addcell (new Label (J,i, List.get (j). ToString (), WCF));} Wwb.write (); Wwb.close (); catch (IOException e) {} catch (Rowsexceededexception e) {} catch (WriteException e) {}}}
Batchlook.javapackage Sms.batch; /** Record Monthly Resource area * @author LZF * * */public class Batchlook {private int batch_look_id;//mass Item number private int ent_id;//Enterprise ID priv Ate String batch_look_date;//date private int befor_need;//predictive demand private int continue_need;//to undertake demand private int Continue_abili ty;//ability to undertake private int leave_resource;/remaining resources private string resource_vlaue;//resource felling value public string Getresource_vlaue () {Retu RN Resource_vlaue; Public String Getresource_vlaue_color () {if (Resource_vlaue = = NULL | | resource_vlaue.equals (")) {return" color: #FF9 933 "; }else if (float.parsefloat (Resource_vlaue) < 0) {return "color:red";} else if (float.parsefloat (Resource_vlaue) >1000) {return "Color:green";} else if (float.parsefloat (Resource_vlaue) >=0 && float.parsefloat (Resource_vlaue) <100) {return "COLOR: # FF9933 "; }else{return "";} public void Setresource_vlaue (String resourcevlaue) {Resource_vlaue = Resourcevlaue.} public int getbatch_look_id () {RE Turn batch_look_id; } public voidsetbatch_look_id (int batchlookid) {batch_look_id = batchlookid;} public int getent_id () {return ent_id;} public void S etent_id (int entid) {ent_id = Entid;} public String getbatch_look_date () {return batch_look_date;} public void SETBATC H_look_date (String batchlookdate) {batch_look_date = batchlookdate;} public int getbefor_need () {return befor_need;} p ublic void setbefor_need (int beforneed) {befor_need = beforneed;} public int getcontinue_need () {return continue_need; public void setcontinue_need (int continueneed) {continue_need = continueneed.} public int getcontinue_ability () {Retu RN continue_ability; public void setcontinue_ability (int continueability) {continue_ability = continueability;} public int Getleave_resourc E () {return leave_resource.} public void Setleave_resource (int leaveresource) {leave_resource = Leaveresource;}}