导入excel

页面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" ;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值