zoukankan      html  css  js  c++  java
  • Java服务器端生成报告文档:使用SQL Server Report Service(SSRS)

    SQL Server Report Service(SSRS)提供了Asp.Net和WinForm两类客户端组件封装,因此使用C#实现SSRS报表的导出功能,仅需要使用相应的组件即可。

    Java操作SSRS,需要借助于SSRS的Report Server应用站点,该应用为SSRS的报表Web查看页面,通过发送Get请求获取要访问的报表并传递报表参数,此外可指明输出报表文件格式。

    SSRS关键参数:

    • rs:Format,指明输出文件格式,PDF、EXCEL、WORD等;

    注意事项:中文报表路径和中文参数需要进行URL编码。

    示例代码如下所示:

    package lims.ssrs;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.List;
    import java.util.Map;
    import java.net.URLEncoder;
    
    public class SSRSDemo {
        public static void main(String[] args) {
            String url = "http://ipaddress/ReportServer?"; 
            url += URLEncoder.encode("/tests/测试2");
            url += "&rs:Command=Render&rs:Format=PDF"; 
            url += "&p1="+URLEncoder.encode("参数1的值")+"&p2="+URLEncoder.encode("参数2的值");
            String filePath = "C:/Workspaces/Java/LimsReportService/Test";
            filePath += "/"+ java.util.UUID.randomUUID() + ".pdf";
            downloadFile(url, filePath);
        }
        /**
         * 
         * @param urlPath
         *            下载路径
         * @param downloadDir
         *            下载存放路径
         * @return 返回下载文件
         */
        public static File downloadFile(String urlPath, String downloadDir) {
            File file = null;
            try {
                // 统一资源
                URL url = new URL(urlPath);
                // 连接类的父类,抽象类
                URLConnection urlConnection = url.openConnection();
                // http的连接类
                HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
                // 设定请求的方法,默认是GET 
                httpURLConnection.setRequestMethod("GET");
                // 设置字符编码
                httpURLConnection.setRequestProperty("Charset", "UTF-8");
                // 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
                httpURLConnection.connect();// 文件名
                String filePathUrl = httpURLConnection.getURL().getFile();
                String fileFullName = filePathUrl.substring(filePathUrl.lastIndexOf(File.separatorChar) + 1);
                URLConnection con = url.openConnection();
                BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
                file = new File(downloadDir);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                OutputStream out = new FileOutputStream(file);
                int size = 0;
                int len = 0;
                byte[] buf = new byte[1024];
                while ((size = bin.read(buf)) != -1) {
                    len += size;
                    out.write(buf, 0, size); 
                }
                bin.close();
                out.close();
            } catch (MalformedURLException e) { 
                e.printStackTrace();
            } catch (IOException e) { 
                e.printStackTrace();
            } finally {
                return file;
            }
        }
    }
  • 相关阅读:
    求数组中最小的k个数
    二叉树的四种遍历方法(C++)
    常见排序算法总结(C++)
    《剑指offer》第六十八题:树中两个结点的最低公共祖先
    《剑指offer》第六十七题:把字符串转换成整数
    《剑指offer》第六十六题:构建乘积数组
    《剑指offer》第六十五题:不用加减乘除做加法
    《剑指offer》第六十四题:求1+2+…+n
    《剑指offer》第六十三题:股票的最大利润
    《剑指offer》第六十二题:圆圈中最后剩下的数字
  • 原文地址:https://www.cnblogs.com/mahongbiao/p/8732802.html
Copyright © 2011-2022 走看看