Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » ASP.NET/Web Applications »
Creating word files with asp.net
|
Creating word files were also similar to creating html pages.
Html file is collection of html tags. Word file is collection of word tags.
Here html tags are very easy to remember but rtf tags are very difficult to remember.
For easy to remember the rtf tags, here I am creating one class called FilesSystem, this class contains some of the tags which are necessary to create rtf files. eg: Html Tags : For break : <br> Font color blue: <font color=”blue”>
Rtf: For break : \par Font color blue : "{\cf2"
FilesSystem Class
Public Class FilesSystem Public Function rtf_br() As String Return "\par" End Function Public Function rtf_br2() As String Return "\par\par" End Function Public Function rtf_bold(ByVal text As String) As String Return "\b " & text & " \b0 " End Function Public Function rtf_Italic(ByVal text As String) As String Return "\i " & text & " \i0" End Function Public Function rtf_ItalicAndBold(ByVal text As String) As String Return "\i " & "\b " & text & " \b0" & " \i0" End Function Public Function rtf_UnderLine(ByVal text As String) As String Return "\ul " & text & " \ul0" End Function Public Function rtf_BoldAndUnderLine(ByVal text As String) As String Return "\b " & "\ul " & text & " \ul0" & " \b0 " End Function
Public Function rtf_JustifyCenter() As String Return "\qc " End Function Public Function rtf_JustifyLeft() As String Return "\ql " End Function Public Function rtf_JustifyRight() As String Return "\qr " End Function Public Function rtf_LeftSingleQuote() As String Return "\lquote " End Function Public Function rtf_rightSingleQuote() As String Return "\rquote " End Function Public Function rtf_LeftDoubleQuotationMark() As String Return "\ldblquote " End Function Public Function rtf_RightDoubleQuotationMark() As String Return "\rdblquote " End Function Public Function rtf_AllCapitals() As String Return "\caps " End Function
Public Function rtf_FontSize(ByVal size As Integer) As String Return "\fs" & size End Function Public Function rtf_CurrentPageNumber() As String Return "\chpgn " End Function Public Function rtf_Bullet() As String Return "\bullet " End Function Public Function rtf_SmallCapitals() As String Return "\scaps " End Function Public Function rtf_PageBreak() As String Return "\page " End Function Public Function rtf_emdash() As String Return "\emdash " End Function Public Function rtf_Smalls() As String Return "\smls " End Function Public Function rtf_CurrentTime() As String Return "\chtime " End Function Public Function rtf_StartSetting() As String Return "{\rtf1\ansi\ansicpg1252\uc1 \deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f53\froman\fcharset238\fprq2 Times New Roman CE;}{\f54\froman\fcharset204\fprq2 Times New Roman Cyr;}{\f56\froman\fcharset161\fprq2 Times New Roman Greek;}{\f57\froman\fcharset162\fprq2 Times New Roman Tur;}{\f58\froman\fcharset177\fprq2 Times New Roman (Hebrew);}{\f59\froman\fcharset178\fprq2 Times New Roman (Arabic);}{\f60\froman\fcharset186\fprq2 Times New Roman Baltic;}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;\red255\green102\blue0;\red51\green153\blue102;\red51\green51\blue153;\red153\green51\blue0;\red204\green153\blue255;\red255\green153\blue0;\red51\green51\blue0;\red0\green51\blue0;\red255\green153\blue204;\red51\green204\blue204;\red153\green204\blue0;\red102\green102\blue153;\red153\green204\blue255;\red255\green204\blue0;\red153\green51\blue102;\red0\green51\blue102;\red0\green204\blue255;}{\stylesheet{\ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \snext0 Normal;}{\s1\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel0\adjustright\rin0\lin0\itap0 \fs40\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 1;}{\s2\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel1\adjustright\rin0\lin0\itap0 \fs40\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 2;}{\s3\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel2\adjustright\rin0\lin0\itap0 \fs40\cf5\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 3;}{\s4\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel3\adjustright\rin0\lin0\itap0 \fs40\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 4;}{\s5\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel4\adjustright\rin0\lin0\itap0 \fs40\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 5;}{\s6\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel5\adjustright\rin0\lin0\itap0 \fs40\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 6;}{\s7\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel6\adjustright\rin0\lin0\itap0 \fs40\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 7;}{\s8\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel7\adjustright\rin0\lin0\itap0 \fs40\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 8;}{\s9\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel8\adjustright\rin0\lin0\itap0 \fs40\cf1\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 \sbasedon0 \snext0 heading 9;}{\*\cs10 \additive Default Paragraph Font;}}{\info{\title LOD DOCUMENT}{\author HISHAM,SOBRI&KADIR}{\operator mms}{\creatim\yr2005\mo5\dy2\hr22\min6}{\revtim\yr2005\mo5\dy6\hr17\min49}{\version12}{\edmins28}{\nofpages2}{\nofwords0}{\nofchars0}{\*\company MMCSPL}{\nofcharsws0}{\vern8247}}\widowctrl\ftnbj\aenddoc\noxlattoyen\expshrtn\noultrlspc\dntblnsbdb\nospaceforul\hyphcaps0\horzdoc\dghspace120\dgvspace120\dghorigin1701\dgvorigin1984\dghshow0\dgvshow3\jcompress\viewkind1\viewscale100\nolnhtadjtbl \fet0\sectd \linex0\sectdefaultcl {\footer \pard\plain \qc \li0\ri0\nowidctlpar\brdrt\brdrs\brdrw10\brsp100 \faauto\rin0\lin0\itap0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 {\fs18 Page }{\field{\*\fldinst {\fs18 PAGE}}{\fldrslt {\fs18\lang1024\langfe1024\noproof 1}}}{\fs18 of }{\field{\*\fldinst {\fs18 NUMPAGES}}{\fldrslt {\fs18\lang1024\langfe1024\noproof 2}}}{\fs18 \par }}{\*\pnseclvl1\pnucrm\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl2\pnucltr\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl3\pndec\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang{\pntxta )}}{\*\pnseclvl5\pndec\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl6\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl8\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}\pard\plain \s9\ql \li0\ri0\keepn\widctlpar\aspalpha\aspnum\faauto\outlinelevel8\adjustright\rin0\lin0\itap0 \fs40\cf1\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 " End Function
Public Function FontColor_Blue() As String Return "{\cf2" End Function Public Function FontColor_BrightGreen() As String Return "{\cf4" End Function Public Function FontColor_Pink() As String Return "{\cf5" End Function Public Function FontColor_Yellow() As String Return "{\cf7" End Function
Public Function FontColor_White() As String Return "{\cf8" End Function Public Function FontColor_Teal() As String Return "{\cf10" End Function Public Function FontColor_DarkRed() As String Return "{\cf13" End Function Public Function FontColor_DarkYellow() As String Return "{\cf14" End Function Public Function FontColor_Gray50() As String Return "{\cf15" End Function Public Function FontColor_Gray25() As String Return "{\cf16" End Function Public Function FontColor_Red() As String Return "{\cf17" End Function Public Function FontColor_Orange() As String Return "{\cf17 " End Function Public Function Fontcolor_Close() As String Return " }" End Function Public Function FontColor_SeaGreen() As String Return "{\cf18" End Function Public Function FontColor_Indigo() As String Return "{\cf19" End Function Public Function FontColor_Levender() As String Return "{\cf21" End Function Public Function FontColor_Brown() As String Return "{\cf20" End Function Public Function FontColor_LightOrange() As String Return "{\cf22" End Function
Public Function FontColor_LightGreen() As String Return "{\cf22" End Function Public Function FontColor_OliveGreen() As String Return "{\cf23" End Function Public Function FontColor_DarkGreen() As String Return "{\cf24" End Function Public Function FontColor_Rose() As String Return "{\cf25" End Function Public Function FontColor_Aqua() As String Return "{\cf26" End Function Public Function FontColor_Lime() As String Return "{\cf27" End Function Public Function FontColor_BlueGray() As String Return "{\cf28" End Function Public Function FontColor_PaleBlue() As String Return "{\cf29" End Function Public Function FontColor_Gold() As String Return "{\cf30" End Function Public Function FontColor_Plum() As String Return "{\cf31" End Function Public Function FontColor_SktBlue() As String Return "{\cf33" End Function Public Function FontColor_DarkTeal() As String Return "{\cf32" End Function Public Function FontColor_Black() As String Return "{ " End Function Public Function FontColor_Nth(ByVal n As Integer, ByVal text As String) As String Return "{\cf" & n End Function
Public Function rtf_BackGroundColor() As String Return "\cb75 " End Function Public Function rtf_UnderLineColor(ByVal text As String) As String Return "\ulc35 " End Function Public Function FileExtension(ByVal filename As String) Dim sExt As String sExt = Left(StrReverse(filename), 3) Return StrReverse(sExt) End Function
Public Function table_RowStart(ByVal ColSize As Integer) As String Dim rowStrTag1, StrCellWidth, rowStrTag2, rowStrTag4, endTag, strMidTag As String Dim i As Integer Dim iCellWidth As Long rowStrTag1 = "\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trbrdrh\brdrs\brdrw10\trbrdrv\brdrs\brdrw10\trftsWidth1\trautofit1\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10" rowStrTag2 = "\clbrdrr\brdrs\brdrw10\cltxlrtb\clftsWidth3\clwWidth1771\cellx" rowStrTag4 = "\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10" iCellWidth = 1663 For i = 1 To ColSize - 1 strMidTag = strMidTag & rowStrTag2 & CType(iCellWidth, String) & rowStrTag4 iCellWidth = iCellWidth + 1771 Next iCellWidth = iCellWidth + 1 endTag = "\clbrdrr\brdrs\brdrw10 \cltxlrtb\clftsWidth3\clwWidth1772\cellx" & CType(iCellWidth, String) & " "
rowStrTag1 = rowStrTag1 & strMidTag & endTag Return rowStrTag1 End Function Public Function table_RowClose(ByVal ColSize As Integer) As String Dim rowStrTag1, StrCellWidth, rowStrTag2, rowStrTag4, endTag, strMidTag As String Dim i As Integer Dim iCellWidth As Long rowStrTag1 = "{\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10\trbrdrl\brdrs\brdrw10\trbrdrb\brdrs\brdrw10\trbrdrr\brdrs\brdrw10\trbrdrh\brdrs\brdrw10\trbrdrv\brdrs\brdrw10\trftsWidth1\trautofit1\trpaddl108\trpaddr108\trpaddfl3\trpaddfr3\clvertalt\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clbrdrb\brdrs\brdrw10" rowStrTag2 = "\clbrdrr\brdrs\brdrw10 \cltxlrtb\clftsWidth3\clwWidth1771\cellx" rowStrTag4 = "\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10" iCellWidth = 1663 For i = 1 To ColSize - 1 strMidTag = strMidTag & rowStrTag2 & CType(iCellWidth, String) & rowStrTag4 iCellWidth = iCellWidth + 1771 Next iCellWidth = iCellWidth + 1 endTag = "\clbrdrr\brdrs\brdrw10 \cltxlrtb\clftsWidth3\clwWidth1772\cellx" & iCellWidth & "\row}"
rowStrTag1 = rowStrTag1 & strMidTag & endTag Return rowStrTag1 End Function Public Function Table_CellsStart() As String Return "\pard\plain \ql \li0\ri0\widctlpar\intbl\aspalpha\aspnum\faauto\adjustright\rin0\lin0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 {" End Function Public Function Table_CellsClose() As String Return "}\pard \ql \li0\ri0\widctlpar\intbl\aspalpha\aspnum\faauto\adjustright\rin0\lin0" End Function Public Function Table_CellText(ByVal text As String) As String Return text & "\cell" End Function
Public Function rtf_CloseTag() As String Return "}" End Function
Public Function table_CloseTag() As String Return "\pard \ql \li0\ri0\widctlpar\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0 {\par }" End Function
End Class
Creating rtf files using the above FilesSystem class.
First we have to create one text file with file system object then we have to write the tags line by line. the starting tag (rtf_StartSetting) and end tag "}" are must required.
Start tag: rtf_StartSetting End tag : "}" Dim fso, myfile Dim fsD As New FilesSystem Dim fName As String = "WordRpt1" & ".rtf" Try Dim FILE_NAME As String = Server.MapPath("\rnd3\Files\") & fName If File.Exists(FILE_NAME) Then openDocument(fName) Exit Sub End If fso = CreateObject("Scripting.FileSystemObject") myfile = fso.CreateTextFile(Server.MapPath("\rnd3\Files\") & fName, True)
myfile.Writeline(rtf_StartSetting) ....... ....... ....... ....... myfile.Writeline("}")
Example 1:
Dim fso, myfile Dim fsD As New FilesSystem Dim fName As String = "WordRpt1" & ".rtf" Try Dim FILE_NAME As String = Server.MapPath("\rnd3\Files\") & fName If File.Exists(FILE_NAME) Then openDocument(fName) Exit Sub End If fso = CreateObject("Scripting.FileSystemObject") myfile = fso.CreateTextFile(Server.MapPath("\rnd3\Files\") & fName, True) myfile.Writeline(fsD.rtf_StartSetting)
myfile.Writeline(fsD.rtf_JustifyCenter) myfile.Writeline(fsD.rtf_FontSize(38)) myfile.Writeline(fsD.FontColor_Indigo) myfile.Writeline(fsD.rtf_bold("Welcome to word report")) myfile.Writeline(fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br) myfile.Writeline("}") openDocument(fName)
Catch ex As Exception Response.Write(ex.Message) End Try
Example 2 :
Dim fso, myfile Dim fsD As New FilesSystem Dim fName As String = "WordRpt2" & ".rtf" Try Dim FILE_NAME As String = Server.MapPath("\rnd3\Files\") & fName If File.Exists(FILE_NAME) Then openDocument(fName) Exit Sub End If fso = CreateObject("Scripting.FileSystemObject") myfile = fso.CreateTextFile(Server.MapPath("\rnd3\Files\") & fName, True) myfile.Writeline(fsD.rtf_StartSetting)
myfile.Writeline(fsD.rtf_JustifyCenter) myfile.Writeline(fsD.rtf_FontSize(38)) myfile.Writeline(fsD.FontColor_Indigo) myfile.Writeline(fsD.rtf_bold("Welcome to word report")) myfile.Writeline(fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br)
myfile.Writeline(fsD.rtf_FontSize(30)) myfile.Writeline(fsD.FontColor_Blue) myfile.Writeline(fsD.rtf_BoldAndUnderLine("Table Of Contents ") & fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br2)
myfile.Writeline(fsD.rtf_FontSize(28)) myfile.Writeline(fsD.FontColor_Orange) myfile.Writeline(fsD.rtf_BoldAndUnderLine("1. Introduction") & fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br2)
myfile.Writeline(fsD.rtf_FontSize(28)) myfile.Writeline(fsD.FontColor_Orange) myfile.Writeline(fsD.rtf_BoldAndUnderLine("2. Lession1") & fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br2)
myfile.Writeline(fsD.rtf_FontSize(28)) myfile.Writeline(fsD.FontColor_Orange) myfile.Writeline(fsD.rtf_BoldAndUnderLine("3. Lession2") & fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br2)
myfile.Writeline(fsD.rtf_FontSize(28)) myfile.Writeline(fsD.FontColor_Orange) myfile.Writeline(fsD.rtf_BoldAndUnderLine("4. Lession3") & fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br2)
myfile.Writeline(fsD.rtf_FontSize(28)) myfile.Writeline(fsD.FontColor_Orange) myfile.Writeline(fsD.rtf_BoldAndUnderLine("5. Lession4") & fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br2)
myfile.Writeline("}") openDocument(fName)
Catch ex As Exception Response.Write(ex.Message) End Try
Example 2 : Creating Table while creating table the following tags are required to create one row. here I am passing 2 as a parameter in RowStart and RowClose tags. this value idicate the number of columns.
table_RowStart(2) Table_CellsStart) Table_CellText("Name :") Table_CellText("Class") Table_CellsClose table_RowClose(2)
Dim fso, myfile Dim fsD As New FilesSystem Dim fName As String = "WordRpt3" & ".rtf" Try Dim FILE_NAME As String = Server.MapPath("\rnd3\Files\") & fName If File.Exists(FILE_NAME) Then openDocument(fName) Exit Sub End If fso = CreateObject("Scripting.FileSystemObject") myfile = fso.CreateTextFile(Server.MapPath("\rnd3\Files\") & fName, True) myfile.Writeline(fsD.rtf_StartSetting)
myfile.Writeline(fsD.rtf_JustifyLeft) myfile.Writeline(fsD.rtf_FontSize(38)) myfile.Writeline(fsD.FontColor_Indigo) myfile.Writeline(fsD.rtf_bold("Welcome to word report")) myfile.Writeline(fsD.Fontcolor_Close) myfile.Writeline(fsD.rtf_br)
Dim iCnt = 2 myfile.Writeline(fsD.table_RowStart(iCnt)) myfile.Writeline(fsD.Table_CellsStart) myfile.Writeline(fsD.rtf_bold(fsD.Table_CellText("Name :"))) myfile.Writeline(fsD.Table_CellText(" ")) myfile.Writeline(fsD.Table_CellsClose) myfile.Writeline(fsD.table_RowClose(iCnt))
myfile.Writeline(fsD.table_RowStart(iCnt)) myfile.Writeline(fsD.Table_CellsStart) myfile.Writeline(fsD.rtf_bold(fsD.Table_CellText("Calass : "))) myfile.Writeline(fsD.Table_CellText(" ")) myfile.Writeline(fsD.Table_CellsClose) myfile.Writeline(fsD.table_RowClose(iCnt))
myfile.Writeline(fsD.table_RowStart(iCnt)) myfile.Writeline(fsD.Table_CellsStart) myfile.Writeline(fsD.rtf_bold(fsD.Table_CellText("School : "))) myfile.Writeline(fsD.Table_CellText(" ")) myfile.Writeline(fsD.Table_CellsClose) myfile.Writeline(fsD.table_RowClose(iCnt))
myfile.Writeline(fsD.table_RowStart(iCnt)) myfile.Writeline(fsD.Table_CellsStart) myfile.Writeline(fsD.rtf_bold(fsD.Table_CellText("City : "))) myfile.Writeline(fsD.Table_CellText(" ")) myfile.Writeline(fsD.Table_CellsClose) myfile.Writeline(fsD.table_RowClose(iCnt)) myfile.Writeline(fsD.table_CloseTag)
myfile.Writeline(fsD.rtf_br2) myfile.Writeline("}") openDocument(fName)
Catch ex As Exception Response.Write(ex.Message) End Try
OPEN DOCUMET
Sub openDocument(ByVal fName As String) Try Dim pstrContentType As String Dim dPath As String dPath = "Files/" & fName Response.Write("<script language=javascript> window.open('" & dPath & "','_blank');</script>") Catch EX As System.Exception Response.Write(EX.Message)
End Try
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|