EXCEL:读取ACCESS数据库中指定表数据到EXCEL

本文介绍了一种使用C#从Access数据库读取数据并将其导入Excel的方法。通过ADO.NET技术,实现了连接Access数据库、执行SQL语句并将指定表格的数据导出到Excel文件的功能。此外,还提供了浏览和打开已生成Excel文件的选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Word = Microsoft.Office.Interop.Word;
using System.Threading;
using office = Microsoft.Office.Core;
using System.Reflection;
using System.Data.OleDb;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtAccess.Text = Server.MapPath("~/File/access.mdb");//初始化access文件路径
            txtExcel.Text = Server.MapPath("~/File/excel.xls");//初始化Excel文件路径
            BindDropDownList();
        }
    }

    protected void btnRead_Click(object sender, EventArgs e)
    {
        try
        {
            string P_str_Con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtAccess.Text + ";Persist Security Info=True";//记录连接Access的语句
            string P_str_Sql = "";//存储要执行的SQL语句
            OleDbConnection oledbcon = new OleDbConnection(P_str_Con);//实例化OLEDB连接对象
            OleDbCommand oledbcom;//定义OleDbCommand对象
            oledbcon.Open();//打开数据库连接
            //向Excel工作表中导入数据
            P_str_Sql = @"select * into [Excel 8.0;database=" + txtExcel.Text + "]." + "[" + DropDownList1.SelectedValue + "] from " + DropDownList1.SelectedValue + "";//记录连接Excel的语句
            oledbcom = new System.Data.OleDb.OleDbCommand(P_str_Sql, oledbcon);//实例化OleDbCommand对象
            oledbcom.ExecuteNonQuery();//执行SQL语句,将数据表的内容导入到Excel中
            oledbcon.Close();//关闭数据库连接
            oledbcon.Dispose();//释放资源
            Response.Write("<script>alert('导入成功!');</script>");
        }
        catch
        {
            Response.Write("<script>alert('工作表已经存在,请选择其他数据表!');/script>");
        }
    }
    protected void btnBrowse_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Process.Start(txtExcel.Text);//打开选择的Excel文件
    }

    private void BindDropDownList()
    {
        string P_str_Con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtAccess.Text + ";Persist Security Info=True";//记录连接Access的语句
        OleDbConnection oledbcon = new OleDbConnection(P_str_Con);//实例化OLEDB连接对象
        oledbcon.Open();//打开数据库连接
        DataTable DTable = oledbcon.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });//获取所有数据表信息
        oledbcon.Close();//关闭数据库连接
        DropDownList1.Items.Clear();//清空下拉列表
        for (int i = 0; i < DTable.Rows.Count; i++)//遍历数据表信息
        {
            DropDownList1.Items.Add(DTable.Rows[i][2].ToString());//将数据表名称添加到下拉列表中
        }
        if (DropDownList1.Items.Count > 0)//判断下拉列表中是否有项
            DropDownList1.SelectedIndex = 0;//设置下拉列表默认选择第一项
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值