web 2.0

ASP.NET(vb.net) & Add Style and Format into excel sheet (Excel.Application)

ASP.NET(vb.net) & Add Style & Format into excel sheet (Excel.Application) - This article example scripts you will learn how to add font style format into excel sheet using ASP.NET Scripts.,

ShotDev Focus:
- ASP.NET(vb.net) & Add Style & Format into excel sheet (Excel.Application)

Example

AspNetCreateFormatStyle.aspx

  1. <%@ Import Namespace="Excel"%>  
  2. <%@ Import Namespace="System.IO"%>  
  3. <%@ Page Language="VB" %>  
  4. <script runat="server">  
  5. Sub Page_Load(sender As Object, e As EventArgs)  
  6.   
  7. Dim FileName As String = "MyXls/MyExcel.xls"  
  8.   
  9. '*** Create Excel.Application ***'  
  10. Dim xlApp As New Excel.Application  
  11. Dim xlBook As Excel.Workbook  
  12.   
  13. xlBook = xlApp.Workbooks.Add()  
  14. xlBook.Application.Visible = False  
  15.   
  16. '*** Create Sheet 1 ***'  
  17. xlBook.Worksheets(1).Name = "My Sheet1"  
  18. xlBook.Worksheets(1).Select()  
  19.   
  20. '*** Width & Height (A1:A1) ***'  
  21. With xlApp.ActiveSheet.Range("A1:A1")  
  22. .ColumnWidth = 40.0  
  23. .RowHeight = 25.0  
  24. End With  
  25.   
  26. '*** Write text to Row 1 Column 1 ***'  
  27. With xlApp.ActiveSheet.Cells(1, 1)  
  28. .Value = "ShotDev.Com "  
  29. .Font.Name = "Tahoma"  
  30. .Font.Bold = True  
  31. .VerticalAlignment = -4108 '*** Center Rows ***'  
  32. .HorizontalAlignment = -4108 '*** Center Column ***'  
  33. .Font.Size = 12  
  34. End With  
  35.   
  36. '*** Width & Height (A1:B1) ***'  
  37. With xlApp.ActiveSheet.Range("A1:B1")  
  38. End With  
  39.   
  40. '*** Write text to Row 1 Column 2 ***'  
  41. With xlApp.ActiveSheet.Cells(1, 2)  
  42. .Value = "Mr.Weerachai Nukitram "  
  43. .Font.Name = "Tahoma"  
  44. .Font.Size = 20  
  45. End With  
  46.   
  47. '*** Width & Height (A2:A2) ***'  
  48. With xlApp.ActiveSheet.Range("A2:A2")  
  49. .BORDERS.Weight = 1 '*** Border ***'  
  50. End With  
  51.   
  52. '*** Write text to Row 1 Column 2 ***'  
  53. With xlApp.ActiveSheet.Cells(2, 1)  
  54. .Value = "I Love ShotDev.Com "  
  55. .Font.Name = "Tahoma"  
  56. .Font.Size = 10  
  57. .HorizontalAlignment = 4  
  58. End With  
  59.   
  60. '*** Width & Height (A3:D3) ***'  
  61. With xlApp.ActiveSheet.Range("A3:D3")  
  62. .BORDERS.Color = RGB(0, 0, 0) '*** Border Color ***'  
  63. .BORDERS.Weight = 1 '*** Border ***'  
  64. .MergeCells = True '*** Merge Cells ***'  
  65. End With  
  66.   
  67. '*** Write text to Row 1 Column 2 ***'  
  68. With xlApp.ActiveSheet.Cells(3, 1)  
  69. .Value = "I Love My Live"  
  70. .Font.Name = "Tahoma"  
  71. .Font.Size = 10  
  72. .HorizontalAlignment = -4108  
  73. .Interior.ColorIndex = 44 '*** Background Color ***'  
  74. End With  
  75.   
  76. '*** Write text to Row 4 Column 5 ***'  
  77. With xlApp.ActiveSheet.Cells(4, 5)  
  78. .Value = "My Live"  
  79. .Font.Name = "Tahoma"  
  80. .Font.Size = 10  
  81. .Font.Italic = True  
  82. .Font.ColorIndex = 4  
  83. .EntireColumn.AutoFit() '*** AutoFit Column ***'  
  84. End With  
  85.   
  86. '*** Write text to Row 5 Column 5 ***'  
  87. With xlApp.ActiveSheet.Cells(5,5)  
  88. .Value = "My Life"  
  89. .Font.Name = "Tahoma"  
  90. .Font.Size = 10  
  91. .Font.Italic = True  
  92. .Font.ColorIndex = 4  
  93. .HorizontalAlignment = -4152 '*** Text align Right ***'  
  94. .EntireColumn.AutoFit  '*** AutoFit Column ***'  
  95. End With  
  96.   
  97. '*** Write text to Row 6 Column 1 ***'  
  98. With xlApp.ActiveSheet.Cells(6,1)  
  99. .Value = "Version 2010"  
  100. .Font.Name = "Tahoma"  
  101. .Font.Size = 10  
  102. .Font.ColorIndex = 4  
  103. .Characters(8, 12).Font.Bold = True  
  104. End With  
  105.   
  106. '** SheetType  
  107. 'xlChart = -4109;  
  108. 'xlWorksheet = -4167;  
  109. '** WBATemplate  
  110. 'xlWBATWorksheet = -4167;  
  111. 'xlWBATChart = -4109;  
  112. '** Page Setup  
  113. 'xlPortrait = 1;  
  114. 'xlLandscape = 2;  
  115. 'xlPaperA4 = 9;  
  116. '** Format Cells  
  117. 'xlBottom = -4107;  
  118. 'xlLeft = -4131;  
  119. 'xlRight = -4152;  
  120. 'xlTop = -4160;  
  121. '** Text Alignment  
  122. 'xlHAlignCenter = -4108;  
  123. 'xlVAlignCenter = -4108;  
  124. '** Cell Borders  
  125. 'xlThick = 4;  
  126. 'xlThin = 2;  
  127.   
  128. '*** If Files Already Exist Delete files ***'  
  129. Dim MyFile As New FileInfo(Server.MapPath(FileName))  
  130. If MyFile.Exists Then  
  131. MyFile.Delete()  
  132. End IF  
  133. MyFile = Nothing  
  134.   
  135. '*** Save Excel ***'  
  136. 'xlSheet1.PrintOut 1 '*** Print to printer ***'  
  137. xlBook.SaveAs(Server.MapPath(FileName))  
  138. xlApp.Application.Quit()  
  139.   
  140. '*** Quit and Clear Object ***'  
  141. xlBook = Nothing  
  142. xlApp = Nothing  
  143.   
  144. Me.lblText.Text = "Excel Created <a href="& FileName & ">Click here</a> to Download."  
  145.   
  146. End Sub  
  147.   
  148. </script>  
  149. <html>  
  150. <head>  
  151. <title>ShotDev.Com Tutorial</title>  
  152. </head>  
  153. <body>  
  154. <form id="form1" runat="server">  
  155. <asp:Label id="lblText" runat="server"></asp:Label>  
  156. </form>  
  157. </body>  
  158. </html>  

Screenshot

ASP.NET(vb.net) & Add Style & Format into excel sheet (Excel.Application)

.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.