C# 使用C语言中的模板打印word文档#

C# 使用C语言中的模板打印word文档#,c#,office-interop,novacode-docx,C#,Office Interop,Novacode Docx,我正在尝试在c#应用程序中打印游戏记分卡。 我尝试采用的方法是——使用模板word文件,在需要添加球队名称、球员信息和分数的地方执行字符串替换和添加 下面是我到目前为止使用的代码片段。但它似乎并不完美 object readOnly = false; //default object isVisible = false; object addToRecentFiles = false; wo

我正在尝试在c#应用程序中打印游戏记分卡。 我尝试采用的方法是——使用模板word文件,在需要添加球队名称、球员信息和分数的地方执行字符串替换和添加

下面是我到目前为止使用的代码片段。但它似乎并不完美

                object readOnly = false; //default
            object isVisible = false;
            object addToRecentFiles = false;

            wordApp.Visible = false;

            aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                                        ref addToRecentFiles, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref isVisible,
                                        ref missing, ref missing, ref missing, ref missing);

            aDoc.Activate();
            this.FindAndReplace(wordApp, "pname", tFirstname.Text);
            this.FindAndReplace(wordApp, "teamname", tLastname.Text);
            this.FindAndReplace(wordApp, "tel", tPhone.Text);
            this.FindAndReplace(wordApp, "Company", tCompany.Text);
            this.FindAndReplace(wordApp, "Date", DateTime.Now.ToShortDateString());
            object copies = "1";
            object pages = "1";
            object range = Word.WdPrintOutRange.wdPrintCurrentPage;
            object items = Word.WdPrintOutItem.wdPrintDocumentContent;
            object pageType = Word.WdPrintOutPages.wdPrintAllPages;
            object oTrue = true;
            object oFalse = false;

            Word.Document document = aDoc;
            object nullobj = Missing.Value;
            int dialogResult = wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(ref nullobj);
            wordApp.Visible = false;
            if (dialogResult == 1)
            {
                document.PrintOut(
                ref oTrue, ref oFalse, ref range, ref missing, ref missing, ref missing,
                ref items, ref copies, ref pages, ref pageType, ref oFalse, ref oTrue,
                ref missing, ref oFalse, ref missing, ref missing, ref missing, ref missing);
            }
        aDoc.Close(ref missing, ref missing, ref missing);
        //File.Delete(tempPath);
        MessageBox.Show("File created.");
        List<int> processesaftergen = getRunningProcesses();
        killProcesses(processesbeforegen, processesaftergen);
objectreadonly=false//违约
对象isVisible=false;
对象addToRecentFiles=false;
可见=false;
aDoc=wordApp.Documents.Open(ref文件名、ref缺失、ref只读、,
ref addToRecentFiles,ref缺失,ref缺失,
参考缺失,参考缺失,参考缺失,
ref缺失,ref缺失,ref可见,
参考缺失、参考缺失、参考缺失、参考缺失);
aDoc.Activate();
this.FindAndReplace(wordApp,“pname”,tFirstname.Text);
this.FindAndReplace(wordApp,“teamname”,tLastname.Text);
this.findandplace(wordApp,“tel”,tPhone.Text);
this.findandplace(wordApp,“Company”,tCompany.Text);
this.FindAndReplace(wordApp,“Date”,DateTime.Now.toSortDateString());
对象副本=“1”;
对象页=“1”;
对象范围=Word.WdPrintOutRange.wdPrintCurrentPage;
对象项=Word.WdPrintOutItem.wdPrintDocumentContent;
对象页面类型=Word.WdPrintOutPages.wdPrintAllPages;
object oTrue=true;
ALSE的对象=false;
Word.Document Document=aDoc;
对象nullobj=缺少.Value;
int dialogResult=wordApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrint].Show(参考nullobj);
可见=false;
如果(dialogResult==1)
{
文件打印输出(
参考oTrue,参考ALSE,参考范围,参考缺失,参考缺失,参考缺失,
参考项目、参考副本、参考页面、参考页面类型、参考ALSE、参考oTrue、,
参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失,参考缺失);
}
aDoc.关闭(参考缺失、参考缺失、参考缺失);
//Delete(tempPath);
Show(“文件已创建”);
List processesaftergen=getrunningprocesss();
终止进程(前进程、后进程);
我所面临的一些问题:

  • 文档在其他进程中打开
  • 模板本身会被修改
  • 打印效果并不理想
    我还尝试过使用docx库。但据我所知,它不提供打印功能

    1)您无法控制打开文档的流程,有时Word决定启动新流程。2) 当然模板被修改了,你做了“FindAndReplace”,你期待什么?3) 你的具体问题是什么?具体问题是我该怎么做。我在使用FindAndReplace时遇到了问题。也许我应该打开一个本地副本并修改它。我会试试的。