captian_900331的博客

像牛一样的耕耘,但是不能像牛一样的活着!

java实现xls、csv文件导出或下载

一般在java开发中需要下载xls或者是csv文件,需要在做报表或者是数据采集的时候需要,现将代码片段摘出:

public String exportMouldCsv() {
        OutputStream os = null;
        WritableWorkbook wbook = null;
        try {
            HttpServletResponse response = ServletActionContext.getResponse();
            // 取得输出流
            os = response.getOutputStream();
            // 清空输出流
            response.reset();
            // 设定输出文件头
            String fileName = "文件名设置.xls";
            fileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
            response.setHeader("Content-disposition", "attachment; filename="
                    + fileName);
            // 定义输出类型
            response.setContentType("application/msexcel");
            // 建立excel文件
            wbook = Workbook.createWorkbook(os);

            WritableCellFormat cellFormat = new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);

            // WritableSheet.setColumnView(int i,int width)
            // 作用是指定第i+1列的宽度,比如:
            // 将第一列的宽度设为30
            // sheet.setColumnView(0,30)
            // wsheet.setRowView(0,10)
            WritableFont font1 = new WritableFont(WritableFont.TIMES, 10,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.BLACK);
            WritableCellFormat cellFormat_top = new WritableCellFormat();
            cellFormat_top.setAlignment(jxl.format.Alignment.CENTRE);
            // 设置单元格背景颜色
            // cellFormat_top.setBackground(Colour.LIGHT_BLUE);
            // 设置字体格式
            cellFormat_top.setFont(font1);
            cellFormat_top.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.THIN);

            WritableCellFormat cellFormat_bottom = new WritableCellFormat();
            cellFormat_bottom.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.THIN);

            WritableCellFormat cellFormat_bottom_1 = new WritableCellFormat();
            // 设置居中
            cellFormat_bottom_1.setAlignment(jxl.format.Alignment.CENTRE);
            // 设置表格边框
            cellFormat_bottom_1.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.THIN);
            WritableSheet wsheet = wbook.createSheet("
            文件名设置", 0);

            wsheet.setRowView(0, 300);
            wsheet.setRowView(1, 300);
            wsheet.setColumnView(0, 10);
            wsheet.setColumnView(1, 20);
            wsheet.setColumnView(2, 15);
            wsheet.setColumnView(3, 15);
            wsheet.setColumnView(4, 15);
            wsheet.setColumnView(5, 15);
            wsheet.setColumnView(6, 20);
            wsheet.setColumnView(7, 30);
            //表头设置
            Label label_0 = new Label(0, 0, "组织");
            label_0.setCellFormat(cellFormat_top);
            wsheet.addCell(label_0);

            Label label_1 = new Label(1, 0, "代码");
            label_1.setCellFormat(cellFormat_top);
            wsheet.addCell(label_1);

            Label label_2 = new Label(2, 0, "名称");
            label_2.setCellFormat(cellFormat_top);
            wsheet.addCell(label_2);

            Label label_3 = new Label(3, 0, "年");
            label_3.setCellFormat(cellFormat_top);
            wsheet.addCell(label_3);

            Label label_4 = new Label(4, 0, "月");
            label_4.setCellFormat(cellFormat_top);
            wsheet.addCell(label_4);

            Label label_5 = new Label(5, 0, "项");
            label_5.setCellFormat(cellFormat_top);
            wsheet.addCell(label_5);

            Label label_6 = new Label(6, 0, "项名称");
            label_6.setCellFormat(cellFormat_top);
            wsheet.addCell(label_6);

            Label label_7 = new Label(7, 0, "数量");
            label_7.setCellFormat(cellFormat_top);
            wsheet.addCell(label_7);

            wbook.write();
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        } finally {
            if (wbook != null) {
                try {
                    wbook.close();
                } catch (Exception e) {
                    logger.error(e);
                }
                wbook = null;
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    logger.error(e);
                }
                os = null;
            }
        }
    }

还有一种情况就是将后台获取的数据以xls或者是csv格式为文件导出,方便打印或者是传阅等。

