C#通过OleDbConnection的GetOleDbSchemaTable方法从Excel取得的表,自动排列顺序咋办
这是部分源代码:stringstrConn=@"Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+strFileName+";E...
这是部分源代码:
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
OleDbConnection ExcelConn = new OleDbConnection(strConn);
ExcelConn.Open();
DataTable dtSheetName = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
sheetsNum = dtSheetName.Rows.Count;
string[] strTableNames = new string[sheetsNum];
DataSet ExcelDS = new DataSet();
DataTable ExcelDT = new DataTable();
。。。。。。。。
为什么strTableNames里面所取得的表的名称与Excel里实际表的名称顺序不一样呢。自动排序导致我在程序中用tab页的形式显示的各张表与Excel中实际的表顺序不一样,请问咋处理呢? 展开
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
OleDbConnection ExcelConn = new OleDbConnection(strConn);
ExcelConn.Open();
DataTable dtSheetName = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
sheetsNum = dtSheetName.Rows.Count;
string[] strTableNames = new string[sheetsNum];
DataSet ExcelDS = new DataSet();
DataTable ExcelDT = new DataTable();
。。。。。。。。
为什么strTableNames里面所取得的表的名称与Excel里实际表的名称顺序不一样呢。自动排序导致我在程序中用tab页的形式显示的各张表与Excel中实际的表顺序不一样,请问咋处理呢? 展开
展开全部
/// <summary>
/// 生成DataSet
/// </summary>
/// <param name="strConn">链接字符串</param>
/// <param name="tableName">表名</param>
/// <returns></returns>
private DataSet CreateDateSet()
{
string sqlConn = "SELECT * FROM [" + this.TableName + "$]";
string strConn = this.StrConnection + this.FilePath;
OleDbConnection conn = new OleDbConnection(strConn);
// string sqlConn = "SELECT * FROM [sheet1$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(sqlConn, strConn);
DataSet myDataSet = new DataSet();
try
{
myCommand.Fill(myDataSet);
int row = myDataSet.Tables[0].Rows.Count;
int col = myDataSet.Tables[0].Columns.Count;
this.rowCount = (row > 0) ? row : 0;
this.columnCount = (col > 0) ? col : 0;
conn.Close();
}
catch (Exception ex)
{
if (conn != null)
{
conn.Close();
}
throw new Exception("该Excel文件的工作表的名字不正确," + ex.Message);
}
return myDataSet;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询