Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C#制作带有背景图像的A4报告(如Jasper报告和iReport)_C#_Visual Studio 2010_Crystal Reports - Fatal编程技术网

用C#制作带有背景图像的A4报告(如Jasper报告和iReport)

用C#制作带有背景图像的A4报告(如Jasper报告和iReport),c#,visual-studio-2010,crystal-reports,C#,Visual Studio 2010,Crystal Reports,我只想将数据库中的一些值放在一个A4页面上(我有一个JPG模板) 和创建一个PDF书籍/报告,每页插入一个内容。 我已经使用iReport编辑器轻松地使用NetBeans Java Jasper报告完成了这项工作 在VisualStudioC#Crystal报告中,这似乎要困难得多。 我真的搜索了Crystal Reports的教程,但没有一本 正在使用A4图像作为模板。如果你知道任何这样的教程,请帮助我 我更喜欢以编程方式而不是通过向导工作的解决方案。 我已经用我的程序管理了我的数据库。我只需

我只想将数据库中的一些值放在一个A4页面上(我有一个JPG模板) 和创建一个PDF书籍/报告,每页插入一个内容。 我已经使用iReport编辑器轻松地使用NetBeans Java Jasper报告完成了这项工作

在VisualStudioC#Crystal报告中,这似乎要困难得多。 我真的搜索了Crystal Reports的教程,但没有一本 正在使用A4图像作为模板。如果你知道任何这样的教程,请帮助我

我更喜欢以编程方式而不是通过向导工作的解决方案。 我已经用我的程序管理了我的数据库。我只需要报告和一些 关于如何为报告提供输入值的文档。我甚至不知道 需要报表才能访问数据库。我可以得到我的所有价值观 节目。对我来说,最好的解决方案是以我的JPG文件为背景的模板 和框(如文本框),我通过参数从程序中提供文本
函数的定义。比如Jasper Reports/iReport

您可以使用Crystal自身中的以下选项设置报告的纸张大小。 首先打开报告,然后转到
文件->页面设置

好的,我花了一些时间找到了一个简单的解决方案,我得到了以下结果:

首先,我没有使用Crystal Reports,而是使用Windows Reports(rdlc文件)。 Windows报告更简单、更容易,可以添加图像作为背景 在这张图片的上方,有一些文本框引用了字符串参数(这正是我所需要的)。默认情况下,它们位于Visual Studio中,您可以在Visual Studio中设计报表(在解决方案资源管理器中单击鼠标右键-->添加报表)

然后,我找到了一个将报告转换为PDF文件的代码示例,并使用它编写了以下类:

public class XReport
{
    private ReportViewer reportViewer = new ReportViewer();

    public XReport()
    {
    }
    public XReport(String reportFilePath)
    {
        setReportFile(reportFilePath);
    }
    // set rdlc file
    public void setReportFile(String reportFilePath)
    {
        reportViewer.Reset();
        reportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
        reportViewer.LocalReport.ReportPath = reportFilePath;
    }
    public void setParameters(List<ReportParameter> parameters)
    {
        reportViewer.LocalReport.SetParameters(parameters.ToArray());
        reportViewer.LocalReport.Refresh();
    }
    public void setParameters(Dictionary<String, String> parameters)
    {
        XList<ReportParameter> parameterList = new List<ReportParameter>();
        XList<String> parameterKeys = parameters.getKeys();
        foreach (String parameterKey in parameterKeys) {
            parameterList.Add(new ReportParameter(parameterKey, parameters.get(parameterKey))); }
        setParameters(parameterList);
    }
    public void exportToPDF(String pdfFilePath)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;
        byte[] bytes = null;
        // render PDF file
        try { bytes = reportViewer.LocalReport.Render( "PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings); }
        catch (Exception ex) { System.Console.Write(ex.Message); }
        // write PDF file
        using (FileStream fs = new FileStream(pdfFilePath, FileMode.Create)) { fs.Write(bytes, 0, bytes.Length); fs.Close(); }
        // release reportViewer resources to avoid errors
        reportViewer.LocalReport.ReleaseSandboxAppDomain();
    }
}
公共类XReport
{
private ReportViewer ReportViewer=new ReportViewer();
公共报告(
{
}
公共XReport(字符串reportFilePath)
{
setReportFile(reportFilePath);
}
//设置rdlc文件
public void setReportFile(字符串reportFilePath)
{
reportViewer.Reset();
reportViewer.ProcessingMode=Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer.LocalReport.ReportPath=reportFilePath;
}
公共void setParameters(列出参数)
{
reportViewer.LocalReport.SetParameters(parameters.ToArray());
reportViewer.LocalReport.Refresh();
}
公共void setParameters(字典参数)
{
XList参数列表=新列表();
XList parameterKeys=parameters.getKeys();
foreach(参数键中的字符串参数键){
parameterList.Add(newreportparameter(parameterKey,parameters.get(parameterKey));}
设置参数(参数列表);
}
public void exportToPDF(字符串pdfFilePath)
{
警告[]警告;
字符串[]流线;
string mimeType=string.Empty;
字符串编码=string.Empty;
字符串扩展名=string.Empty;
字节[]字节=null;
//呈现PDF文件
请尝试{bytes=reportViewer.LocalReport.Render(“PDF”,null,out mimeType,out encoding,out extension,out streamid,out warnings);}
catch(异常ex){System.Console.Write(ex.Message);}
//编写PDF文件
使用(FileStream fs=newfilestream(pdfFilePath,FileMode.Create)){fs.Write(bytes,0,bytes.Length);fs.Close();}
//释放reportViewer资源以避免错误
reportViewer.LocalReport.ReleaseSandboxAppDomain();
}
}
它起作用了。试试看。注意两件事:

  • 为reportFilePath和PdfilePath使用正确的路径。(PdfilePath仅适用于我的非相对路径)

  • 确保在rdlc报告中添加了所有名称正确的参数。您可以在查看-->报告数据-->添加新参数(在参数处单击鼠标右键)处添加它们。另请参见:

希望我能帮忙。这对我很有效