【问题标题】:Generating formatted (tables) report using rtf or word in .net在 .net 中使用 rtf 或 word 生成格式化(表格)报告
【发布时间】:2011-11-05 11:39:21
【问题描述】:

我需要从我的 don net 应用程序生成一个报告文件。我需要将数据以表格格式列出。通常,您会希望我使用 Microsoft 报表查看器控件。但我使用 sqlite,无法自动生成报告。此外,我的报告基于多个表格,而不是特定表格。所以我决定使用 RichTextFormat 或 Word .doc。

我需要创建一个看起来像这样的报告

-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|                 |                 |                 |                 |
|                 |                 |                 |                 |
|                 |                 |                 |                 |
-------------------------------------------------------------------------

当然,行不是固定的,但可以更小或更多。你能指导我以最好的方式做到这一点吗?

谢谢

【问题讨论】:

  • 如果你得到答案,请标记答案。

标签: .net winforms report rtf doc


【解决方案1】:

您也可以使用互操作库并直接写入 Word 文档。特别是如果文档结构非常简单。缺点:必须在生成报告的机器上安装 Word。下面的例子,记得添加程序集:Microsoft.Office.Interop.Word (...\Office12\Microsoft.Office.Interop.Word.dll), Office (...\Office12\Office.dll)。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;

namespace WriteToWordSO
{
    class Program
    {
        static void Main(string[] args)
        {
            _Application app = null;
            Documents docs = null;
            Document doc = null;
            Range range = null;
            Tables tables = null;
            Table table = null;

            object oMissing = System.Reflection.Missing.Value;
            object oFalse = false;
            object oTrue = true;
            object fileName = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "tmp.doc");
            object fileFormat = WdSaveFormat.wdFormatDocument;

            FileInfo fi = new FileInfo(fileName.ToString());
            if (fi.Exists) fi.Delete();

            int rows = 4, cols = 5;

            try
            {
                app = new ApplicationClass();
                app.Visible = false;
                app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                app.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;

                docs = app.Documents;
                doc = docs.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                range = doc.Range(ref oMissing, ref oMissing);

                tables = doc.Tables;

                object oAutoFitCell = WdDefaultTableBehavior.wdWord9TableBehavior;
                object oAutoFitWindow = WdAutoFitBehavior.wdAutoFitWindow;

                table = tables.Add(range, rows, cols, ref oAutoFitCell, ref oAutoFitWindow);

                Cell c = null;
                Range cellRange = null;

                // header row
                for (int i = 1; i < cols + 1; i++)
                {
                    c = table.Cell(1, i);
                    cellRange = c.Range;
                    cellRange.Text = "Header " + i.ToString();
                    cellRange.Bold = 1;
                    Marshal.ReleaseComObject(c);
                    Marshal.ReleaseComObject(cellRange);
                }

                // data rows
                for (int i = 1; i < cols + 1; i++)
                {
                    for (int j = 1; j < rows; j++)
                    {
                        c = table.Cell(j + 1, i);
                        cellRange = c.Range;
                        cellRange.Text = "Cell " + i.ToString() + j.ToString();
                        Marshal.ReleaseComObject(c);
                        Marshal.ReleaseComObject(cellRange);
                    }
                }

                doc.SaveAs(ref fileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing);

                ((_Document)doc).Close(ref oFalse, ref oMissing, ref oMissing);

                ((_Application)app).Quit(ref oFalse, ref oMissing, ref oMissing);

                Process.Start(fileName.ToString());
            }
            finally
            {
                // frees memory

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();

                // its very important to release all used object,
                // otherwise some complications may appear
                if (table != null) Marshal.ReleaseComObject(table);
                table = null;

                if (tables != null) Marshal.ReleaseComObject(tables);
                tables = null;

                if (range == null) Marshal.ReleaseComObject(range);
                range = null;

                if (doc != null) Marshal.ReleaseComObject(doc);
                doc = null;

                if (docs != null) Marshal.ReleaseComObject(docs);
                docs = null;

                if (app != null) Marshal.ReleaseComObject(app);
                app = null;
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    您可以使用 “Office Open XML” 并构建自己的 docx 编写器,请查看此 wiki url 了解更多信息。在 dotnet 中,您将获得此作业的特定命名空间。希望这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 2018-02-12
      相关资源
      最近更新 更多