WinForm Read Excel

9053 단어 WinForm
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;

            }

        }

    }

}

좋은 웹페이지 즐겨찾기