如何在 ASP.NET 中使用 Visual Basic / Visual C# 填充 Word 文档

发布于 2024-11-09 18:40:04 字数 1665 浏览 0 评论 0原文

我正在寻找从 Visual Basic 或 Visual C# 填充 Word 文档的选项。我目前正在使用合并字段和下面的代码来填充 Word 文档中的特定字段,但现在我遇到了需要将表格数据推送到 MS Word 的情况。无论如何,是否可以从网格视图中获取数据(行数是动态的),并使用合并字段或类似的东西将其导入到Word文档表中?我必须维护模板文档的格式,并且希望能够控制页面的布局..

            Dim templateDoc As String = Server.MapPath("\Userfiles\docs\" & location)
            Dim mergePath As String = Server.MapPath("\App_Data\Temp\")
            Dim mergeFileName As String = location.Replace("/", "_") & ".docx"
            Dim mergeDoc As String = mergePath & "\" & mergeFileName

            File.Copy(templateDoc, mergeDoc, True)

            Using pkg As Package = Package.Open(mergeDoc, FileMode.Open, FileAccess.ReadWrite)

                Dim uri As Uri = New Uri("/word/document.xml", UriKind.Relative)
                Dim part As PackagePart = pkg.GetPart(uri)

                Dim xmlMainXMLDoc As XmlDocument = New XmlDocument()
                xmlMainXMLDoc.Load(part.GetStream(FileMode.Open, FileAccess.Read))

                Dim innerXml As String = xmlMainXMLDoc.InnerXml _
                                         .Replace("«Corporate Legal Name»", businessName) _
                                         .Replace("«Address 1»", mailingAddress1) _
                                         .Replace("«Address 2»", mailingAddress2) _
                                         .Replace("«City»", city)
                xmlMainXMLDoc.InnerXml = innerXml

                Using partWriter As New StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write))
                    xmlMainXMLDoc.Save(partWriter)
                End Using

                pkg.Close()
            End Using

I'm looking for options to fill a Word Document from either Visual Basic, or Visual C#. I'm currently using merge fields, and the code below to fill specific fields in a Word Document, but now I've run into a situation where I need tabular data pushed to MS Word. Is there anyway to take data from a grid view (number of rows is dynamic), and import it into a Word Document Table using a merge field or something of that sort? I have to maintain the format of my template doc, and would like to be able to control the layout of the page ..

            Dim templateDoc As String = Server.MapPath("\Userfiles\docs\" & location)
            Dim mergePath As String = Server.MapPath("\App_Data\Temp\")
            Dim mergeFileName As String = location.Replace("/", "_") & ".docx"
            Dim mergeDoc As String = mergePath & "\" & mergeFileName

            File.Copy(templateDoc, mergeDoc, True)

            Using pkg As Package = Package.Open(mergeDoc, FileMode.Open, FileAccess.ReadWrite)

                Dim uri As Uri = New Uri("/word/document.xml", UriKind.Relative)
                Dim part As PackagePart = pkg.GetPart(uri)

                Dim xmlMainXMLDoc As XmlDocument = New XmlDocument()
                xmlMainXMLDoc.Load(part.GetStream(FileMode.Open, FileAccess.Read))

                Dim innerXml As String = xmlMainXMLDoc.InnerXml _
                                         .Replace("«Corporate Legal Name»", businessName) _
                                         .Replace("«Address 1»", mailingAddress1) _
                                         .Replace("«Address 2»", mailingAddress2) _
                                         .Replace("«City»", city)
                xmlMainXMLDoc.InnerXml = innerXml

                Using partWriter As New StreamWriter(part.GetStream(FileMode.Open, FileAccess.Write))
                    xmlMainXMLDoc.Save(partWriter)
                End Using

                pkg.Close()
            End Using

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-11-16 18:40:18

就像我对您的回答关于 Excel 的问题Office writer 适合您这里也有!

Just like my answer for your question regarding Excel, Office writer will work for you here too!

泪痕残 2024-11-16 18:40:15

您可以用 HTML 格式编写并使用 .doc 扩展名保存,Word 会妥善处理它。

You can write out in HTML and save it with a .doc extension and Word will handle it gracefully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文