Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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#WinForms:报表中的数据未打印。_C#_Reporting Services_Report - Fatal编程技术网

C#WinForms:报表中的数据未打印。

C#WinForms:报表中的数据未打印。,c#,reporting-services,report,C#,Reporting Services,Report,我不熟悉C#和报告。我做错了什么或错过了什么 我找了又找,我一辈子都找不出这个 我正在使用C#Windows窗体应用程序。我有report2.rdlc,在报告查看器、文本框和tablix打印中的数据中,一切都很好。当我不使用报表查看器直接打印到打印机时,文本框会打印,但tablix中的数据不会打印。下面提供了报告数据窗口的屏幕截图以及编码和说明 StatusCodes.cs表单:包含具有DialogResult的打印按钮。如果单击“是”,则打开StatusCodePreview.cs表单。这里

我不熟悉C#和报告。我做错了什么或错过了什么

我找了又找,我一辈子都找不出这个

我正在使用C#Windows窗体应用程序。我有report2.rdlc,在报告查看器、文本框和tablix打印中的数据中,一切都很好。当我不使用报表查看器直接打印到打印机时,文本框会打印,但tablix中的数据不会打印。下面提供了报告数据窗口的屏幕截图以及编码和说明

StatusCodes.cs表单:包含具有DialogResult的打印按钮。如果单击“是”,则打开StatusCodePreview.cs表单。这里有报表查看器(这里没有问题)。如果单击“否”,将触发PrintWithoutReportViewer.cs,并将报告直接打印到默认打印机(问题:仅打印文本框和列标题,不打印数据)

打印时不使用ReportViewer.cs的代码

public static class PrintWithoutReportViewer
{
    private static int m_CurrentPageIndex;
    private static IList<Stream> m_Streams;
    private static PageSettings m_PageSettings;

