功能要求:
在mastplan頁面添加按鈕,實現上傳excel文件的功能,並在保存之前讀取文件內容將其保存到數據庫裏面。
具體實現:
^_^ 先閃閃圖片吧。
Jsp頁面代碼:
在tbar工具欄中添加一按鈕: 單擊此按鈕,彈出一個上傳文件的窗口
,{id:"upload_excel",xtype: "button",cls:"x-btn-icon",icon:"/_static/icon/upload_excel.png",tooltip:"upload",scope:this,handler: function(){win_upload.show();}}
var form = new Ext.form.FormPanel({
baseCls : 'x-plain',
labelWidth : 70,
labelHeight: 150,
fileUpload : true,
defaultType : 'textfield',
items : [{
xtype : 'textfield',
fieldLabel : '選擇文件',
name : 'userfile',
id : 'userfile',
inputType : 'file',
anchor : '95%' // anchor width by percentage
}]
});
var win_upload = new Ext.Window({
title : 'UploadFile',
width :450,
height : 180,
modal : true,
x : 100,
y : 50,
layout : 'form',
autoScroll : true,
constrain : true,
bodyStyle : 'padding:10px 10px 10px 10px;',
items:form,
buttons : [{
text : '確認上傳',
handler : function() {
if (form.form.isValid()) {
if(Ext.getCmp('userfile').getValue() == ''){
Ext.Msg.alert('溫馨提示','請選擇要上傳的文件');
return;
}
Ext.MessageBox.show({
title : '請稍後....',
msg : '文件正在上傳中....',
progressText : '',
width : 300,
progress : true,
closable : false,
animEl : 'loding'
});
form.getForm().submit({
url : 'upload',
method : 'POST',
success : function(form, action) {
Ext.Msg.alert('成功','恭喜!文件上傳成功!'+action.result.success);
win_upload.hide();
},
failure : function(form, action) {
Ext.Msg.alert('錯誤',"文件上傳失敗,請重新操作!");
}
})
}
}
}, {
text : 'Close',
handler : function() {
win_upload.hide();
}
}],
closable: false,
draggable: false,
resizable: false
});
Java代碼:
在保存文件之前獲取流信息,並將其讀出。
獲取excel內容
package javaservlets.production.uploadexcel;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.json.JSONException;
import org.json.JSONObject;
public class Upload extends HttpServlet {
private static final long serialVersionUID = 6777945010008132796L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
System.out.println("doGet");
resp.sendRedirect("/upload/index.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("--------------------------------- upload ---------------------------------------------------");
InputStream is = null;
Workbook workbook = null;
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
for (FileItem item : items) {
if (item.isFormField()) {
System.out.println(item.getFieldName());
System.out.println(item.getString());
} else {
System.out.println(item.getFieldName());
is = item.getInputStream();
workbook = Workbook.getWorkbook(is);
}
}
Sheet[] sheetNum = workbook.getSheets();
System.out.println("----------------------------------打印sheet的個數:"+sheetNum.length+"----------------------------");
Sheet sheet = workbook.getSheet(0);
Cell cell = null;
int columnCount = sheet.getColumns();
int rowCount = sheet.getRows();
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
cell = sheet.getCell(j, i);
System.out.print(cell.getContents());
}
System.out.println(" \n");
}
resp.setContentType("text/html");
JSONObject jObject = new JSONObject();
try {
jObject.put("success", "true");
} catch (JSONException e) {
e.printStackTrace();
try {
jObject.put("success", "false");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
resp.getWriter().print(jObject.toString());
// resp.sendRedirect("/upload/index.jsp");
workbook.close();
} catch (FileUploadException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}