c#打印格式化数据的简便方法

col*_*sso 6 .net c# xml printing winforms

我正在寻找一种简单的方法来打印DIN-A4纸,上面有数据库中的数据.应将数据填充到带边框的多个表中.某些数据应采用不同的文本格式,如粗体或带下划线.我还应该能够在该表上打印多个图像.

该程序应该是Windows窗体应用程序或用C#编写的控制台应用程序.

哪种格式化数据并将其打印出来的最简单/最常用的方法是什么?

任何建议apreciated :)

编辑:

这是我目前的代码几乎没有成功.它实际上打印但我得到的只是打印的xml文件.

        private void printButton_Click(object sender, EventArgs e)
    {

        string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        fileToPrint = new System.IO.StreamReader(printPath + @"\test.xml");
        printFont = new System.Drawing.Font("Arial", 10);

        PrintDocument printDocument1 = new PrintDocument();
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        printDocument1.Print();
        fileToPrint.Close();
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        float yPos = 0f;
        int count = 0;
        float leftMargin = e.MarginBounds.Left;
        float topMargin = e.MarginBounds.Top;
        string line = null;
        float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
        while (count < linesPerPage)
        {
            line = fileToPrint.ReadLine();
            if (line == null)
            {
                break;
            }
            yPos = topMargin + count * printFont.GetHeight(e.Graphics);
            e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
            count++;
        }
        if (line != null)
        {
            e.HasMorePages = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Kam*_*mil 2

如果没有额外的工具,在 .NET 中打印是相当困难的。

  1. Visual Studio 2008 和报告向导

    这是我最喜欢的工具。它基于 Visual Studio 2008 中提供的 Microsoft Reporting Services。它的工作方式与 MS Access 报告功能类似。

    不幸的是,我无法在其他 Visual Studio 版本中运行它并设计报告。Reporting Services 在 .NET Framework 中可用,您可以将它们与任何 Visual Studio 一起使用,但 2008 以外的版本中没有报表设计器/向导工具。

  2. 水晶报表(昂贵但非常好)。

  3. 如果你有时间,你可能会像这样使用像素:

    使用 C# 简化 .NET 打印 Dave Brighton,位于 codeproject.com

  4. WebBrowser 控件,正如 @colosso 在另一个答案中所写,但这取决于 Internet Explorer 版本,我个人不喜欢这种方法。