Background
In some web applications it is required to create a document generated dynamically, like an Offer letter given to the candidate, some other official letters used during the interviewing purpose, display Whole results of the Interview rounds wise in only one page template or mark sheet templates etc.
In such type of documents most of the things are similar except for few changes.
Let’s take some examples.
1.Mark Sheet: Name, Marks Scored, Seat Number etc will change from student to student but other details like the Exam name, stream , university name etc. will remain same.
2. Offer letter: The name, address, post date of offer and date of joining, designation and pay structure etc. will change from candidate to candidate. But other details will remain same.
3. Hiring Assessment Sheet: The name, address, post date of offer and date of joining, designation display Whole results of the Interview rounds wise etc. will change from candidate to candidate. But other details will remain same.
Solution
To generate O/P for such type of requirement follow a step wise procedure given below.
Step1: Create A Word Template
Why Word template?
I. It is very easy to generate formatted O/P in HTML
II. Sub scripts and super scripts e.g. ™ can be easily written.
III. Maintenance of such documents can be easily done, if changes arise.
How to Create Template in any formate like in HTML or Word ?
e.g.
1. Offer Letter:
##Address##, ##DOF##
Dear ##Name##,
We are glad to appoint you for the designation ## desig ##.
- Mark sheet:
Name: ##NAME##
Seat Number: ##Number##
Subject1 ##SUB1##
Subject2 ##SUB2##
Subject3 ##SUB3##
Subject4 ##SUB4##
Subject5 ##SUB5##
Total ##Total##
Percentage: ##PER##
In the e.g.1 we see ##Address##, ##DOF##, ##NAME## and ##desig##. Allthese will be replaced bydynamic dataselectedby the user. Whereas the static data will remain thesame.
Similarly in example 2. for Mark sheet
##SUB1## , ##SUB2## , ##SUB3## etc. will be dynamic.
This dynamic data can be made available from the database.
Once you have formatted your template file in word as per the guidelinesoutlined above save the file. Provide appropriate name to the file.
Note: The saving format shouldbe Rich Text Format(RTF). Click on File – select save as and then select thepath and save the file as .rtf extension file.
Step2: Proceed with the code&instructions given below
Make use of file scripting object to copy the template file into sometemporary file.
Note: The path of the temporaryfile should be fixed and it should also have a .rtf extension as the templatefile.
‘ Declare ascripting object.
Dim td =Server.CreateObject("Scripting.FileSystemObject")
‘Copy the File
td.CopyFile(Server.MapPath(".")&"Template.rtf", Server.MapPath(".")&"Temporary.rtf")
‘ set the file path in a string variable.
strFilePath = Server.MapPath(".")&"Temporary.rtf"
‘Now open the termporary file in Read mode.
Dim fs2 As New FileStream(strFilePath,FileMode.Open, FileAccess.Read)
'Declare d as a StreamReader
Dim d As New StreamReader(fs2)
Dim swrtarget As String
'Initialisethe stream reader d to the begining of the file.
d.BaseStream.Seek(0,SeekOrigin.Begin)
swrtarget = d.ReadToEnd'Read the file from Start to End in one go.
d.Close()'Close the Stream Reader
Declare string variables say,
Str1 as strvalue1.
Now get the values from database.
Use can use a select query and a data reader to get thevalues.
Now set the file path to the Temporary file
'Set the pathto open Temporary
strFilePath = Server.MapPath(".")&"Temporary.rtf"
‘Open thetemporary file in Read-Write Mode.
Dim fs1 As New FileStream(strFilePath,FileMode.Open, FileAccess.ReadWrite)
Dims As New StreamWriter(fs1)'Declare a Stream Writer.
'Replace thevalues with the values in the string read from Temporary.
While myReader.Read
Strvalue1=myReader.GetValue(0).ToString()
Str1= Replace(swrtarget.ToString,##Address##",Trim(strvalue1)
Strvalue1=myReader.GetValue(1).ToString()
str1= Replace(str.ToString,##Name##",Trim(strvalue1)
Similarly it will come for ##DOF## and ##desig###.
EndWhile
Close the Reader.
Close the Connection.
' Afterreplacing all the values write these values in the file2.rtf via the file write(s).
s.WriteLine(str)
'After writingthe data in the stream writer the values must be flushed.
s.Flush()
'Close thestream writer.
s.Close()
fs1.Close()
fs2.Close()
Now copy the details intothe final file.
This file can be a .dcfile.
Dim td1 =Server.CreateObject("Scripting.FileSystemObject")
td1.CopyFile(Server.MapPath(".")&"Temporary.rtf", Server.MapPath(".")&"final.htm")
'Open a new windowin the browser and display final.doc file to the user.
strtargetFilepath = "finalletter.htm"
Response.Write("<script>window.open('"&strtargetFilepath&"')</script>")
Main Code for dynamic HTML Template Generation:=
Call this Method after Creating a template save the template file in template Folder
<SPAN style="COLOR: black"><SPAN style="mso-spacerun: yes">******************************************************************
Public Sub ReadCanMasterInfo()
'Declare StreamWriter
Dim dstFile1 As StreamWriter
Dim strFilePath As String
strFilePath = Server.MapPath(".") & "/template/output2.doc"
dstFile1 = System.IO.File.CreateText(strFilePath)
Try
Dim strvalue As String
Dim myReader As OleDb.OleDbDataReader
Dim strString As String
'Dim dstFile As System.IO.File
'dstFile1.Flush()
Dim cmd As OleDbCommand
'Declare StringWriter
Dim swrtarget As New System.IO.StringWriter
Dim str As String
Server.Execute("template/template1.aspx", swrtarget)
Dim SqlStr As String
SqlStr = " Your SQL-Query”
clsCon.OpenCon(ConfigurationSettings.AppSettings("strConnection").ToString)
cmd = New OleDb.OleDbCommand(SqlStr, clsCon.Con)
myReader = cmd.ExecuteReader
While myReader.Read
strvalue = myReader.GetValue(1).ToString()
str = Replace(swrtarget.ToString, "##candidatename##", strvalue)
strvalue = myReader.GetValue(2).ToString()
str = Replace(str, "##dateofbirth##", strvalue)
strvalue = myReader.GetValue(3).ToString()
str = Replace(str, "##canAge##", strvalue)
strvalue = myReader.GetValue(4).ToString()
str = Replace(str, "##cantelno##", strvalue)
strvalue = myReader.GetValue(5).ToString()
End while
myReader.Close()
cmd.Dispose()
'dstfile1 write only one time pass same str value
dstFile1.Write(str)
dstFile1.Flush()
dstFile1.Close()
'Open this file in the last when writes all thing to the file
Response.Write("<script> window.open('template/output2.doc') </script>")
Catch ex As Exception
Response.Write("" + ex.Message)
'Response.Write("")
dstFile1.Close()
clsCon.closeCon()
End Try
End Sub
******************************************************************
Advantages
- Easy maintainability – Changes in the template canbe made easily and updated.
- It reduce the Lines of code