Access导出到Excel

public partial class Form1 : Form
    {
        OleDbConnection myConnection;
        OleDbCommand myCommand;
        OleDbDataReader myDataReader;
        DataTable table;
        string fileName = "";
        public Form1()
        {
            InitializeComponent();
            label2.Text = "";
            if (this.checkBox1.Checked == false)
            {
                setCombobox();
            }
            else
            {
                this.cboCondition.Enabled = false;
            }
        }

        public void setCombobox()
        {
            OleDbConnection conn;
            string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
            strConnection += @"Data Source=" + Application.StartupPath + @"/DB/moban.mdb";
            conn = new OleDbConnection(strConnection);
            try
            {
                conn.Open();
                this.cboCondition.Items.Clear();
                OleDbDataAdapter adp1 = new OleDbDataAdapter("select distinct name2 from xq", conn);
                DataSet ds1 = new DataSet();
                //adp1.Fill(ds1, "Products");
                adp1.Fill(ds1);
                this.cboCondition.DisplayMember = "name2";//(这是显示的)
                this.cboCondition.ValueMember = "name2";//这是不显示的项就是可以看成数据表中的主键一样进行判断好用的(这是实际的)
                this.cboCondition.DataSource = ds1.Tables[0];
            }
            catch (Exception e)
            {
            }
            finally
            {
                conn.Close();
            }
        }
        public void toExcel()
        {
            string sql;
            string connstr = "Provider=Microsoft.Jet.OleDb.4.0;";
            connstr += @"Data Source="+ Application.StartupPath + @"/DB/moban.mdb";
            OleDbConnection cn = new OleDbConnection(connstr);
            OleDbCommand cmd;
            if (File.Exists(fileName + ".xls"))
            {
                File.Delete(fileName + ".xls");
            }
            try
            {
                cn.Open();
                // TODO:计算Sheet数目,进行记录分段,将不同的数据段导入到不同的Sheet(Sheet数目不知道有没有限制:()
                // TODO:文件名,Sheet名字的存在检测略
                //每个Sheet只能最多保存65536条记录。
                sql = @"select top 65535 * into [Excel 8.0;database=" + fileName + ".xls].[Sheet1] from xq";
                if (this.cboCondition.Enabled == true && this.cboCondition.ValueMember.Trim() != string.Empty)
                {
                    sql += " where name2 like '" + this.cboCondition.Text + "'";
                }
                cmd = new OleDbCommand(sql, cn);
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                this.label2.Text = "导出不成功!";
                return;
            }
            finally
            {
                if(cn != null)
                {
                    cn.Close();
                    cn.Dispose();
                    cn = null;
                }
            }
            this.label2.Text = "导出成功!";
        }

        private void btnToExcel_Click(object sender, EventArgs e)
        {
            toExcel();
        }

        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = this.saveFileDialog1.FileName;

            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked == true)
            {
                this.cboCondition.Enabled = false;
            }
            else
            {
                this.cboCondition.Enabled = true;
            }
        }
        #endregion
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值