以编程方式在C#中打印不同类型的word文档

时间:2015-02-26 13:11:52

标签: c# printing ms-word ms-office

我想问一下如何从C#中打印各种文档。我这样做是通过替换文本来修改文档单词,例如:

<Value> -> value. 

我有各种文件,即。纯文本文档和表格中的文档。该表显示了要偿还的分期付款。该表的文档是一个问题,因为我不知道如何检索数据以及如何显示它们。

整体仍然可以配置,添加,编辑,删除文档。 我有一些想法,你可以在下面看到。

public string CreateDoc(object fileName, string _query, string _numUmowy)
    {
        object oMissing = System.Reflection.Missing.Value;
        object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

        string message = string.Empty;

        Application wordApp = new Application();
        Document aDoc = null;

        if (File.Exists((string)fileName))
        {
            DateTime today = DateTime.Now;

            object readOnly = false;
            object isVisible = false;

            wordApp.Visible = false;

            aDoc = wordApp.Documents.Open(ref fileName, 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, ref oMissing);

            aDoc.Activate();



            using (SQLiteConnection conn = new SQLiteConnection(GetConnStr()))
            {
                using (SQLiteCommand cmd = new SQLiteCommand(_query, conn))
                {
                    conn.Open();

                    cmd.Parameters.AddWithValue("@UmowaNum", _numUmowy);

                    using (SQLiteDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();

                            int rdrLength = rdr.FieldCount;
                            for (int i = 0; i < rdrLength; i++)
                            {
                                this.FindAndReplace(wordApp, "<" + rdr.GetName(i) + ">", rdr[i].ToString());
                            }


                            aDoc.PrintOut(oMissing, oMissing, oMissing, oMissing,
                                oMissing, oMissing, oMissing, oMissing, oMissing,
                                oMissing, oMissing, oMissing, oMissing, oMissing,
                                oMissing, oMissing, oMissing, oMissing);

                            message = "Drukowanie zakończone powodzeniem!";
                        }
                    }
                }
            }

            aDoc.Close(ref doNotSaveChanges, ref oMissing, ref oMissing);
            wordApp.Quit();

            return message;

        }
        else
        {
            message = "Plik nie istnieje!";
            return message;
        }
    }

此代码打印协议,但我不知道如何打印分期付款。 Agreement.doc

aa bbb fbdsf
<Value1>
aa bbb fbdsf
<Value2>
aa bbb fbdsf
<Value3>
aa bbb fbdsf
<Value4>
aa bbb fbdsf
<Value5>

Installment.doc

aa bbb fbdsf
<Value1>
aa bbb fbdsf
<Value2>
aa bbb fbdsf
TABLE (start)
1. <val1.1> <val1.2> <val1.3>
2. <val2.1> <val2.2> <val2.3>
3. <val3.1> <val3.2> <val3.3>
4. <val4.1> <val4.2> <val4.3>
5. <val5.1> <val5.2> <val5.3>
TABLE (end)

3 个答案:

答案 0 :(得分:0)

看看这个: Printing using Word Interop with Print Dialog

如果它不会帮助你,请尝试更具体地解决您的问题。

编辑:来自(https://wurstkoffer.wordpress.com/2013/05/18/c-printing-to-word-programmatically-in-3-way/

的第二个选项(没有interoop)代码
string filename = @"C:\test.docx";
string printer = "printer name";

// Send it to the selected printer
using (PrintDialog printDialog1 = new PrintDialog())
{
  printDialog1.PrinterSettings.PrinterName = printer;
  if (printDialog1.ShowDialog() == DialogResult.OK)
  {
    System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(filename);
    //info.Arguments = “\”” + printDialog1.PrinterSettings.PrinterName + “\””;
    info.Arguments = "\"" + printer + "\"";
    info.CreateNoWindow = true;
    info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    info.UseShellExecute = true;
    info.Verb = "PrintTo";
    System.Diagnostics.Process.Start(info);
  }
}

答案 1 :(得分:0)

这是我的FindAndReplace函数,它完美地运行

public void FindAndReplace(Application WordApp, object findText, object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        WordApp.Selection.Find.Execute(ref findText, 
            ref matchCase, ref matchWholeWord, 
            ref matchWildCards, ref matchSoundsLike,
            ref nmatchAllWordForms, ref forward, 
            ref wrap, ref format, ref replaceWithText, 
            ref replace, ref matchKashida, 
            ref matchDiacritics, ref matchAlefHamza, ref matchControl);
    }

答案 2 :(得分:0)

嗯,我尝试了一切。我使用了所有代码的精确副本,我使用'northwindEF.db'作为数据库。

我将CustomerID用作'_numUmowy'

cmd.Parameters.AddWithValue("@CustomerID", _numUmowy);

我将表Customers中的确切列名称放入所有'&lt;' '&GT;'在那些word文档中。一切都很顺利。特定用户的所有值都很好地复制到word文档中,然后打印这些文档没有任何问题。

this.CreateDoc("path to agreement doc", "SELECT * FROM Customers", "ANATR")
this.CreateDoc("path to installment doc", "SELECT * FROM Customers", "ALFKI")

我真的很抱歉,我无法帮助你解决这个问题。我希望别人会。