java 使用poi 结合Struts2导出execl表格

第一步写action方法:

  public String exportActiveExcel()
    {
        String name ="活跃度列表.xls";
        try {
                name = java.net.URLEncoder.encode(name, "UTF-8");
                fileName = new String(name.getBytes(), "iso-8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "success";
    }

fileName 为类变量要生成get和set方法,作为文件的名称

第二步把数据写入到一个流里:

 public InputStream getInputStream()
    {
     // 测试学生
        ExportExcel ex = new ExportExcel();
        String[] headers = {"学号", "姓名", "年龄", "性别", "出生日期"};
        JSONArray array=new JSONArray();
        JSONArray arr=new JSONArray();
        arr.add(10000001);
        arr.add("张三");
        arr.add(20);
        arr.add(true);
        arr.add("2013-5-6");
        JSONArray arr1=new JSONArray();
        arr1.add(10000002);
        arr1.add("李四");
        arr1.add(24);
        arr1.add(false);
        arr1.add("2013-5-6");
        array.add(arr);
        array.add(arr1);
        return  ex.exportExcel("活跃度分析",headers, array);
    }

用到的类:

public class ExportExcel
{
   
    @SuppressWarnings("deprecation")
    public InputStream exportExcel(String title, String[] headers, JSONArray array)
    {
        // 声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 生成一个表格
        HSSFSheet sheet = workbook.createSheet(title);
        // 设置表格默认列宽度为15个字节
        sheet.setDefaultColumnWidth((short) 30);
        // 生成一个标题样式
        HSSFCellStyle titleStyle = workbook.createCellStyle();
        // 设置这些样式
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        titleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        titleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 生成一个标题字体
        HSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.VIOLET.index);
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 把字体应用到当前的样式
        titleStyle.setFont(font);
        // 生成内容样式
        HSSFCellStyle contentStyle = workbook.createCellStyle();
        contentStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        contentStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 生成内容字体
        HSSFFont contentFont = workbook.createFont();
        contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        contentStyle.setFont(contentFont);
        // 产生表格标题行
        HSSFRow row = sheet.createRow(0);
        for (short i = 0; i < headers.length; i++)
        {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(titleStyle);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //生成内容行
        for (int i = 0; i < array.size(); i++)
        {
            row = sheet.createRow(i+1);
            JSONArray arr=(JSONArray) array.get(i);
            for (int j = 0; j < arr.size(); j++)
            {
                HSSFCell cell = row.createCell(j);
                cell.setCellStyle(contentStyle);
                HSSFRichTextString richString = new HSSFRichTextString(arr.get(j).toString());
                HSSFFont font3 = workbook.createFont();
                font3.setColor(HSSFColor.BLUE.index);
                richString.applyFont(font3);
                cell.setCellValue(richString);
            }
        }
        //写入输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try
        {
            workbook.write(baos);// 写入
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        byte[] ba = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(ba);
        try
        {
            baos.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return bais;
    }
}

第三步配置struts.xml文件

  <action name="exportActiveExcel" class="activeCountAction"
   method="exportActiveExcel">
   <result name="success" type="stream">
    <param name="contentType">application/vnd.ms-excel</param>
    <param name="contentDisposition">attachment;filename="${fileName}"</param>
    <param name="inputName">inputStream</param>
    <param name="bufferSize">4096</param>
   </result>
  </action>

inputName的值要与流的方法一致,生成getXxxxx,不然找不到方法

在页面上直接调用action方法的路径

 <input type="button" value="数据导出" οnclick="javascript:window.location.href='exportActiveExcel';"/>

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值