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
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值