web 2.0

ASP.NET(vb.net) & Word (Word.Application) - Word Document Sample Report

ASP.NET(vb.net) & Word (Word Application) - Word Document Sample Report - This article example scripts you will learn how to Create Word Document sample report using ASP.NET Scripts

ShotDev Focus:
- ASP.NET(vb.net) & Word (Word Application) - Word Document Sample Report

Example

AspNetWordReport.aspx

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="AspNetWordReport.aspx.vb" Inherits="AspNetWordReport" %>  
  2. <html>  
  3. <head runat="server">  
  4. <title>ShotDev.Com Tutorial</title>  
  5. </head>  
  6. <body>  
  7. <form id="form1" runat="server">  
  8. <asp:Label ID="lblText" runat="server"></asp:Label>  
  9. </form>  
  10. </body>  
  11. </html>  

AspNetWordReport.aspx.vb

  1. Imports Microsoft.Office.Interop.Word  
  2. Imports System.Data  
  3. Imports System.Data.OleDb  
  4. Public Class AspNetWordReport  
  5. Inherits System.Web.UI.Page  
  6.   
  7. Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  8.   
  9. Const wdAlignParagraphCenter = 1  
  10. Const wdAlignParagraphRight = 2  
  11.   
  12. Dim Wrd As New Microsoft.Office.Interop.Word.Application  
  13. Dim WrdDoc As Microsoft.Office.Interop.Word.Document  
  14. Dim MyRange1, MyRange2, MyRange3 As Microsoft.Office.Interop.Word.Range  
  15. Dim objTable As Microsoft.Office.Interop.Word.Table  
  16. Dim DocName As String = "MyDoc/MyWord.doc"  
  17. Dim intRows As Integer  
  18. Wrd.Application.Visible = False  
  19.   
  20. WrdDoc = Wrd.Documents.Open(Server.MapPath("shotdev.dot"))  
  21.   
  22. MyRange1 = WrdDoc.Paragraphs.Add.Range  
  23. With MyRange1  
  24. .ParagraphFormat.Alignment = wdAlignParagraphCenter  
  25. .Font.Name = "Verdana"  
  26. .Font.Size = "20"  
  27. .Font.Bold = True  
  28. .InsertBefore("Customer Report" & vbCrLf)  
  29. End With  
  30.   
  31. '*** DataTable ***'  
  32. Dim objConn As OleDbConnection  
  33. Dim objCmd As OleDbCommand  
  34. Dim dtAdapter As OleDbDataAdapter  
  35. Dim dt As New DataTable  
  36. Dim strConnString As String  
  37. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("database/mydatabase.mdb") & ";Jet OLEDB:Database Password=;"  
  38. objConn = New OleDbConnection(strConnString)  
  39. objConn.Open()  
  40.   
  41. Dim strSQL As String  
  42. strSQL = "SELECT * FROM customer"  
  43. dtAdapter = New OleDbDataAdapter(strSQL, objConn)  
  44. dtAdapter.Fill(dt)  
  45. dtAdapter = Nothing  
  46. objConn.Close()  
  47. objConn = Nothing  
  48. '*** (End) DataTable ***'  
  49.   
  50. MyRange2 = WrdDoc.Paragraphs.Add.Range  
  51. With MyRange2  
  52. .Font.Size = "10"  
  53. End With  
  54. objTable = Wrd.ActiveDocument.Tables.Add(MyRange2, dt.Rows.Count, 6, 1, 2) '** Range,Rows,Column **'  
  55.   
  56. '*** Header ***'  
  57. objTable.Cell(1, 1).Range.InsertAfter("CustomerID")  
  58. objTable.Cell(1, 1).Range.Bold = True  
  59. objTable.Cell(1, 1).Range.ParagraphFormat.Alignment = 1  
  60.   
  61. objTable.Cell(1, 2).Range.InsertAfter("Name")  
  62. objTable.Cell(1, 2).Range.Bold = True  
  63. objTable.Cell(1, 2).Range.ParagraphFormat.Alignment = 1  
  64.   
  65. objTable.Cell(1, 3).Range.InsertAfter("Email")  
  66. objTable.Cell(1, 3).Range.Bold = True  
  67. objTable.Cell(1, 3).Range.ParagraphFormat.Alignment = 1  
  68.   
  69. objTable.Cell(1, 4).Range.InsertAfter("CountryCode")  
  70. objTable.Cell(1, 4).Range.Bold = True  
  71. objTable.Cell(1, 4).Range.ParagraphFormat.Alignment = 1  
  72.   
  73. objTable.Cell(1, 5).Range.InsertAfter("Budget")  
  74. objTable.Cell(1, 5).Range.Bold = True  
  75. objTable.Cell(1, 5).Range.ParagraphFormat.Alignment = 1  
  76.   
  77. objTable.Cell(1, 6).Range.InsertAfter("Used")  
  78. objTable.Cell(1, 6).Range.Bold = True  
  79. objTable.Cell(1, 6).Range.ParagraphFormat.Alignment = 1  
  80.   
  81. '*** Detail ***  
  82. For intRows = 0 To dt.Rows.Count - 1  
  83. objTable.Cell(intRows + 2, 1).Range.InsertAfter(dt.Rows(intRows)("CustomerID"))  
  84. objTable.Cell(intRows + 2, 1).Range.ParagraphFormat.Alignment = 1  
  85.   
  86. objTable.Cell(intRows + 2, 2).Range.InsertAfter(dt.Rows(intRows)("Name"))  
  87. objTable.Cell(intRows + 2, 2).Range.ParagraphFormat.Alignment = 0  
  88.   
  89. objTable.Cell(intRows + 2, 3).Range.InsertAfter(dt.Rows(intRows)("Email"))  
  90. objTable.Cell(intRows + 2, 3).Range.ParagraphFormat.Alignment = 0  
  91.   
  92. objTable.Cell(intRows + 2, 4).Range.InsertAfter(dt.Rows(intRows)("CountryCode"))  
  93. objTable.Cell(intRows + 2, 4).Range.ParagraphFormat.Alignment = 1  
  94.   
  95. objTable.Cell(intRows + 2, 5).Range.InsertAfter(FormatNumber(dt.Rows(intRows)("Budget"), 2))  
  96. objTable.Cell(intRows + 2, 5).Range.ParagraphFormat.Alignment = 2  
  97.   
  98. objTable.Cell(intRows + 2, 6).Range.InsertAfter(FormatNumber(dt.Rows(intRows)("Used"), 2))  
  99. objTable.Cell(intRows + 2, 6).Range.ParagraphFormat.Alignment = 2  
  100. Next  
  101.   
  102. MyRange3 = WrdDoc.Paragraphs.Add.Range  
  103. With MyRange3  
  104. .ParagraphFormat.Alignment = wdAlignParagraphRight  
  105. .Font.Name = "Verdana"  
  106. .Font.Size = "10"  
  107. .InsertBefore(vbCrLf & vbCrLf & vbCrLf & "................................Manager" & vbCrLf & Now())  
  108. End With  
  109.   
  110. WrdDoc.SaveAs(Server.MapPath(DocName))  
  111. Wrd.Application.Quit()  
  112. Wrd = Nothing  
  113.   
  114. End Sub  
  115. End Class  

Screenshot

ASP.NET(vb.net) & Word (Word Application) - Word Document Sample Report

.
.
.
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.