excel 读取模板,病写入新文件

/**
        *
        * @param modelPath 模板路径
        * @param headRow 表头行数
        * @param list 数据集合
        * @param fileName 导出文件名
        */
    

       public void exportExcel(String modelPath,, List<String[]> list,String fileName,HttpServletResponse response) {

           int headRow=1;

            try {
                String strFileName = new Date().getTime()+".xls";
                if (fileName != null) {
                    strFileName = fileName + strFileName;
                }
                //strFileName = java.net.URLEncoder.encode(strFileName, "UTF-8");// 处理中文文件名的问题
                strFileName = new String(strFileName.getBytes("gbk"), "iso8859-1");// 处理中文文件名的问题
                response.reset();
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-Disposition","attachment; filename=" + strFileName);
                
                File tFile = new File(modelPath);
                Workbook tBook = Workbook.getWorkbook(tFile);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                WritableWorkbook book = Workbook.createWorkbook(baos, tBook);
                WritableSheet sheet = book.getSheet(0);
                for (int i = 0; list != null && i < list.size(); i++) {
                    String[] temp = list.get(i);
                    for (int j = 0; j < temp.length; j++) {
                        sheet.addCell(new Label(j, i + headRow, temp[j]));
                    }
                }
                book.write();
                book.close();
                tBook.close();
                ServletOutputStream out1;
                try {
                    out1 = response.getOutputStream();
                    baos.writeTo(out1);
                    out1.flush();
                    
                    baos.close();
                    out1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    


function exportData(){
        var ids = '';
        var $table = $('#table1');
        var ts = $table.find('input[type="checkbox"]:checked');
        var idArr = new Array();
        if(ts.size()==0){
            $.messager.alert("操作提示", "请至少选择一条记录!","warning");
            return false;
        }
        for(var i=0;i<ts.size();i++){
            var $item = ts.eq(i);
            ids += $item.attr('id') +',';
            idArr.push($item.attr('id'));
        }
        if(ids != null && ids.length>0){
            $.messager.confirm('操作提示', '确认导出选中的信息', function(r){
                if (r){    
                    location.href ="<%=path%>/t.do?ids="+idArr.join(',');
                }
            });
        }
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值