码迷,mamicode.com
首页 > 其他好文 > 详细

导出报表

时间:2014-09-18 11:19:43      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

public String export() {
        HttpServletResponse response = ServletActionContext.getResponse();
        filename = filename==null?"chart":filename;
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if (null != type && null != svg) {
            svg = this.handleSvg(svg);
            String ext = "";
            Transcoder t = null;
            if (type.equals("image/png")) {
                ext = "png";
                t = new PNGTranscoder();
            } else if (type.equals("image/jpeg")) {
                ext = "jpg";
                t = new JPEGTranscoder();
            } else if (type.equals("application/pdf")) {
                ext = "pdf";
                t =(Transcoder) new PDFTranscoder();
            } else if(type.equals("image/svg+xml")) 
                ext = "svg";   
            response.addHeader("Content-Disposition", "attachment; filename="+ filename + "."+ext);
            response.addHeader("Content-Type", type);
            
            if (null != t) {
                TranscoderInput input = new TranscoderInput(new StringReader(svg));
                TranscoderOutput output = new TranscoderOutput(out);
                try {
                    t.transcode(input, output);
                } catch (TranscoderException e) {
                    try {
                        out.print("Problem transcoding stream. See the web logs for more details.");
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                }
            } else if (ext.equals("svg")) {
                try {
                    OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
                    writer.append(svg);
                    writer.close();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }catch (IOException e1) {
                    e1.printStackTrace();
                }
            } else
                try {
                    out.print("Invalid type: " + type);
                } catch (IOException e) {
                    e.printStackTrace();
                }
        } else {
            response.addHeader("Content-Type", "text/html");
            try {
                out.println("Usage:\n\tParameter [svg]: The DOM Element to be converted." +
                        "\n\tParameter [type]: The destination MIME type for the elment to be transcoded.");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

 

导出报表

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/mlloc-clove/p/3978731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!