I have a Word Document stored on a server I wish to use as a template. I need to replace certain text with data from a database.
The two issues I have are:
No office on the server to use Microsoft.Office.Interop, and I cannot save any documents to the server.
I think I am on the right track, but cannot come up with a viable solution. I am thinking my best route is to read into memory and use a byte array to allow the user to save the file.
I was doing something like this, but am currently stumped.
Dim path As String = HttpContext.Current.Request.PhysicalApplicationPath & "Letters\Test.docx" Dim docBA As Byte() = File.ReadAllBytes(path) Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(path, True) Using (wordDoc) Dim docText As String = Nothing Dim sr As StreamReader = New StreamReader(wordDoc.MainDocumentPart.GetStream) Using (sr) docText = sr.ReadToEnd End Using Dim regexText As Regex = New Regex("FIRST_NAME") docText = regexText.Replace(docText, "TESTING!!!") Dim sw As StreamWriter = New StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)) Dim modBA As Byte() Using (sw) sw.Write(docText) modBA = sw.Encoding.GetBytes(sw.BaseStream, 0, sw.BaseStream.Length) HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename= DownloadSample.docx") HttpContext.Current.Response.ContentType = "application/octectstream" HttpContext.Current.Response.BinaryWrite(modBA) HttpContext.Current.Response.End() End Using