前台Extjs:
{xtype : 'panel',
border:0,
items : [
{
xtype : 'form',
itemId: 'datasourceId',
bodyPadding: 5,
title : '数据源',
layout: {
type:'hbox',
padding:'3'
},
items : [
{
xtype: 'fileuploadfield',
fileUpload: true,//chrome浏览器 必须设置为true
width:560,
labelAlign: 'right',
labelWidth: 60,
id:'upFile',
name: 'upFile',
fieldLabel: '*文件名',
labelStyle : 'color:red;',
allowBlank: false,
emptyText:'请选择xls或者xlsx格式的文件',
buttonText: '选择文件',
regex:/\.xls$|\.xlsx$/,
invalidText :'文件格式不正确'
},
{
xtype: 'button',
itemId: 'parseFile',
text: '解析文件',
handler: 'analysisFile'
}
]
}
]}
前台Controller:
/**
* 上传文件
* @param btn
* @returns {boolean}
*/
analysisFile: function(btn) {
var me = btn.up('createfondsimport');
var myform=me.down('form[itemId="datasourceId"]');
var form = myform.getForm();
if(form.isValid()){
var path = form.getValues().upFile;
var exp = '\.xls$|\.xlsx$';
console.log("path: "+path);
var patt = new RegExp(exp);
if(!patt.test(path)){
Ext.Msg.alert('系统提示','导入文件必须是xls文件或者xlsx!!!');
return ;
}
myform.getEl().mask("正在解析文件,请等待...");
form.submit({
url : 'mvc/dispatch',
timeout: 100000,
method:'post',
params : {
action:'FondAction',
method:'analysisExcel'
},
success : function(response){
var text = response.responseText.result;
console.log(response);
console.log(text);
},
failure : function(response){
myform.getEl().unmask();
Ext.Msg.alert('系统提示','操作失败!');
return ;
}
});
}
}
后台Action:
@Controller("FondAction")
public class FondAction(){
@RequestMapping(value = "/analysisExcel")
public void upLoad(HttpServletRequest request,HttpServletResponse response) throws IOException {
MultipartFile file = ((MultipartHttpServletRequest)request).getFile("upFile");
System.out.println("file : \n"+ file);
System.out.println("file size: \n"+file.getSize());
System.out.println("file Name : \n"+file.getName());
System.out.println("file ContentType : \n"+file.getContentType());
System.out.println("file OriginalFilename : \n"+file.getOriginalFilename());
InputStream fs = null;
HSSFWorkbook workbook = null;
try {
fs = file.getInputStream();
workbook = new HSSFWorkbook(fs);
log.debug("xssf workbook: \n"+workbook);
} catch (IOException e) {
system.out.println("******** 异常发生在FondAction.analysisExcel获取文件流时");
e.printStackTrace();
}
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell(0);
String value = cell.getStringCellValue();
System.out.println("*********************value: \n"+value);
// 清空response
response.reset();
// 设置response的content type
response.setContentType("text/html;charset=utf-8");
reponse.getWriter().write(value);
}
}