public String exportMouldCsv() {
        OutputStream os = null;
        WritableWorkbook wbook = null;
        try {
            HttpServletResponse response = ServletActionContext.getResponse();
            // 取得输出流
            os = response.getOutputStream();
            // 清空输出流
            response.reset();
            // 设定输出文件头
            String fileName = "文件名设置.xls";
            fileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
            response.setHeader("Content-disposition", "attachment; filename="
                    + fileName);
            // 定义输出类型
            response.setContentType("application/msexcel");
            // 建立excel文件
            wbook = Workbook.createWorkbook(os);

            WritableCellFormat cellFormat = new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);

            // WritableSheet.setColumnView(int i,int width)
            // 作用是指定第i+1列的宽度,比如:
            // 将第一列的宽度设为30
            // sheet.setColumnView(0,30)
            // wsheet.setRowView(0,10)
            WritableFont font1 = new WritableFont(WritableFont.TIMES, 10,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    jxl.format.Colour.BLACK);
            WritableCellFormat cellFormat_top = new WritableCellFormat();
            cellFormat_top.setAlignment(jxl.format.Alignment.CENTRE);
            // 设置单元格背景颜色
            // cellFormat_top.setBackground(Colour.LIGHT_BLUE);
            // 设置字体格式
            cellFormat_top.setFont(font1);
            cellFormat_top.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.THIN);

            WritableCellFormat cellFormat_bottom = new WritableCellFormat();
            cellFormat_bottom.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.THIN);

            WritableCellFormat cellFormat_bottom_1 = new WritableCellFormat();
            // 设置居中
            cellFormat_bottom_1.setAlignment(jxl.format.Alignment.CENTRE);
            // 设置表格边框
            cellFormat_bottom_1.setBorder(jxl.format.Border.ALL,
                    jxl.format.BorderLineStyle.THIN);
            WritableSheet wsheet = wbook.createSheet("
            文件名设置", 0);

            wsheet.setRowView(0, 300);
            wsheet.setRowView(1, 300);
            wsheet.setColumnView(0, 10);
            wsheet.setColumnView(1, 20);
            wsheet.setColumnView(2, 15);
            wsheet.setColumnView(3, 15);
            wsheet.setColumnView(4, 15);
            wsheet.setColumnView(5, 15);
            wsheet.setColumnView(6, 20);
            wsheet.setColumnView(7, 30);
            //表头设置
            Label label_0 = new Label(0, 0, "组织");
            label_0.setCellFormat(cellFormat_top);
            wsheet.addCell(label_0);

            Label label_1 = new Label(1, 0, "代码");
            label_1.setCellFormat(cellFormat_top);
            wsheet.addCell(label_1);

            Label label_2 = new Label(2, 0, "名称");
            label_2.setCellFormat(cellFormat_top);
            wsheet.addCell(label_2);

            Label label_3 = new Label(3, 0, "年");
            label_3.setCellFormat(cellFormat_top);
            wsheet.addCell(label_3);

            Label label_4 = new Label(4, 0, "月");
            label_4.setCellFormat(cellFormat_top);
            wsheet.addCell(label_4);

            Label label_5 = new Label(5, 0, "项");
            label_5.setCellFormat(cellFormat_top);
            wsheet.addCell(label_5);

            Label label_6 = new Label(6, 0, "项名称");
            label_6.setCellFormat(cellFormat_top);
            wsheet.addCell(label_6);

            Label label_7 = new Label(7, 0, "数量");
            label_7.setCellFormat(cellFormat_top);
            wsheet.addCell(label_7);

            //获取从后台获取数据,并赋值给对应的列值
            MList mList = goalService.getMaterielViewList(mat);
            if (null!=mList ) {
                for (int i = 0; i < matList.size(); i++) {
                    Materiel materiel=new Materiel();
                    materiel=matList.get(i);
                    Label label_00 = new Label(0, (i+1), materiel.getMvgr1()==null?"":materiel.getMvgr1());
                    label_00.setCellFormat(cellFormat_bottom_1);
                    wsheet.addCell(label_00);
                    Label label_01 = new Label(1, (i+1), materiel.getBezei()==null?"":materiel.getBezei());
                    label_01.setCellFormat(cellFormat_bottom_left);
                    wsheet.addCell(label_01);
                    .....
                    //后面是自己来咯!!
                }
            }

            wbook.write();
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        } finally {
            if (wbook != null) {
                try {
                    wbook.close();
                } catch (Exception e) {
                    logger.error(e);
                }
                wbook = null;
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    logger.error(e);
                }
                os = null;
            }
        }
    }
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/captian_900331/article/details/51554890
文章标签: java
个人分类: java
想对作者说点什么? 我来说一句

java操作CSV文件文件

2008年10月22日 135KB 下载

javaCSV csv文件处理

2017年08月18日 13KB 下载

没有更多推荐了,返回首页

不良信息举报

java实现xls、csv文件导出或下载

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