Default.aspx

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AccessToExcel.aspx.cs" Inherits="AccessToExcel" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center" border="1" bordercolor="honeydew" cellpadding="0" 
        cellspacing="0">
        <tr>
            <td style="FONT-SIZE: 9pt; COLOR: #ff0000; BACKGROUND-COLOR: #ff9933; TEXT-ALIGN: center" 
                class="style1">
            </td>
            <td 
                
                style="FONT-SIZE: 9pt; COLOR: #ffffff; HEIGHT: 16px; BACKGROUND-COLOR: #ff9933; TEXT-ALIGN: center">
                将Access数据库数据写入Excel中</td>
        </tr>
        <tr>
            <td style="BACKGROUND-COLOR: #ffffcc; TEXT-ALIGN: center">
                <asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Size="9pt" 
                    ForeColor="#333333" GridLines="None" Width="228px">
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
            </td>
            <td style="BACKGROUND-COLOR: #ffffcc; TEXT-ALIGN: center">
                <iframe id="I1" name="I1" scrolling="yes" src="mrMembers.xls" 
                    style="WIDTH: 407px; HEIGHT: 280px"></iframe>
            </td>
        </tr>
        <tr>
            <td style="BACKGROUND-COLOR: #ff9900; TEXT-ALIGN: center" class="style3">
    <asp:Button ID="btnAccessToExcel" runat="server" onclick="btnAccessToExcel_Click" 
        Text="将Access数据库中数据导入到Excel文件中" Width="249px" />
            </td>
            <td style="HEIGHT: 23px; BACKGROUND-COLOR: #ff9900; TEXT-ALIGN: center" 
                valign="top">
                显示导入到Excel中的ACCESS数据库数据</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

View Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;

public partial class AccessToExcel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }
    public OleDbConnection CreateCon()
    {
        string strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath("fdw.mdb") + ";User Id=admin;Password=;";
        OleDbConnection odbc = new OleDbConnection(strconn);
        return odbc;
    }
    protected void btnAccessToExcel_Click(object sender, EventArgs e)
    {
        string sql;
        OleDbConnection cn = CreateCon();
        OleDbCommand cmd;
        cn.Open();
        //先得到记录数目:
        sql = "select Count(*) From UserInfo";
        cmd = new OleDbCommand(sql, cn);
        int RecordCount = (int)cmd.ExecuteScalar();
        try
        {
            //每个Sheet只能最多保存65536条记录,这里在导入到Excel中时需要重新创建一个Sheet表。
            sql = @"select top 65535 * into [Excel 8.0;database=" + Server.MapPath(".") + @"\Members.xls].[Sheet2] from UserInfo";
            cmd = new System.Data.OleDb.OleDbCommand(sql, cn);
            cmd.ExecuteNonQuery();
            cn.Close();
            cn.Dispose();
            cn = null;
            Response.Write("<script>alert('已经成功导入到表Sheet1中!请勿重复操作!')</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        } 
    }
    public void LoadData()
    {
        string sql = "select * From UserInfo";
        OleDbConnection cn = CreateCon();
        cn.Open();
        OleDbDataAdapter oledb = new OleDbDataAdapter(sql, cn);
        DataSet ds = new DataSet();
        oledb.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        oledb.Dispose();
        cn.Close();
    }
}

 

posted on 2013-03-19 19:25  松竹梅  阅读(588)  评论(0编辑  收藏  举报