Public Sub GetOutput()
' Declare a scripting object.
Dim td As Object = Server.CreateObject("Scripting.FileSystemObject")
'Copy File
td.CopyFile(Server.MapPath("./") & "test.rtf", Server.MapPath("./") & "temp.rtf")
' set the file path in a string variable.
Dim strFilePath As String = Server.MapPath("./") & "temp.rtf"
'Now open the termporary file in Read mode.
Dim fs2 As New IO.FileStream(strFilePath, IO.FileMode.Open, IO.FileAccess.Read)
'Declare d as a stream Reader
Dim d As New IO.StreamReader(fs2)
Dim swrtarget As String
'Initialise the stream reader d to the begining of the file.
d.BaseStream.Seek(0, IO.SeekOrigin.Begin)
swrtarget = d.ReadToEnd 'Read the file from Start to End in one go.
d.Close() ' Close the Stream Reader
Dim str As String
Dim strValue As String
Dim str1 As String
Dim strValue1 As String
'Now get the values from database.
'Use can use a select query and a data reader to get the values.
'Now set the file path to the Temporary file
'Set the path to open Temporary
strFilePath = Server.MapPath("./") & "temp.rtf"
'Open the temporary file in Read-Write Mode.
Dim fs1 As New IO.FileStream(strFilePath, IO.FileMode.Open, IO.FileAccess.ReadWrite)
Dim s As New IO.StreamWriter(fs1) 'Declare a Stream Writer.
'Replace the values with the values in the string read from Temporary.
Dim ObjConn As New SqlConnection(strConn)
Dim strSQL As String = "select common_key ,author from tbl_meta where ID = 1"
Dim objComm As New SqlCommand(strSQL, ObjConn)
ObjConn.Open()
Dim myReader As SqlDataReader = objComm.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read
strValue1 = myReader.GetValue(0).ToString()
str1 = Replace(swrtarget.ToString, "##Address##", Trim(strValue1))
strValue1 = myReader.GetValue(1).ToString()
str1 = Replace(swrtarget.ToString, "##Name##", Trim(strValue1))
'Similarly it will come for ##DOF## and ##desig###.
End While
'Close the Reader.
myReader.Close()
myReader = Nothing
'Close the Connection.
ObjConn.Close()
ObjConn.Dispose()
ObjConn = Nothing
' After replacing all the values write these values in the file2.rtf via the file write (s).
s.WriteLine(str)
'After writing the data in the stream writer the values must be flushed.
s.Flush()
'Close the stream writer.
s.Close()
fs1.Close()
fs2.Close()
'Now copy the details into the final file.
'This file can be a .dc file.
Dim td1 = Server.CreateObject("Scripting.FileSystemObject")
td1.CopyFile(Server.MapPath("./") & "temp.rtf", Server.MapPath("./") & "final.doc")
'Open a new window in the browser and display final.doc file to the user.
Dim strtargetFilepath As String = " final.doc"
Response.Write("<script> window.open('" & strtargetFilepath & "') </script>")
End Sub
|