Flex 导入
前端代码 //浏览按钮,选择文件 private function
btnImport_clickHandler(event:MouseEvent):void {
//过滤文件
var fileTypes:Array = new Array(fileType);
//导入
fileRefer.addEventListener(Event.SELECT,parseExcel);
fileRefer.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,upload_resultHandler);
fileRefer.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
fileRefer.browse(fileTypes); } /**
* 文件异常处理
/ private function ioErrorHandler(e:IOErrorEvent):void {
Alert.show(“文件异常:”+e.text,MessageConst.ALERT_TITLE,null,windows,null,Alert.ICON_WARNING);
} private function parseExcel(evt:Event):void {
//调用Servlet
var url:String = ExternalInterface.call(“window.location.href.toString”);
url = url.substring(0,url.indexOf(“bip”));
//controller
var request:URLRequest=new URLRequest(url +“bip/importExcelFeedback/importExcel”);
request.contentType = “multipart/form-data”;
request.method = URLRequestMethod.POST;
fileRefer.upload(request,“multipart/form-data”,true); } /*
* 上传结果返回处理
*/ private function upload_resultHandler(e:DataEvent):void {
var say:Object = JSON.parse(e.data);
if(say.code == GooidServerSay.CODE_ERROR)
{
Alert.show(“上传失败!”);
return;
}
Alert.show(“上传成功!”);
btnFind_clickHandler();
}
/**
* 导入excel、xls格式文件
* @param request
* @param response
* @return
* @throws Exception
*/
@SuppressWarnings("rawtypes")
@RequestMapping(value = "importExcel", method = RequestMethod.POST)
@ResponseBody
public String exportExcel(HttpServletRequest request ,HttpServletResponse response) throws Exception
{
Map<String,Object> map = new HashMap<String,Object>();
map.put("code","success");
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUp = new ServletFileUpload(factory);
fileUp.setSizeMax(1024 * 1025 * 1024);
List items = fileUp.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String msg=importExcelFeedbackService.getExcel(item);
map.put("msg",msg);
}
}
} catch(Exception e) {
log.error("ImportExcelFeedbackService.exportExcel", e);
map.put("code",CODE_ERROR);
map.put("msg",MessageUtils.build(e));
}
return JSON.stringify(map);
}
public String getExcel(FileItem item) throws Exception{
try {
Boolean flag=true;
String fileName=item.getName();
if(fileName.endsWith(xls)){
flag=true;
}else if(fileName.endsWith(xlsx)){
flag=false;
}
HSSFSheet sheet = null;
Integer count=null;
HSSFWorkbook workbook=null;
XSSFWorkbook xssfWorkBook=null;
XSSFSheet xssfSheet=null;
if(flag){
workbook = new HSSFWorkbook(item.getInputStream());
}else{
xssfWorkBook=new XSSFWorkbook(OPCPackage.open(item.getInputStream()));
}
for(int i = 0;i < (flag?workbook.getNumberOfSheets():xssfWorkBook.getNumberOfSheets());i++){
//获取每个sheet
//getPhysicalNumberOfRows获取有记录的行数
if(flag){
sheet = workbook.getSheetAt(i);
count=sheet.getPhysicalNumberOfRows();
}else{
xssfSheet= xssfWorkBook.getSheetAt(i);
count=xssfSheet.getPhysicalNumberOfRows();
}
for(int j = 0;j < count;j++){
Row row =(flag?sheet.getRow(j):xssfSheet.getRow(j));
if(null!=row&&j>=1){
for(int k = 0;k < row.getLastCellNum();k++){
if(null!=row.getCell(k)){
Cell cell = row.getCell(1); //
String numericCellValue =cell.getStringCellValue();
}
}
}
}
}
log.debug("读取sheet表:"+ (flag?workbook.getSheetName(i):xssfWorkBook.getSheetName(i)) + "完成,记录总数"+(count-1));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
此后台方法适用于xls及xlsx格式的excel