spring boot 导入excel

jsp---------------------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传</title>


<script type="text/javascript">
$(function(){
$(function(){


})
})
var opts = {
  lines: 13 // The number of lines to draw
, length: 7// The length of each line
, width: 7 // The line thickness
, radius: 20 // The radius of the inner circle
, scale: 1 // Scales overall size of the spinner
, corners: 1 // Corner roundness (0..1)
, color: '#000' // #rgb or #rrggbb or array of colors
, opacity: 0.25 // Opacity of the lines
, rotate: 0 // The rotation offset
, direction: 1 // 1: clockwise, -1: counterclockwise
, speed: 1 // Rounds per second
, trail: 60 // Afterglow percentage
, fps: 20 // Frames per second when using setTimeout() as a fallback for CSS
, zIndex: 2e9 // The z-index (defaults to 2000000000)
, className: 'spinner' // The CSS class to assign to the spinner
, top: '50%' // Top position relative to parent
, left: '50%' // Left position relative to parent
, shadow: false // Whether to render a shadow
, hwaccel: false // Whether to use hardware acceleration
, position: 'absolute' // Element positioning
}
var target = document.getElementById('foo')
var spinner = new Spinner(opts).spin(target);


function upload(){
console.log($('#foo'));
$('#foo').show();

var filepath = $('#myFile').val();
console.log("filepath:----"+filepath);
  var fileext=filepath.substring(filepath.lastIndexOf("."),filepath.length); 
    fileext=fileext.toLowerCase();
    console.log(fileext);
if(filepath==""){
$.messager.alert({
    title:'提示信息',
    msg:'未选择文件'
  });
   $('#foo').hide();
}else if(fileext!=".xls"){
   $.messager.alert({
    title:'提示信息',
    msg:'请上传excel文件'
    });
    $('#foo').hide();
}else{
$('#form').form('submit', {  
            url:'/jjrgl/importHolidays',  
            dataType: "json",
            type: "post",
            success:function(data){
      
            var jdata = JSON.parse(data);
            console.log(JSON.parse(data));
            console.log(jdata.message);
            if(jdata.success){
            $('#foo').hide();
            $.messager.alert("操作提示", '提交成功!',"warning", function () {
             $wupload.window('close');
    location.reload(true);
    });
            } else{
             $('#foo').hide();
             $.messager.alert({
    title:'提示信息',
    msg:jdata.message
    });
            }
           
            }
            });          
           
    }
}




</script>
</head>
<div id="foo" hidden></div>
<div>
<form   id="form" enctype="multipart/form-data" method="post">
     <div style="margin-top:20px;margin-left:20px;">
<input id="myFile" type="file" name="myFile" />
</div>
<div style="margin-top:40px;margin-left:130px;">
    <a  style="margin-left: 50px;" href="javascript:void(0)" class="easyui-linkbutton"
iconCls="icon-remove" οnclick="upload()";>提交</a>
</div>
</form>
</div>

</html>


jsp 完---------------------------------------------------------------------------------------------------------


controller 层-------------------------------------------------------------------------------------------------


@RequestMapping(value = "/import", method = RequestMethod.POST)

@ResponseBody
public String  importHolidays(@RequestParam("myFile") MultipartFile file,HttpServletRequest request){
Boolean success = false;
String message;
Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
       
        InputStream is = file.getInputStream();
        //得到excel 常用对象
    POIFSFileSystem fs = new POIFSFileSystem(is);
    //得到excel 工作簿对象
    HSSFWorkbook wb = new HSSFWorkbook(fs); 
    //得到 工作表对象
HSSFSheet sheet = wb.getSheetAt(0);
//总行数
int trLength = sheet.getLastRowNum();
//得到行
HSSFRow row = sheet.getRow(0);
//总列数
int tdLength = row.getLastCellNum();
//得到指定单元格
HSSFCell cell = row.getCell(1);
CellStyle cellStyle = cell.getCellStyle();
System.out.println("trLength:--"+trLength);  //2
System.out.println("tdLength:--"+tdLength);  //3
System.out.println("cell:--"+cell);          //结束时间
System.out.println("cellStyle:--"+cellStyle); //org.apache.poi.hssf.usermodel.HSSFCellStyle@6eb82310

for(int i =0;i<=trLength;i++){ 
HSSFRow row1 = sheet.getRow(i);
HolidayBean holiday = new HolidayBean();
for(int j=0;j<tdLength;j++){
HSSFCell cell1 = row1.getCell(j);
if(cell1 != null ){
cell1.setCellType(Cell.CELL_TYPE_STRING);
String content = cell1.getRichStringCellValue().getString();
if(j==0){ //开始时间
holiday.setStartDate(content);
}else if(j==1){
if("yyyy/m/d".equals(cell.getCellStyle().getDataFormatString())){
System.out.println("123123");
System.out.println(new SimpleDateFormat("yyyy/MM/dd").format(cell.getDateCellValue()));
}
holiday.setEndDate(content);
}else if(j==2){
holiday.setHolidayNo(content);
}
}
//     boolean resu =  jjrglService.addJjr(holiday);
//     if(resu){
//     success = true ;
//     }
}
System.out.println(holiday.toString());
}


} catch (Exception e) {
e.printStackTrace();
}
        dataMap.put("success", success);
dataMap.put("message", "插入完毕");
return "123";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值