页面1:
<!DOCTYPE html>
<html>
<head>
<title>demo7.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="../css/css.css">
<link rel="stylesheet" type="text/css" href="../css/easyui.css">
<link rel="stylesheet" type="text/css" href="../css/icon.css">
<link rel="stylesheet" type="text/css" href="../css/uploadify.css">
<script type="text/javascript" src="../js/jquery.min.js"></script>
<!-- <script type="text/javascript" src="../js/jquery-1.4.4.min.js"></script> --> //这个 jquery 不好
<script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../js/jquery.uploadify.min.js"></script>
<script type="text/javascript" src="../js/easyui-lang-zh_CN.js"></script>
</head>
<script type="text/javascript">
function importPointExcel(){
var $win = $("#win").window({
title: '点表文件',
width: 380,
height: 80,
modal: true,
closed: true,
minimizable: false,
maximizable: false,
collapsible: false,
onLoad: function() {
$("#upload").click(function () {
$.messager.progress({
title: '提示',
msg: '正在努力处理中,请稍后...'
})
var formData = new FormData($('#uploadFile')[0]);
$.ajax({
type: 'post',
url: "../demo6/imp",
data: formData,
cache: false,
processData: false,
contentType: false,
}).success(function (data) {
alert(data);
if (true) {
$.messager.progress('close');
$.messager.alert('提示', "导入模型成功!");
} else {
$.messager.progress('close');
$.messager.alert('提示', "格式错误!导入失败!");
}
}).error(function () {
$.messager.progress('close');
alert("上传失败");
});
});
}
})
$win.window('open').window('refresh',"/demo8.html");
}
</script>
<body>
<button οnclick="importPointExcel()">点点</button>
<div id="win"></div>
</body>
</html>
页面2:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="multipart/form-data; charset=UTF-8">
<title>点表上传</title>
</head>
<body>
<div style="margin-left: 45px; margin-top: 10px">
<form id="uploadFile" enctype="multipart/form-data">
<input type="file" name="file" id="file" accept=".xls">
<a id="upload" class="easyui-linkbutton" data-options="iconCls:'icon-export'">提交</a>
</form>
</div>
</body>
</html>
controller .java
package com.wizpower;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping("demo6")
public class Demo6Controller {
@RequestMapping(value = "/test", method = RequestMethod.POST)
@ResponseBody
public String test(HttpServletRequest request){
System.err.println("----------------test");
return "map";
}
@RequestMapping(value = "/imp", method = RequestMethod.POST)
@ResponseBody
public String func(@RequestParam(value="file",required=false) MultipartFile file ){
System.out.println("-------111111111----------");
try {
InputStream is = file.getInputStream();
Workbook book = new XSSFWorkbook(is);
Sheet firstSheet = book.getSheetAt(0);
Row firstRow = firstSheet.getRow(0);
System.out.println("行数=="+firstSheet.getLastRowNum());//行的index
System.out.println("列数=="+firstRow.getLastCellNum());
String value = firstRow.getCell(0).getStringCellValue();
System.out.println("value=="+value);
for( int j=0 ;j<= firstSheet.getLastRowNum() ;j++){
Row rowObject = firstSheet.getRow(j);
for(int i = 0 ;i< firstRow.getLastCellNum(); i++){
Cell cellObject = rowObject.getCell(i);
cellObject.setCellType(Cell.CELL_TYPE_STRING);
String text = cellObject.getStringCellValue();
System.out.println("第"+j+1+"行数据:"+text+"==");
}
}
} catch (IOException e) {
System.err.println("无-------------");
}
System.out.println("-------2222222222----------");
return "true" ;
}
}