请问ASP.NET(VB)将从SQL数据库中查询出来的数据导出为Excel文档?

解决方案 »

  1.   

    不如做一个报表。
    通过报表进行excel,word转换。
    或者通过一个类似datagrid的第三方控件。
    axpGrid它有一个图片按钮,可以把datatabel或OleDBreader里面的数据转换成excel表格。
    此控件的下载自己去找。
    找不到发邮件到[email protected]中。
      

  2.   

    那请问导出txt文件怎么导出。最好能对齐,对不齐的话用“Tab”空开,请问程序怎么编写?
      

  3.   

    http://www.aspcool.com/lanmu/browse1.asp?ID=1208&bbsuser=aspnet
      

  4.   

    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>
        <title>ExcelExport</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
        <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
          <asp:datagrid id="DataGrid1" runat="server" CellPadding="4" BackColor="White"
           BorderColor="#CC9966" BorderWidth="1px" BorderStyle="None" Width="100%" Height="100%"
            Font-Size="9pt" Font-Names="宋体">
            <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
            <AlternatingItemStyle BackColor="#FFCC99"></AlternatingItemStyle>
            <ItemStyle 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.vbPublic 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
      

  5.   

    to  renyu732 :
    请问将转换后的用html回写的excel怎么将他保存为.xls文档?谢谢·
      

  6.   

    Public Function SaveExcle(ByVal Thispage As Page, ByVal ObjectControl As System.Web.UI.Control) As Boolean        'public void Save(System.Web.UI.Control source, DocumentType type)
            Thispage.Response.Clear()
            Thispage.Response.Buffer = True        '设置Http的头信息,编码格式        Thispage.Response.AppendHeader("Content-Disposition", "attachment;filename=FHJH.xls")
            Thispage.Response.ContentType = "application/ms-excel"        Thispage.Response.Charset = "GB2312"
            Thispage.Response.ContentEncoding = System.Text.Encoding.UTF8        '关闭控件的视图状态
            ObjectControl.EnableViewState = False        '初始化HtmlWriter
            Dim writer As New System.IO.StringWriter
            Dim htmlWriter As New System.Web.UI.Html32TextWriter(writer)
            'System.IO.StringWriter writer = new System.IO.StringWriter() ;
            'System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
            ObjectControl.RenderControl(htmlWriter)        '输出
            Thispage.Response.Write(writer.ToString())        Thispage.Response.End()
        End Function-----------------------------------------------------------------
    我的转换函数,你直接把数据写到一个控件里,然后调用函数就可以了
      

  7.   

    就算你装到DATAGRID后,再使用TABLE调整其他属性,只要最后把TABLE传进来就可以了!