    public static Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_Streams.Add(stream);
        return stream;
    }

    public static void Export(LocalReport report, bool print = true)
    {
        PaperSize paperSize = m_PageSettings.PaperSize;
        Margins margins = m_PageSettings.Margins;

        
        string deviceInfo = string.Format(
            CultureInfo.InvariantCulture,
            "<DeviceInfo>" +
            "<OutputFormat>EMF</OutputFormat>" +
            "<PageWidth>{5}</PageWidth>" +
            "<PageHeight>{4}</PageHeight>" +
            "<MarginTop>{0}</MarginTop>" +
            "<MarginLeft>{1}</MarginLeft>" +
            "<MarginRight>{2}</MarginRight>" +
            "<MarginBottom>{3}</MarginBottom>" +
            "</DeviceInfo>",
            ToInches(margins.Top),
            ToInches(margins.Left),
            ToInches(margins.Right),
            ToInches(margins.Bottom),
            ToInches(paperSize.Height),
            ToInches(paperSize.Width));

        Warning[] warnings;
        m_Streams = new List<Stream>();
        report.Render("Image", deviceInfo, CreateStream,
            out warnings);
        foreach (Stream stream in m_Streams)
            stream.Position = 0;

        if (print)
        {
            Print();
            //PrintDialog startPrint = new PrintDialog();
            //startPrint.ShowDialog();
        }
    }

    
    public static void PrintPage(object sender, PrintPageEventArgs e)
    {
        Stream pageToPrint = m_Streams[m_CurrentPageIndex];
        pageToPrint.Position = 0;

        
        using (Metafile pageMetaFile = new Metafile(pageToPrint))
        {
            Rectangle adjustedRect = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);

            
            e.Graphics.FillRectangle(Brushes.White, adjustedRect);

            
            e.Graphics.DrawImage(pageMetaFile, adjustedRect);

            
            m_CurrentPageIndex++;
            e.HasMorePages = m_CurrentPageIndex < m_Streams.Count;
        }
    }

    public static void Print()
    {
        if (m_Streams == null || m_Streams.Count == 0)
            throw new Exception("Error: no stream to print.");
        PrintDocument printDoc = new PrintDocument();
        if (!printDoc.PrinterSettings.IsValid)
        {
            throw new Exception("Error: cannot find the default printer.");
        }
        else
        {
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            m_CurrentPageIndex = 0;
            printDoc.Print();
        }
    }

    public static void PrintToPrinter(this LocalReport report)
    {
        m_PageSettings = new PageSettings();
        ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

        m_PageSettings.PaperSize = reportPageSettings.PaperSize;
        m_PageSettings.Margins = reportPageSettings.Margins;

        Export(report);
    }

    public static void DisposePrint()
    {
        if (m_Streams != null)
        {
            foreach (Stream stream in m_Streams)
                stream.Close();
            m_Streams = null;
        }
    }

    private static string ToInches(int hundrethsOfInch)
    {
        double inches = hundrethsOfInch / 100.0;
        return inches.ToString(CultureInfo.InvariantCulture) + "in";
    }
}
公共静态类PrintWithoutReportViewer
{
私有静态int m_CurrentPageIndex;
私有静态IList m_流;
私有静态页面设置m_页面设置;
公共静态流CreateStream(字符串名称、字符串文件名扩展名、编码编码、字符串mimeType、bool willSeek)
{
Stream=新的MemoryStream();
m_Streams.Add(stream);
回流;
}
公共静态void导出(LocalReport报告,bool print=true)
{
PaperSize PaperSize=m_PageSettings.PaperSize;
页边距页边距=m_PageSettings.Margins;
string deviceInfo=string.Format(
CultureInfo.InvariantCulture,
"" +
“电动势”+
"{5}" +
"{4}" +
"{0}" +
"{1}" +
"{2}" +
"{3}" +
"",
ToInches(页边距。顶部),
ToInches(页边距。左侧),
ToInches(页边距。右侧),
ToInches(页边距。底部),
英寸(纸张尺寸、高度),
英寸(纸张尺寸、宽度);
警告[]警告;
m_Streams=新列表();
report.Render(“图像”、deviceInfo、CreateStream、,
发出警告);
foreach(m_Streams中的流)
流位置=0;
如果(打印)
{
打印();
//PrintDialog startPrint=新建PrintDialog();
//startPrint.ShowDialog();
}
}
公共静态无效打印页(对象发送方,PrintPageEventArgs e)
{
Stream PageTopPrint=m_Streams[m_CurrentPageIndex];
PageTopPrint.Position=0;
使用(图元文件pageMetaFile=新图元文件(pageToPrint))
{
矩形adjustedRect=新矩形(
e、 PageBounds.Left-(int)e.PageSettings.HardMarginX,
e、 PageBounds.Top-(int)e.PageSettings.HardMarginY,
e、 页面边界。宽度,
e、 身高),;
e、 图形。填充矩形(画笔。白色,调整矩形);
e、 Graphics.DrawImage(pageMetaFile,adjustedRect);
m_CurrentPageIndex++;
e、 HasMorePages=m_CurrentPageIndex
我最初尝试使用PrintDialog打印(我希望这个选项允许用户仍然能够选择打印机),但这根本不起作用,只是让我感到非常沮丧。我现在正在尝试使用Print()


就像我一开始说的,我对这一点很陌生,我迷路了。如果有人能帮助我,我们将不胜感激。

您使用的是什么版本?我以为您必须在返回的每个StreamSid上调用RenderStream()。很抱歉,回答这个问题花了这么长时间。当我检查Report2.rdlc时,它正在显示。当我检查报表服务器时,它正在显示。我不确定这是否是你想要的。RAD-忽略我上面的评论,我看到这是本地报告处理。
public static class PrintWithoutReportViewer
{
    private static int m_CurrentPageIndex;
    private static IList<Stream> m_Streams;
    private static PageSettings m_PageSettings;

    public static Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_Streams.Add(stream);
        return stream;
    }

    public static void Export(LocalReport report, bool print = true)
    {
        PaperSize paperSize = m_PageSettings.PaperSize;
        Margins margins = m_PageSettings.Margins;

        
        string deviceInfo = string.Format(
            CultureInfo.InvariantCulture,
            "<DeviceInfo>" +
            "<OutputFormat>EMF</OutputFormat>" +
            "<PageWidth>{5}</PageWidth>" +
            "<PageHeight>{4}</PageHeight>" +
            "<MarginTop>{0}</MarginTop>" +
            "<MarginLeft>{1}</MarginLeft>" +
            "<MarginRight>{2}</MarginRight>" +
            "<MarginBottom>{3}</MarginBottom>" +
            "</DeviceInfo>",
            ToInches(margins.Top),
            ToInches(margins.Left),
            ToInches(margins.Right),
            ToInches(margins.Bottom),
            ToInches(paperSize.Height),
            ToInches(paperSize.Width));

        Warning[] warnings;
        m_Streams = new List<Stream>();
        report.Render("Image", deviceInfo, CreateStream,
            out warnings);
        foreach (Stream stream in m_Streams)
            stream.Position = 0;

        if (print)
        {
            Print();
            //PrintDialog startPrint = new PrintDialog();
            //startPrint.ShowDialog();
        }
    }

    
    public static void PrintPage(object sender, PrintPageEventArgs e)
    {
        Stream pageToPrint = m_Streams[m_CurrentPageIndex];
        pageToPrint.Position = 0;

        
        using (Metafile pageMetaFile = new Metafile(pageToPrint))
        {
            Rectangle adjustedRect = new Rectangle(
                    e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
                    e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
                    e.PageBounds.Width,
                    e.PageBounds.Height);

            
            e.Graphics.FillRectangle(Brushes.White, adjustedRect);

            
            e.Graphics.DrawImage(pageMetaFile, adjustedRect);

            
            m_CurrentPageIndex++;
            e.HasMorePages = m_CurrentPageIndex < m_Streams.Count;
        }
    }

    public static void Print()
    {
        if (m_Streams == null || m_Streams.Count == 0)
            throw new Exception("Error: no stream to print.");
        PrintDocument printDoc = new PrintDocument();
        if (!printDoc.PrinterSettings.IsValid)
        {
            throw new Exception("Error: cannot find the default printer.");
        }
        else
        {
            printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
            m_CurrentPageIndex = 0;
            printDoc.Print();
        }
    }

    public static void PrintToPrinter(this LocalReport report)
    {
        m_PageSettings = new PageSettings();
        ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

        m_PageSettings.PaperSize = reportPageSettings.PaperSize;
        m_PageSettings.Margins = reportPageSettings.Margins;

        Export(report);
    }

    public static void DisposePrint()
    {
        if (m_Streams != null)
        {
            foreach (Stream stream in m_Streams)
                stream.Close();
            m_Streams = null;
        }
    }

    private static string ToInches(int hundrethsOfInch)
    {
        double inches = hundrethsOfInch / 100.0;
        return inches.ToString(CultureInfo.InvariantCulture) + "in";
    }
}