我正在尋找從Visual Basic或Visual C#中填充Word文檔的選項。我目前正在使用合併字段,下面的代碼填寫Word文檔中的特定字段,但現在我遇到了需要將表格數據推送到MS Word的情況。無論如何要從網格視圖獲取數據(行數是動態的),並使用合併字段或類似的東西將其導入到Word文檔表中?我要保持我的模板文檔的格式,並希望能夠控制頁面佈局..如何在ASP.NET中使用Visual Basic/Visual C#填充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
你有什麼例子嗎?我需要爲整個文檔寫出HTML嗎?這似乎可能會相當耗時,並且需要很多代碼... – 2011-05-24 17:56:04
爲什麼不用一個html table來代替其他合併域? – hunter 2011-05-24 17:58:49
我希望這就是你的意思...所以如果我有一個網格視圖,我不能使用HTML編寫器輸出到合併領域的gridview html?有什麼樣式的方式? – 2011-05-24 18:01:58