Apache POI 第七讲之利用 POI 技术实现使用模板批量添加数据

27 篇文章 0 订阅
6 篇文章 0 订阅

有时候我们在做项目时,有些项目需要生成Microsoft Excel文件格式的报告。有时,甚至希望将Excel文件作为输入数据。这是我们需要用到Apache POI 。例如,本次利用 POI 技术实现使用模板批量添加数据。

下载上传模板

1.编写页面

function downloadTemplate(){
    window.open('template/userExporTemplate.xls');
}

2.查看结果

这里写图片描述

这里写图片描述

这里写图片描述

利用 POI 技术实现使用模板批量添加数据

1.编写导出工具类

public static String formatCell(HSSFCell hssfCell){
    if(hssfCell==null){
        return "";
    }else{
        if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_BOOLEAN){
            return String.valueOf(hssfCell.getBooleanCellValue());
        }else if(hssfCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC){
            return String.valueOf(hssfCell.getNumericCellValue());
        }else{
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }
}

2.编写action类导出方法

public String upload()throws Exception{
        POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(userUploadFile));
        HSSFWorkbook wb=new HSSFWorkbook(fs);
        HSSFSheet hssfSheet=wb.getSheetAt(0);  // 获取第一个Sheet页
        if(hssfSheet!=null){
            for(int rowNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
                HSSFRow hssfRow=hssfSheet.getRow(rowNum);
                if(hssfRow==null){
                    continue;
                }
                User user=new User();
                user.setName(ExcelUtil.formatCell(hssfRow.getCell(0)));
                user.setPhone(ExcelUtil.formatCell(hssfRow.getCell(1)));
                user.setEmail(ExcelUtil.formatCell(hssfRow.getCell(2)));
                user.setQq(ExcelUtil.formatCell(hssfRow.getCell(3)));
                Connection con=null;
                try{
                    con=dbUtil.getCon();
                    userDao.userAdd(con, user);
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    dbUtil.closeCon(con);
                }
            }
        }
        JSONObject result=new JSONObject();
        result.put("success", "true");
        ResponseUtil.write(ServletActionContext.getResponse(), result);
        return null;
    }

3.编写页面

<div id="dlg2" class="easyui-dialog" style="width:400px;height:180px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons2">
        <form id="uploadForm" action="user!upload" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td>下载模版:</td>
                    <td><a href="javascript:void(0)" class="easyui-linkbutton"  onclick="downloadTemplate()">导入模版</a></td>
                </tr>
                <tr>
                    <td>上传文件:</td>
                    <td><input type="file" name="userUploadFile"></td>
                </tr>
            </table>
        </form>
    </div>
function uploadFile(){
    alert("1");
    $("#uploadForm").form("submit",{
        success:function(result){
        var result=eval('('+result+')');
        if(result.errorMsg){
            $.messager.alert("系统提示",result.errorMsg);
        }else{
            $.messager.alert("系统提示","上传成功");
            $("#dlg2").dialog("close");
            $("#dg").datagrid("reload");
        }
               }
    });
}

4.查看结果

这里写图片描述

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值