WinForm Read Excel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using Excel = Microsoft.Office.Interop.Excel;
using System.Data.OleDb;


namespace ReadExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnShowDialog_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (SqlConnection sqlCon = new SqlConnection(""))
                {
                    sqlCon.Open();
                    string fileName = openFileDialog1.FileName;

                    DataTable dt = ExcelToDataSet(fileName);
                    int numSucc = 0;
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            string strID = dt.Rows[i][0].ToString().Trim();
                            string strName = dt.Rows[i][1].ToString().Trim();
                            string strNum = dt.Rows[i][2].ToString().Trim();
                            string strUnit = dt.Rows[i][3].ToString().Trim();
                            string Price = dt.Rows[i][4].ToString().Trim();

                            string str = "";
                            SqlCommand sqlCmd = new SqlCommand(str, sqlCon);
                            numSucc += sqlCmd.ExecuteNonQuery();
                        }
                    }
                    MessageBox.Show("成功導入" + numSucc + "");
                }
            }
        }

        public static DataTable ExcelToDataSet(string filename)
        {
            try
            {
                //string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filename + ";Extended Properties=Excel 8.0";
                string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";   // Office 07及以上版本 不能出现多余的空格 而且分号注意
                OleDbConnection conn = new OleDbConnection(strCon);
                conn.Open();
                //返回Excel的架构,包括各个sheet表的名称,类型,创建时间和修改时间等 
                DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
                //包含excel中表名的字符串数组
                string[] strTableNames = new string[dtSheetName.Rows.Count];
                for (int k = 0; k < dtSheetName.Rows.Count; k++)
                {
                    strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
                }
                OleDbDataAdapter myCommand = null;
                DataTable dt = new DataTable();
                //从指定的表明查询数据,可先把所有表明列出来供用户选择
                string strExcel = "select * from [" + strTableNames[0] + "]";
                myCommand = new OleDbDataAdapter(strExcel, strCon);
                myCommand.Fill(dt);

                return dt;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/siri/archive/2013/01/15/2861069.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值