C#读取Excel到DataTable

2023-05-16

1、将Excel中的表格数据读取到DataTable中:

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "打开Excel文件";
openFileDialog1.Filter = "Files|*.xls;*.xlsx";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
   {
       textBox1.Text = openFileDialog1.FileName;
       #region 读取excel到dt
       Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
       xApp.Visible = false;
       Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks.Open(openFileDialog1.FileName, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
       Microsoft.Office.Interop.Excel.Worksheet xSheet = xBook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
       string tableName = xSheet.Name;
       DataTable dt1 = DBhelper.getDataSetFromExcelTable(tableName, openFileDialog1.FileName);
       xBook.Close(true);
       Marshal.ReleaseComObject(xBook);
       Marshal.ReleaseComObject(xSheet);
       xBook = null;
       xSheet = null;
       System.Runtime.InteropServices.Marshal.ReleaseComObject(xApp);
       System.Diagnostics.Process[] excelprocess2 = System.Diagnostics.Process.GetProcessesByName("WINEXCEL");
       foreach (System.Diagnostics.Process pr in excelprocess2)
       {
            pr.Kill();//停止关联进程
       }
       #endregion
  }

2、读取Excel中指定表中的表格数据到DataTable中的公共类

        /// <summary>
        /// 读取Excel中指定表中的数据到DataTable中
        /// </summary>
        /// <param name="tableName">Excel中的表名</param>
        /// <param name="dataFilePath">Excel文件路径</param>
        /// <returns></returns>
        static public DataTable getDataSetFromExcelTable(string tableName,string dataFilePath)
        {
            string oleDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dataFilePath+ ";Extended Properties='Excel 12.0;IMEX=1';";
            OleDbConnection connct = new OleDbConnection(oleDB);
            try
            {
                connct.Open();
                DataSet dataSet = new DataSet();
                string sql = "select * from " + "[" + tableName + "$]";
                OleDbDataAdapter datpTextdapter = new OleDbDataAdapter(sql, oleDB);

                try
                {
                    datpTextdapter.Fill(dataSet);
                    connct.Close();
                    return dataSet.Tables[0];
                }
                finally
                {
                    dataSet.Dispose();
                    datpTextdapter.Dispose();
                }
            }
            finally
            {
                connct.Dispose();
                //command.Dispose();
            }
        }

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C#读取Excel到DataTable 的相关文章

随机推荐