会员
周边
众包
新闻
博问
闪存
赞助商
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
. NET 技术讨论
学于明志,交流增加见识,讨论改变思维
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
公告
将Excel中所有Sheet的数据导入一个DataSet
Posted on
2005-12-30 18:54
君
阅读(
572
) 评论(
1
)
编辑
收藏
举报
# region 将Excel中所有Sheet的数据导入一个DataSet
/**/
///
<summary>
///
将Excel中所有Sheet的数据导入一个DataSet
///
</summary>
///
<returns></returns>
public
static
DataSet CreateDataSource(
string
strPath)
{
try
{
bool
m_DelRow
=
false
;
string
strConn
=
"
Provider = Microsoft.Jet.OLEDB.4.0;Data Source =
"
+
strPath
+
"
; Extended Properties = 'Excel 8.0;HDR = Yes;IMEX = 1'
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
conn.Open();
System.Data.DataTable dtSheetName
=
conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new
object
[]
{
null
,
null
,
null
,
"
Table
"
}
);
//
=======删除文件中的打印区域============================================================
for
(
int
k
=
0
;k
<
dtSheetName.Rows.Count;k
++
)
{
if
(dtSheetName.Rows[k][
"
TABLE_NAME
"
].ToString().EndsWith(
"
Print_Area
"
))
{
dtSheetName.Rows[k].Delete();
}
}
dtSheetName.AcceptChanges();
//
========================================================================
string
strSql
=
string
.Empty;
DataSet myDataSet
=
new
DataSet();
//
===========================================================================
for
(
int
i
=
0
;i
<
dtSheetName.Rows.Count;i
++
)
{
//
用多个表来填充myDataSet
strSql
=
"
Select * from [
"
+
dtSheetName.Rows[i][
"
TABLE_NAME
"
].ToString()
+
"
]
"
;
OleDbDataAdapter myCommand
=
new
OleDbDataAdapter(strSql,conn);
myCommand.Fill(myDataSet,dtSheetName.Rows[i][
"
TABLE_NAME
"
].ToString());
}
//
==========删除空表=========================================
//
for(int h =0;h < myDataSet.Tables.Count;h++)
//
{
//
if(myDataSet.Tables[h].Rows.Count == 0)
//
{
//
myDataSet.Tables[h].
//
}
//
}
//
删除文件中的全为空的行
for
(
int
j
=
0
;j
<
myDataSet.Tables.Count;j
++
)
{
for
(
int
i
=
0
;i
<
myDataSet.Tables[j].Rows.Count;i
++
)
{
for
(
int
k
=
0
; k
<
myDataSet.Tables[j].Columns.Count;k
++
)
{
if
(myDataSet.Tables[j].Rows[i][k].ToString()
!=
string
.Empty)
{
m_DelRow
=
true
;
}
}
if
(
!
m_DelRow)
{
myDataSet.Tables[j].Rows[i].Delete();
}
m_DelRow
=
false
;
}
}
myDataSet.AcceptChanges();
//
================================================================================
m_DelRow
=
false
;
return
myDataSet;
}
catch
(Exception err)
{
Console.WriteLine(err.Message
+
System.Environment.NewLine
+
err.StackTrace);
return
null
;
}
}
#endregion
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 君
Powered by .NET 8.0 on Kubernetes