您的位置:首页 > 其它

将页面的列表数据导出到excel文件中

2017-12-12 17:21 543 查看
1、用的是springMVC的模式

2、在service接口层

//根据页面传过来的id在库里查询

List<TbItem> selectByPrimaryKeydhh(long ids);

     在service接口的实现类中

    @Override

    public List<TbItem> selectByPrimaryKeydhh(long ids) {

//执行查询的sql

        List<TbItem> selectByPrimaryKey = tbItemMapper.selectByPrimaryKeydhh(ids);

        return selectByPrimaryKey;

    }

3、在controller层

@Controller

public class Example {

    //定义文件的格式

    private static final ReportFileTypeEnum X = ReportFileTypeEnum.XLS;

    @Autowired

    private ItemService itemService;

                        

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

    public  void maiddhhn(long ids) {

        List<Model> dataCode0 = new ArrayList<Model>();

        List<TbItem> list = itemService.selectByPrimaryKeydhh(ids);

        for (int i = 0; i < list.size(); i++) {

            

            Model model=new Model();

             TbItem tbItem = list.get(i);

             model.set("id",tbItem.getId());

             model.set("title",tbItem.getTitle());

             model.set("sell_point",tbItem.getSellPoint());

             model.set("price",tbItem.getPrice());

             model.set("num",tbItem.getNum());

             model.set("barcode",tbItem.getBarcode());

             model.set("image",tbItem.getImage());

             model.set("cid",tbItem.getCid());

             model.set("status",tbItem.getStatus());

             model.set("created",tbItem.getCreated());

             model.set("updated",tbItem.getUpdated());

            

            dataCode0.add(model);

        }

        LinkedHashMap<String, String> headersCode = new LinkedHashMap<String, String>();

        headersCode.put("id","商品ID");

        headersCode.put("title","商品标题");

        headersCode.put("sell_point","叶子类目");

        headersCode.put("price","价格");

        headersCode.put("num","库存数量");

        headersCode.put("barcode","条形码");

        headersCode.put("image","图片");

        headersCode.put("cid","商品类目ID");

        headersCode.put("status","状态");

        headersCode.put("created","创建时间");

        headersCode.put("updated","更新日期");

 

        //code2列表的模板

        String code2temlateUrl = "D:\\export\\code2" + X.getStatusDesc();

        //实际导出来的数据

        String copytemplate2Url = "D:\\export\\copy" + X.getStatusDesc();

        //如果不存在这个文件夹就会创建这个文件夹

        if (!new File("D:\\export").exists()) {

            new File("D:\\export").mkdir();

        }

        HashMap<Integer, Integer> width = new HashMap<>();

        //设置这个excle会导出到少个列

        width.put(0, 3);

        width.put(1, 3);

        width.put(2, 3);

        width.put(3, 3);

        width.put(4, 3);

        width.put(5, 3);

        width.put(7, 3);

        width.put(8, 3);

        width.put(9, 3);

        width.put(10, 3);

        // 根据模板导出数据

        Utils.code2Tempalte(code2temlateUrl, dataCode0.size(), "商品列表", 12, X, width,

                headersCode.values().toArray());

        File file = new File(code2temlateUrl);

        if (!file.exists() || !file.isFile()) {

            System.out.println("文件不存在");

            return;

        }

        Utils.copyTemplate(file, copytemplate2Url, X);

        try {

            Workbook wb = Utils.getWorkbookInstance(X, new FileInputStream(copytemplate2Url));

            int rowNo = 2;

            int rowSum = 0;

            Utils.file2FileWithData(wb, X, dataCode0, rowNo, 0, 12,headersCode.keySet().toArray());

            rowSum = 2 + dataCode0.size();

            FileOutputStream out = null;

            out = new FileOutputStream(copytemplate2Url);

            wb.write(out);

            System.out.println("导出成功");

            out.close();

        } catch (Exception e) {

            e.printStackTrace();

        }
    }

4、对于controller中用到的几个工具类

//判断excel文件的后缀格式

public enum ReportFileTypeEnum {

    XLSX(0, ".xlsx"),

    XLS(1, ".xls");

    int status;

    String statusDesc;

    private ReportFileTypeEnum(int status,String statusDesc){

        this.status=status;

        this.statusDesc=statusDesc;

    }

    public int getStatus(){

        return status;

    }

    public String getStatusDesc(){

        return statusDesc;

    }

    public static ReportFileTypeEnum getType(int status){

        for (ReportFileTypeEnum excelFileTypeEnums : ReportFileTypeEnum.values()) {

            if (excelFileTypeEnums.getStatus() == status) {

                return excelFileTypeEnums;

            }

        }

        return null;

    }

    public static String getStatusDesc(int status){

        return getType(status).getStatusDesc();

    }

}

//用来封装实体类

public class Model{

    private Map<String, Object> attrs = new HashMap<String, Object>();

    public void set(String attr, Object value) {

        attrs.put(attr, value);

    }

    public Object get(String attr) {

        return attrs.get(attr);

    }

    public Set entrySet() {

        return attrs.entrySet();

    }

4、需要引入poi的jar包的

          <dependency>

            <groupId>org.apache.poi</groupId>

            <artifactId>poi-ooxml</artifactId>

            <version>3.8</version>

        </dependency>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