如何在模板中生成包含现有文本的word文档docx,但是通过asp.net Web表单填写值?

时间:2023-01-24 09:09:16

I want to generate a word document (.docx) file, which is based on a template could be a word template or that the text can be stored in code.

我想生成一个word文档(.docx)文件,该文件基于模板可以是一个单词模板,或者文本可以存储在代码中。

Now the document generated should be based on the Input provided by the user using an ASP.net Web Form with Text Boxes to input the values.

现在生成的文档应该基于用户使用带有文本框的ASP.net Web窗体提供的输入来输入值。

These values will be taken in and placed in placeholder positions in the word template.

这些值将被接收并放置在单词模板中的占位符位置。

  1. How can i use a word template in an ASP.net web project?
  2. 如何在ASP.net Web项目中使用单词模板?

  3. How can i take the values from the ASP.net webform and pass it to the word template, fill in the placeholder with the text and then generate the word document?
  4. 如何从ASP.net webform获取值并将其传递给word模板,用文本填充占位符然后生成word文档?

I have seen some examples where the text to be generated is created as HTML and then the file exported is given filetype msword to save as a word file. But in this case i wonder, how can i give page numbers and other header or footer values to the generated document.

我已经看到了一些示例,其中要生成的文本是作为HTML创建的,然后导出的文件被赋予filetype msword以保存为word文件。但在这种情况下,我想知道,我如何给生成的文档提供页码和其他页眉或页脚值。

So if i can use a word template, to just fill in the values the user wants and then generate the final document, then that's great.

因此,如果我可以使用单词模板,只需填写用户想要的值,然后生成最终文档,那就太棒了。

Is there anything new to do this in Visual Studio 2012? I am trying to do this using C#.

在Visual Studio 2012中有什么新功能吗?我试图用C#做这个。

3 个解决方案

#1


7  

You have several options for creating word documents -

您有几种创建word文档的选项 -

  1. Use word automation - this is using Ms Word directly. Not recommended if you're running on a server - it's slow and suffers from extensibility and other issues.

    使用word自动化 - 这是直接使用Ms Word。如果您在服务器上运行,则不推荐使用它 - 它很慢并且存在可扩展性和其他问题。

  2. Use OpenXML - this is Microsoft's SDK to work with Office 2007+ file formats. There are several ways to accomplish your goal with OpenXML - like using bookmarks or content controls. Look at this SO question - How to replace content in template docx document and Open XML SDK 2.0 (Aug 09)? or this one - How to create *.docx files from a template in C# or google it (while not that difficult, the entire solution is more that a few lines of code and does require a bit of understanding)

    使用OpenXML - 这是Microsoft的SDK,可以使用Office 2007+文件格式。使用OpenXML可以通过多种方式实现目标 - 例如使用书签或内容控件。看看这个问题 - 如何替换模板docx文档和Open XML SDK 2.0(2009年8月)中的内容?或者这个 - 如何从C#中的模板创建* .docx文件或谷歌它(虽然不是那么困难,整个解决方案更多的是几行代码并且需要一些理解)

  3. Use third party tools or controls - I'm not really familiar with them.

    使用第三方工具或控件 - 我并不熟悉它们。

#2


3  

You can create your template by creating a Word document ( including logos, header, footer, etc), putting the placeholders and save it as "Word 2003 xml" document.

您可以通过创建Word文档(包括徽标,页眉,页脚等)来创建模板,放置占位符并将其另存为“Word 2003 xml”文档。

Next, you can read the template.xml as a text file, replace the placeholders with the values, and response the entire string as a content type "application/msword"

接下来,您可以将template.xml作为文本文件读取,将占位符替换为值,并将整个字符串作为内容类型“application / msword”响应

The placeholder could be {MY_PLACEHOLDER1}

占位符可以是{MY_PLACEHOLDER1}

Before creating the template, you must deactivate ( in Word ) all related with Ortography, because the "bad ortography" highlightings are stored as a part of the xml document, and {MY_PLACEHOLDER1} can be stored as MY_PLACEHOLDER1 (the "{" and "}" characters are stored separated with their own word xml tags )

在创建模板之前,必须停用(在Word中)所有与Ortography相关的内容,因为“坏ortography”突出显示存储为xml文档的一部分,{MY_PLACEHOLDER1}可以存储为MY_PLACEHOLDER1(“{”和“ }“字符以自己的单词xml标签分隔存储)

Regards,

#3


2  

You can use TemplateEngine.Docx for replacing the text in template document

您可以使用TemplateEngine.Docx替换模板文档中的文本

#1


7  

You have several options for creating word documents -

您有几种创建word文档的选项 -

  1. Use word automation - this is using Ms Word directly. Not recommended if you're running on a server - it's slow and suffers from extensibility and other issues.

    使用word自动化 - 这是直接使用Ms Word。如果您在服务器上运行,则不推荐使用它 - 它很慢并且存在可扩展性和其他问题。

  2. Use OpenXML - this is Microsoft's SDK to work with Office 2007+ file formats. There are several ways to accomplish your goal with OpenXML - like using bookmarks or content controls. Look at this SO question - How to replace content in template docx document and Open XML SDK 2.0 (Aug 09)? or this one - How to create *.docx files from a template in C# or google it (while not that difficult, the entire solution is more that a few lines of code and does require a bit of understanding)

    使用OpenXML - 这是Microsoft的SDK,可以使用Office 2007+文件格式。使用OpenXML可以通过多种方式实现目标 - 例如使用书签或内容控件。看看这个问题 - 如何替换模板docx文档和Open XML SDK 2.0(2009年8月)中的内容?或者这个 - 如何从C#中的模板创建* .docx文件或谷歌它(虽然不是那么困难,整个解决方案更多的是几行代码并且需要一些理解)

  3. Use third party tools or controls - I'm not really familiar with them.

    使用第三方工具或控件 - 我并不熟悉它们。

#2


3  

You can create your template by creating a Word document ( including logos, header, footer, etc), putting the placeholders and save it as "Word 2003 xml" document.

您可以通过创建Word文档(包括徽标,页眉,页脚等)来创建模板,放置占位符并将其另存为“Word 2003 xml”文档。

Next, you can read the template.xml as a text file, replace the placeholders with the values, and response the entire string as a content type "application/msword"

接下来,您可以将template.xml作为文本文件读取,将占位符替换为值,并将整个字符串作为内容类型“application / msword”响应

The placeholder could be {MY_PLACEHOLDER1}

占位符可以是{MY_PLACEHOLDER1}

Before creating the template, you must deactivate ( in Word ) all related with Ortography, because the "bad ortography" highlightings are stored as a part of the xml document, and {MY_PLACEHOLDER1} can be stored as MY_PLACEHOLDER1 (the "{" and "}" characters are stored separated with their own word xml tags )

在创建模板之前,必须停用(在Word中)所有与Ortography相关的内容,因为“坏ortography”突出显示存储为xml文档的一部分,{MY_PLACEHOLDER1}可以存储为MY_PLACEHOLDER1(“{”和“ }“字符以自己的单词xml标签分隔存储)

Regards,

#3


2  

You can use TemplateEngine.Docx for replacing the text in template document

您可以使用TemplateEngine.Docx替换模板文档中的文本