asp excel导出类

来源:互联网 发布:手机淘宝的设置在哪 编辑:程序博客网 时间:2024/05/02 02:56
  1. 导出Excel报表的类asp学习网 来自:leadbbs 作者:一千零一个愿望 
  2. 类文件Excel.asp 
  3. <
  4. ''/**************************************/ 
  5. ''/* written by yzcangel */ 
  6. ''/* version : v1.0 */ 
  7. ''/* createdata:2005-09-01 */ 
  8. ''/* lastmodifydate:2005-09-01 */ 
  9. ''/* Eamil:yzcangel@sohu.com */ 
  10. ''/* QQ:80214600 */ 
  11. ''/**************************************/ 
  12. ''类开始 
  13. Class Cls_Excel 
  14. ''声明常量、变量 
  15. Private objRs 
  16. Private objExcelApp 
  17. Private objExcelBook 
  18. Private Conn 
  19. Private Sql 
  20. Private Title 
  21. Private FieldName 
  22. Private FieldValue 
  23. Private FilePath 
  24. Private FileName 
  25. Private Col 
  26. Private Row 
  27. ''Class_Initialize 类的初始化 
  28. Private Sub Class_Initialize() 
  29. Row = 1 ''设定生成的Excel默认起始行 
  30. Col = 1 ''设定生成的Excel默认起始列 
  31. End Sub 
  32. ''ReportConn得到数据库连接对象 
  33. Public Property Let ReportConn(ByVal objConn) 
  34. Set Conn = objConn 
  35. End Property 
  36. ''ReportSql得到SQL字符串 
  37. Public Property Let ReportSql(ByVal strSql) 
  38. Sql = strSql 
  39. End Property 
  40. ''ReportTitle得到所要生成报表的标题 
  41. Public Property Let ReportTitle(ByVal strTitle) 
  42. Title = strTitle 
  43. End Property 
  44. ''RsFieldName得到所要生成报表的列名称 
  45. Public Property Let RsFieldName(ByVal strName) 
  46. FieldName = Split(strName,"||") 
  47. End Property 
  48. ''RsFieldValue得到所要生成报表的列值的数据库标识字段 
  49. Public Property Let RsFieldValue(ByVal strValue) 
  50. FieldValue = Split(strValue,"||") 
  51. End Property 
  52. ''SaveFilePath得到Excel报表的保存路径 
  53. Public Property Let SaveFilePath(ByVal strFilePath) 
  54. FilePath = strFilePath 
  55. End Property 
  56. ''SaveFileName得到Excel报表的保存文件名 
  57. Public Property Let SaveFileName(ByVal strFileName) 
  58. FileName = strFileName 
  59. End Property 
  60. ''ColumnOffset得到Excel报表默认起始列 
  61. Public Property Let ColumnOffset(ByVal ColOff) 
  62. If ColOff > 0 then 
  63. Col = ColOff 
  64. Else 
  65. Col = 1 
  66. End If 
  67. End Property 
  68. ''RowOffset得到Excel报表默认起始行 
  69. Public Property Let RowOffset(ByVal RowOff) 
  70. If RowOff > 0 then 
  71. Row = RowOff 
  72. Else 
  73. Row = 1 
  74. End If 
  75. End Property 
  76. ''生成报表 
  77. Sub Worksheet() 
  78. Dim iCol,iRow,Num 
  79. iCol = Col 
  80. iRow = Row 
  81. Num = 1 
  82. Call DBRs() 
  83. Call ExcelApp() 
  84. Set objExcelBook = objExcelApp.Workbooks.Add 
  85. ''写Excel标题 
  86. ''-------------------------------------------------------- 
  87. objExcelBook.WorkSheets(1).Cells(iRow,iCol).Value = Title 
  88. ''-------------------------------------------------------- 
  89. ''写Excel各列名 
  90. ''-------------------------------------------------------- 
  91. iRow = Row + 1 
  92. objExcelBook.WorkSheets(1).Cells(iRow,iCol).Value = "序号" 
  93. iColiCol = iCol + 1 
  94. For i = 0 to Ubound(FieldName) 
  95. objExcelBook.WorkSheets(1).Cells(iRow,iCol).Value = FieldName(i) 
  96. iColiCol = iCol + 1 
  97. Next 
  98. ''-------------------------------------------------------- 
  99. ''写Excel各列值 
  100. ''-------------------------------------------------------- 
  101. iRow = Row + 2 
  102. Do While Not objRS.EOF 
  103. iCol = Col 
  104. objExcelBook.WorkSheets(1).Cells(iRow,iCol).Value = Num 
  105. iColiCol = iCol + 1 
  106. For i = 0 to Ubound(FieldValue) 
  107. If IsNull(objRS(FieldValue(i))) then 
  108. objExcelBook.WorkSheets(1).Cells(iRow,iCol).Value = "" 
  109. Else 
  110. objExcelBook.WorkSheets(1).Cells(iRow,iCol).Value = objRS(FieldValue(i)) 
  111. End If 
  112. iColiCol = iCol + 1 
  113. Next 
  114. objRS.MoveNext 
  115. iRowiRow = iRow + 1 
  116. NumNum = Num + 1 
  117. Loop 
  118. ''-------------------------------------------------------- 
  119. Call SaveWorksheet() 
  120. End Sub 
  121. ''创建Adodb.Recordset对象 
  122. Sub DBRs() 
  123. If IsObject(objRs) = True Then Exit Sub 
  124. Set objRs = Server.CreateObject("Adodb.Recordset") 
  125. objRs.Open Sql,Conn,1,1 
  126. If Err.Number > 0 Then 
  127. Response.End 
  128. End If 
  129. End Sub 
  130. ''创建Excel.Application对象 
  131. Sub ExcelApp() 
  132. If IsObject(objExcelApp) = True Then Exit Sub 
  133. Set objExcelApp = Server.CreateObject("Excel.Application") 
  134. objExcelApp.Application.Visible = True 
  135. If Err.Number > 0 Then 
  136. Response.End 
  137. End If 
  138. End Sub 
  139. ''保存Excel报表 
  140. Sub SaveWorksheet() 
  141. objExcelbook.SaveAs FilePath & FileName & ".xls" 
  142. If Err.Number = 0 Then 
  143. Call Message("导出数据成功!") 
  144. Else 
  145. Call Message("导出数据失败!") 
  146. End If 
  147. End Sub 
  148. ''信息提示 
  149. Sub Message(msg) 
  150. Response.Write("<script language=''JavaScript''>") 
  151. Response.Write("alert(''"&msg&"'');") 
  152. Response.Write("</script>") 
  153. Response.End 
  154. End Sub 
  155. ''Class_Terminate 类注销 
  156. Private Sub Class_Terminate() 
  157. objExcelApp.Application.Quit 
  158. Set objExcelBook = Nothing 
  159. Set objExcelApp = Nothing 
  160. objRs.Close 
  161. Set objRs = Nothing 
  162. End Sub 
  163. ''类结束 
  164. End Class 
  165. %> 
  166. 示例文件test.asp 
  167. <!--#include file="Lib/conn.asp"--> 
  168. <!--#include file="Excel.asp"--> 
  169. <
  170. Dim MyExcel 
  171. Set MyExcel = new Cls_Excel 
  172. With MyExcel 
  173. .ReportConn = conn 
  174. .ReportSql = "SELECT b.pm AS A_PM, b.dqccl AS A_Num, b.dqccl * 100 / SUM(a.dqccl) AS A_Percent FROM dbo.V_DQTJWHP a CROSS JOIN dbo.V_DQTJWHP b GROUP BY b.pm, b.dqccl order by A_Num desc" 
  175. .ReportTitle = "MyExcel 报表" 
  176. .RsFieldName = "品名||数量||百分比" 
  177. .RsFieldValue = "A_PM||A_Num||A_Percent" 
  178. .SaveFilePath = "d:/" 
  179. .SaveFileName = "Excel" 
  180. .ColumnOffset = 1 
  181. .RowOffset = 1 
  182. End With 
  183. MyExcel.Worksheet() 
  184. MyExcel = Null 
  185. Set MyExcel = Nothing 
  186. %>
原创粉丝点击