国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.NET中將數據直接輸出成Excel格式

2024-07-10 13:12:55
字體:
來源:轉載
供稿:網友

本文實現了將數據庫中的數據直接輸出到excel文件格式并在瀏覽器里輸出。下面就是實現的例子:
查看例子

<以下為引用的內容:

excelexport.aspx
  
  <%@ page language="vb" autoeventwireup="false" codebehind="excelexport.aspx.vb"
  inherits="aspxweb.mengxianhui.com.excelexport"%>
  <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
  <html>
   <head>
   ?。紅itle>excelexport</title>
   ?。糾eta name="generator" content="microsoft visual studio.net 7.0">
    <meta name="code_language" content="visual basic 7.0">
   ?。糾eta name="vs_defaultclientscript" content="javascript">
   ?。糾eta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
  ?。?head>
  <body ms_positioning="gridlayout">
   <form id="form1" method="post" runat="server">
   ?。糰sp:datagrid id="datagrid1" runat="server" cellpadding="4" backcolor="white"
  bordercolor="#cc9966" borderwidth="1px" borderstyle="none" width="100%" height="100%"
  font-size="9pt" font-names="宋體">
   ?。約electeditemstyle font-bold="true" forecolor="#663399" backcolor="#ffcc66"></selecteditemstyle>
   ?。糰lternatingitemstyle backcolor="#ffcc99"></alternatingitemstyle>
   ?。糹temstyle borderwidth="2px" forecolor="#330099" borderstyle="solid"
  bordercolor="black" backcolor="white"></itemstyle>
    <headerstyle font-bold="true" horizontalalign="center" borderwidth="2px"
  forecolor="#ffffcc" borderstyle="solid" bordercolor="black" backcolor="#990000"></headerstyle>
   ?。?asp:datagrid>
  ?。?form>
  </body>
  </html>
  
  
    excelexport.aspx.vb
  
  public class excelexport
  inherits system.web.ui.page
  protected withevents datagrid1 as system.web.ui.webcontrols.datagrid
  #region " web 窗體設計器生成的代碼 "
  '該調用是 web 窗體設計器所必需的。
  <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
  end sub
  
  private sub page_init(byval sender as system.object, byval e as system.eventargs) _
  handles mybase.init
   'codegen: 此方法調用是 web 窗體設計器所必需的
   '不要使用代碼編輯器修改它。
   initializecomponent()
  end sub
  
  #end region
  private sub page_load(byval sender as system.object, byval e as system.eventargs) _
  handles mybase.load
   '在此處放置初始化頁的用戶代碼
   ' 定義是否是 sql server 數據庫,這里為false
   dim blnissqlserver as system.boolean = false
   dim strsql as string
   dim objdataset as new dataset()
   dim objconn as object
   dim strcnn as string
   if blnissqlserver then
    strcnn = "user id=sa;initial catalog=northwind;data source=./netsdk;"
    objconn = new system.data.sqlclient.sqlconnection(strcnn)
    objconn.open()
    dim objadapter as new system.data.sqlclient.sqldataadapter()
    strsql = "select * from customers where country='usa'"
    objadapter.selectcommand = new system.data.sqlclient.sqlcommand(strsql, objconn)
    objadapter.fill(objdataset)
   else
    strcnn = "provider=microsoft.jet.oledb.4.0;data source=" + server.mappath("test.mdb")
    objconn = new system.data.oledb.oledbconnection(strcnn)
    objconn.open()
    dim objadapter as new system.data.oledb.oledbdataadapter()
    strsql = "select top 10 title from document"
    objadapter.selectcommand = new system.data.oledb.oledbcommand(strsql, objconn)
    objadapter.fill(objdataset)
   end if
   dim oview as new dataview(objdataset.tables(0))
   datagrid1.datasource = oview
   datagrid1.databind()
   objconn.close()
   objconn.dispose()
   objconn = nothing
   if request.querystring("bexcel") = "1" then
    response.contenttype = "application/vnd.ms-excel"
    ' 從content-type header中去除charset設置
    response.charset = ""
    ' 關閉 viewstate
    me.enableviewstate = false
    dim tw as new system.io.stringwriter()
    dim hw as new system.web.ui.htmltextwriter(tw)
    ' 獲取control的html
    datagrid1.rendercontrol(hw)
    ' 把html寫回瀏覽器
    response.write(tw.tostring())
    response.end()
   end if
  end sub
  end class

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 衡水市| 武城县| 广东省| 南阳市| 城步| 叶城县| 天峻县| 确山县| 湘潭县| 乐安县| 云浮市| 丰都县| 湘阴县| 石棉县| 龙州县| 漳州市| 镇安县| 松溪县| 塘沽区| 永年县| 镇康县| 西城区| 商丘市| 上林县| 洪江市| 富宁县| 高唐县| 东平县| 宝清县| 怀柔区| 旬阳县| 岫岩| 赤水市| 海盐县| 牙克石市| 读书| 无棣县| 驻马店市| 双城市| 团风县| 中方县|