java动态生成带下拉框的Excel导入模板

 在实际开发中,由于业务需要,常常需要进行Excel导入导出操作。以前做一些简单的导入时,先准备一个模板,再进行导入,单有十几、

二十几个导入模板时,往往要做十几、二十几个模板。而且,当在模板中需要有下拉框,而且下拉数据要实时查询时,这样的方法就不太好了;

现在,做成了动态生成导入模板,只要参数传对了,就可以生成任何我们想要的导入模板,而且随意设置下拉框的数目、位置。

 

具体步骤如下:

1、需要用到的jar包:

poi-3.10-FINAL.jar

poi-ooxml-3.10-FINAL.jar

poi-ooxml-schemas-3.10-FINAL.jar

gradle引入:

compile group: 'org.apache.poi', name: 'poi', version: '3.10'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.10-FINAL'

 

2、生成Excel导入模板 :

复制代码

/**
     * @Title: createExcelTemplate 
     * @Description: 生成Excel导入模板  
     * @param @param filePath  Excel文件路径
     * @param @param handers   Excel列标题(数组)
     * @param @param downData  下拉框数据(数组)
     * @param @param downRows  下拉列的序号(数组,序号从0开始)
     * @return void
     * @throws
     */
    private static void createExcelTemplate(String filePath, String[] handers, 
            List downData, String[] downRows){
        
        HSSFWorkbook wb = new HSSFWorkbook();//创建工作薄
        
        //表头样式
        HSSFCellStyle style = wb.createCellStyle();  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
        //字体样式
        HSSFFont fontStyle = wb.createFont();    
        fontStyle.setFontName("微软雅黑");    
        fontStyle.setFontHeightInPoints((short)12);    
        fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
        style.setFont(fontStyle);
        
        //新建sheet
        HSSFSheet sheet1 = wb.createSheet("Sheet1");
        HSSFSheet sheet2 = wb.createSheet("Sheet2");
        HSSFSheet sheet3 = wb.createSheet("Sheet3");
        
        //生成sheet1内容
        HSSFRow rowFirst = sheet1.createRow(0);//第一个sheet的第一行为标题
        //写标题
        for(int i=0;i 
  

复制代码

 

3、设置有效性:

 View Code

 

4、下载指定路径的Excel文件:

 View Code

 

5、删除生成的临时文件:

复制代码

/**
     * @Title: delFile 
     * @Description: 删除文件
     * @param @param filePath  文件路径
     * @return void
     * @throws
     */
    public static void delFile(String filePath) {     
        java.io.File delFile = new java.io.File(filePath);     
        delFile.delete();     
    } 

复制代码

 

6、示例:

复制代码

/**
     * @Title: getExcelTemplate 
     * @Description: 生成Excel模板并导出 
     * @param @param uuid
     * @param @param request
     * @param @param response
     * @param @return
     * @return Data
     * @throws
     */
    @RequestMapping("/getExcelTemplate")
    public void getExcelTemplate(HttpServletRequest request, HttpServletResponse response){
        
        String fileName = "员工信息表"; //模板名称
        String[] handers = {"姓名","性别","证件类型","证件号码","服务结束时间","参保地","民族"}; //列标题
        
        //下拉框数据
        List downData = new ArrayList();
        String[] str1 = {"男","女","未知"};
        String[] str2 = {"北京","上海","广州","深圳","武汉","长沙","湘潭"};
        String[] str3 = {"01-汉族","02-蒙古族","03-回族","04-藏族","05-维吾尔族","06-苗族","07-彝族","08-壮族","09-布依族",
              "10-朝鲜族","11-满族","12-侗族","13-瑶族","14-白族","15-土家族","16-哈尼族","17-哈萨克族","18-傣族","19-黎族","20-傈僳族",
              "21-佤族","22-畲族","23-高山族","24-拉祜族","25-水族","26-东乡族","27-纳西族","28-景颇族","29-柯尔克孜族","30-土族",
              "31-达斡尔族","32-仫佬族","33-羌族","34-布朗族","35-撒拉族","36-毛难族","37-仡佬族","38-锡伯族","39-阿昌族","40-普米族",
              "41-塔吉克族","42-怒族","43-乌孜别克族","44-俄罗斯族","45-鄂温克族","46-德昂族","47-保安族","48-裕固族","49-京族","50-塔塔尔族",
              "51-独龙族","52-鄂伦春族","53-赫哲族","54-门巴族","55-珞巴族","56-基诺族","98-外国血统","99-其他"};
        downData.add(str1);
        downData.add(str2);
        downData.add(str3);
        String [] downRows = {"1","5","6"}; //下拉的列序号数组(序号从0开始)
        
        try {
            
            ExcelUtil.getExcelTemplate(fileName, handers, downData, downRows, request, response);
            
        } catch (Exception e) {
            log.error("批量导入信息异常:" + e.getMessage());
        }
    }

复制代码

 

7、结果如图:

java动态生成带下拉框的Excel导入模板_第1张图片

java动态生成带下拉框的Excel导入模板_第2张图片

java动态生成带下拉框的Excel导入模板_第3张图片

你可能感兴趣的:(5小功能)