• 数据导出客户端自动下载


    package com.lala.teamtoneback.code.common.util;

    import java.io.BufferedOutputStream;

    import java.io.OutputStream;
    import java.util.List;

    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts2.ServletActionContext;

    import jxl.Workbook;
    import jxl.format.Alignment;
    import jxl.format.UnderlineStyle;
    import jxl.write.Label;
    import jxl.write.NumberFormats;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;

    public class ExcelUtil{
    /**
    * Excle数据导出
    * @param workbook 工作薄
    * @param sheet 工作表
    * @param str 表头
    * @param list 写入数据,数据格式list<list<String>>
    * @throws Exception
    */
    public static void writeExcel(String workbook,String sheet,List<String> str,
    List<List<String>> list) throws Exception{
    HttpServletResponse response = ServletActionContext.getResponse();
    // 清空response
    response.reset();
    // 设置response的Header
    response.addHeader("Content-Disposition", "attachment;filename=" + new String(workbook.getBytes("gb2312"), "ISO8859-1" ));
    OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
    response.setContentType("application/octet-stream");
    //创建工作簿
    WritableWorkbook wwb = Workbook.createWorkbook(response.getOutputStream());

    //创建工作表
    WritableSheet ws = wwb.createSheet(sheet, 0);
    //创建表头,第一行
    for (int i = 0; i < str.size(); i++)
    {
    ws.addCell(new Label(i, 0, str.get(i),getHeaderCellStyle()));
    }
    //写入数据到Excel
    for (int j = 0; j <list.size(); j++)
    {
    for (int i = 0; i < str.size(); i++)
    {
    ws.addCell(new Label(i, j+1, list.get(j).get(i).toString()));
    }
    }

    wwb.write();
    wwb.close();
    toClient.flush();
    toClient.close();
    }
    /**
    * 表头单元格样式的设定
    */
    public static WritableCellFormat getHeaderCellStyle(){

    /*
    * WritableFont.createFont("宋体"):设置字体为宋体
    * 10:设置字体大小
    * WritableFont.BOLD:设置字体加粗(BOLD:加粗 NO_BOLD:不加粗)
    * false:设置非斜体
    * UnderlineStyle.NO_UNDERLINE:没有下划线
    */
    WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
    10,
    WritableFont.BOLD,
    false,
    UnderlineStyle.NO_UNDERLINE);

    WritableCellFormat headerFormat = new WritableCellFormat(NumberFormats.TEXT);
    try {
    //添加字体设置
    headerFormat.setFont(font);
    //设置单元格背景色:表头为黄色
    //headerFormat.setBackground(Colour.YELLOW);
    //设置表头表格边框样式
    //整个表格线为粗线、黑色
    //headerFormat.setBorder(Border.ALL, BorderLineStyle.THICK, Colour.BLACK);
    //表头内容水平居中显示
    headerFormat.setAlignment(Alignment.CENTRE);
    } catch (WriteException e) {
    System.out.println("表头单元格样式设置失败!");
    }
    return headerFormat;
    }
    }

  • 相关阅读:
    linux常用命令
    10.8统计英文词频
    9月10号作业
    华氏温度与摄氏温度转换
    小故事
    Java的工厂模式(三)
    Javascript实现图片翻转
    Java的工厂模式(二)
    Java的工厂模式(一)
    Java新建线程的两种方式
  • 原文地址:https://www.cnblogs.com/zengdingxin/p/3183935.html
Copyright © 2020-2023  润新知