SSRS使用SOAP in Java上载报告

时间:2016-09-20 13:09:26

标签: java sql-server soap reporting-services

我在JAVA项目中使用此SSRS-API,使用SQL Server Reporting Services。使用此API,我可以访问我的SSRS文件夹和报告,但我想知道是否可以上传或导出Excel或PDF格式的报告。

这是SSRS java类:SSRS.java

我的托管豆:

@ViewScoped
@ManagedBean(name = "biController")
@SuppressWarnings("restriction")
public class BiController {

    private final String ADDRESS = "http://SERVERNAME/reportserver/ReportService2005.asmx?wsdl";
    private String[] listReports;
    private SSRS ssrs;

    @PostConstruct
    public void init() {
        try {
            URL url = new URL(null, ADDRESS, new sun.net.www.protocol.http.Handler());
            NTLMAuthenticator.setDefault(new NTLMAuthenticator("DOMAIN", "USERNAME", "PASSWORD"));
            ssrs = new SSRS(url, "");
        } catch (MalformedURLException e) {
            throw new FacesException();
        }
    }

    // This function let me have the Report's names
    public void constructListOfReports(String path) {
        listReports = ssrs.listReports(path);
    }

    public String[] getListReports() {
        return listReports;
    }

    public void setListReports(String[] listReports) {
        this.listReports = listReports;
    }

}

有什么建议吗?

修改

我使用了上面提到的SSRS api的下载功能( downloadReport )(SSRS-API),这是函数的代码:

public void downloadReport(final String path, final String filename) {
    final File file = new File(filename);
    final String physicalName = toPhysicalFileName(path);

    info("Downloading Report with symbolic name " + path + " to " + file);

    final byte[] data = _soap.getReportDefinition(physicalName);

    try (final FileOutputStream out = new FileOutputStream(file)) {
        out.write(data);
    } catch (final IOException ioe) {
        final String message = "Failed to download report with symbolic name " + path + " to " + file;
        LOG.warning(message);
        if (file.exists() && !file.delete()) {
            throw new IllegalStateException(message + " and failed to delete temporary file", ioe);
        } else {
            throw new IllegalStateException(message, ioe);

        }
    }
}

这是我用来调用这个函数的函数:

public void downloadReport() {
    ssrs.downloadReport('Path/Report name', 'C:\\PATH\\TO\\A\\FOLDER\\REPORT.XML');
}

在给定的路径(C:/PATH/TO/A/FOLDER/REPORT.XML)中,我得到一个这样的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner" xmlns:cl="http://schemas.microsoft.com/sqlserver/reporting/2010/01/componentdefinition" xmlns="http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition">
  <AutoRefresh>0</AutoRefresh>
  <DataSources>
    <DataSource Name="PercallAnalysisDW">
      <DataSourceReference>Entrepôt de données Percall Analysis</DataSourceReference>
      <rd:SecurityType>None</rd:SecurityType>
      <rd:DataSourceID>3a3e3aa4-c6d6-4b44-80f0-f18a9ecd2eac</rd:DataSourceID>
    </DataSource>
  </DataSources>
  <DataSets>
    <DataSet Name="DeliveryMarginCumuleDS">
      <SharedDataSet>
        <SharedDataSetReference>DeliveryMarginCumuleDS</SharedDataSetReference>
      </SharedDataSet>
      <Fields>
        <Field Name="Date">
          <DataField>Date</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
        <Field Name="Projet">
          <DataField>Projet</DataField>
          <rd:TypeName>System.String</rd:TypeName>
        </Field>
        <Field Name="LABOR_facturé">
          <DataField>LABOR_facturé</DataField>
          <rd:TypeName>System.Int32</rd:TypeName>
        </Field>
        <Field Name="TL_facturé">
          <DataField>TL_facturé</DataField>
          <rd:TypeName>System.Int32</rd:TypeName>
        </Field>
        <Field Name="Coût_total">
          <DataField>Coût_total</DataField>
          <rd:TypeName>System.Int32</rd:TypeName>
        </Field>
        <Field Name="DM">
          <DataField>DM</DataField>
          <rd:TypeName>System.Int32</rd:TypeName>
        </Field>
        <Field Name="Revenu">
          <Value>=Fields!LABOR_facturé.Value + Fields!TL_facturé.Value</Value>
        </Field>
      </Fields>
    </DataSet>
  </DataSets>
  <ReportSections>
    <ReportSection>
      <Body>
        <ReportItems>
          <Tablix Name="Tablix1">
            <TablixBody>
                ...

1 个答案:

答案 0 :(得分:0)

我通过生成带URL的报告解决了这个问题,这是允许我生成报告的功能:

public void downloadReportExcel(String path) {
    try {
        String url = "http://" + SSRS_IP + "/ReportServer?/" + path + "&rs:Format=Excel";

        FacesContext.getCurrentInstance().getExternalContext().redirect(url);

        return;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

该函数在参数中获取路径并将我重定向到具有2个参数的服务器URL:

  • ?/ path:是要生成的报告的完整路径(来自根文件夹)
  • rs:Format = Excel:是生成的格式(这里我想将报告导出到Excel,但它可以采用PDF,如:&amp; rs:Format = PDF)