将Excel文件读入DataTable包括系统定义的列

时间:2016-04-28 10:05:57

标签: c# excel

我正在编写一个程序来使用c#读取excel并存储在DataTable中。在调试时检查DataTable中的列时,我会看到系统列,如Table_SchemaTable_CatalogTable_NameTable_Type等。如何排除这些列不受欢迎的列?

我的代码:

string sSheetName = null;
            string sConnection = null;
            int nOutputRow = 0;
            DataTable dtTablesList = default(DataTable);
            OleDbCommand oleExcelCommand = default(OleDbCommand);
            OleDbConnection oleExcelConnection = default(OleDbConnection);

            sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtFileName.Text + ";" + "Extended Properties=Excel 8.0;";
            oleExcelConnection = new OleDbConnection(sConnection);
            oleExcelConnection.Open();

            dtTablesList = oleExcelConnection.GetSchema("Tables");

            if (dtTablesList.Rows.Count > 0)
            {
                sSheetName = dtTablesList.Rows[0].Field<string>("TABLE_NAME");
            }

            dtTablesList.Clear();
            dtTablesList.Dispose();


            if (!string.IsNullOrEmpty(sSheetName))
            {
                oleExcelCommand = oleExcelConnection.CreateCommand();
                string commandText = "Select * From [" + sSheetName + "]";

                oleExcelCommand.CommandType = CommandType.Text;
                OleDbDataAdapter daexcel = new OleDbDataAdapter(commandText, oleExcelConnection);
                dtTablesList.Locale = System.Globalization.CultureInfo.CurrentCulture;
                daexcel.Fill(dtTablesList);
            }
            oleExcelConnection.Close();

0 个答案:

没有答案