java大数据量循环Excel解析入库

   前端页面部分

<style>
.buton{
width: 100px;
    height: 24px;
    color: white;
    background-color: #00b8f5;
    border: 1.5px white solid;
    font-size: 16px;
}
</style>
<script>
$(function(){
var h = $(window).height() - $('#frm1').height();
});

var deal = function (){
var len = $('#leaf').val() ;
var index = len.lastIndexOf(".");
var etx = len.substring(index + 1,len.length);
if(len == ''){
$.dialog.alert('请选择文件!');
return ;
}else if(etx != 'xlsx'){
$.dialog.alert('请上传.xlsx文件类型!');
return ;
}else{
$("#tab").show();
$("#daoru").hide();

$('#frm1').form('submit', {
async: false,
dataType:'json',
type:'text',
url:'<%=path%>/leafdr.do?method=peopleExcel',
success:function(data){
console.log(data);
$("#guocheng").show();
$("#tab").hide();
$.dialog.alert(data);
},
error : function(data) {
console.log(data);
$.dialog.alert(data);
}  
});
}
};

 function sxDeal(){
$.ajax({
url:'<%=path%>/leafdr.do?method=sxDeal',
dataType:'json',
type:'post',
success:function(data){
var msg = data[0];
$.dialog.alert(msg);
$("#guocheng").hide();
},
error : function(data) {
var msg = data[0];
$.dialog.alert(msg);
}  
});

</script>

<body>

<form id="frm1" name="frm1" action="" method="post" enctype="multipart/form-data" >
<table>
<tr>
<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td> <input id="leaf" type="file" name="excel" class="multi"  /></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td><input id="daoru" type="button" value="导入" onclick="deal()" class="buton"></td>
<td><input id="guocheng" type="button" value="数据处理" hidden="hidden" onclick="sxDeal()" class="buton"></td>
</tr>
</table>
</form>
<div>
<input id="tab" value="正在导入,请稍后......" hidden="hidden" readonly="readonly" style="margin-left:  20px;color: red;margin-top:  10px;"></input>
</div>
</body>
</html>

   后台JAVA部分

       /**

* @Title: peopleExcel
* @Description: TODO(这里用一句话描述这个方法的作用)
* @author Nobody
* @date 2018年3月14日 下午8:34:06
* @param actionMapping
* @param actionForm
* @param request
* @param response
* @throws Exception
* @throws

*/

Action层:

public void peopleExcel(ActionMapping actionMapping,
ActionForm actionForm, HttpServletRequest request,
HttpServletResponse response) throws Exception{
SysUserVO userInfo = ResourceUtil.getSessionUserName(request);
    String userId = userInfo.getUserId();
Map<String, String> map =new HashMap<String, String>();
HttpSession session = request.getSession();
FileItemFactory dfif = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(dfif);
List<FileItem> list = null;
String msg = "";

try {
list = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
msg = e.getMessage();
this.printStr(response, msg);
}
try {
FileItem fii = null;
for (Iterator<FileItem> it = list.iterator(); it.hasNext();) {
FileItem fi = it.next();
if (fi.isFormField()) {
map.put(fi.getFieldName(), fi.getString("UTF-8"));
} else {
fii = fi;
}}
    Workbook wb;
Sheet sheet;
Row row;
wb = WorkbookFactory.create(fii.getInputStream());
sheet = wb.getSheetAt(0);
int rowNum = sheet.getLastRowNum()+1;
row = sheet.getRow(0);
Cell cell = null;
List<String> name= new ArrayList<String>();
Map<String,String> res = new HashMap<String,String>();
List<Map<String,String>> allResult = new ArrayList<Map<String,String>>();
for (int i = 0 ;i<1 ;i++){
cell = row.getCell(i);
name.add(ExcelReader.getStringCellValue(cell).trim());
}

int sheetLen = rowNum / 2000 + 1;

for (int i = 0; i < sheetLen; i++) {

int rowLen = rowNum < 2000 ? rowNum-1 : 2000;
rowNum -= 2000;

for (int j = 1; j <= rowLen; j++) {

res = new HashMap<String,String>();
row = sheet.getRow(2000*i + j);
for(int q=0; q<1;q++){
String na = "na" + q ;
String val = "val" + q;
na = (String) name.get(q);
val = ExcelReader.getStringCellValue(row.getCell(q)).trim();
res.put(na, val);
}
allResult.add(res);
}
if(allResult.size()>0){
msg = service.peopleExcel(allResult,userInfo);
allResult.clear();
}

}
this.printStr(response, msg);

} catch (Exception er) {
er.printStackTrace();
msg = er.getMessage();
this.printStr(response, msg);
}

}

service层

public String peopleExcel(List<Map<String, String>> allResult,

SysUserVO userInfo) {
String msg = "导入成功!";
TransManager tm = new TransManager();
JdbcTemplate jdbcTemplate = tm.getJdbcTemplate();
try{
dao.peopleExcel(allResult,jdbcTemplate,userInfo);
tm.commit();
}catch(Exception e){
tm.rollback();
msg = e.getMessage();
e.printStackTrace();
}
System.out.println("MSG=================:"+msg);
return msg;
}

Dao层:

public void peopleExcel(List<Map<String, String>> allResult,

JdbcTemplate jdbcTemplate, SysUserVO userInfo) {
int count = 0;
String userId = userInfo.getUserId();
String userName = userInfo.getName();
int len = allResult.size();
String[] sql = new String[len];
String resul = null;
String Name = null;
for (int i = 0; i < len; i++) {
Name = (String) ((Map<?, ?>) (allResult.get(i))).get("用户名称") == null ? "" :
(String) ((Map<?, ?>) (allResult.get(i))).get("用户名称");


sql[i] = " insert into  table_temp(用户名称  ) VALUES ('" + Name + "' )   ";
}

jdbcTemplate.batchUpdate(sql);


}




猜你喜欢

转载自blog.csdn.net/niubaolei1993/article/details/79604843