我正在编写一个程序来使用c#读取excel并存储在DataTable
中。在调试时检查DataTable
中的列时,我会看到系统列,如Table_Schema
,Table_Catalog
,Table_Name
,Table_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();