zoukankan
html css js c++ java
EXCEL常用的类
using
System;
using
System.Data;
using
System.Data.OleDb;
namespace
my
{
/**/
///
<summary>
///
Excel 的摘要说明。
///
</summary>
public
class
Excel
{
public
Excel()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
public
static
DataSet SelectExcel_AllSheet1(
string
path,
string
condition)
{
string
strConn;
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+
path
+
"
;
"
+
"
Extended Properties=Excel 8.0;
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
OleDbDataAdapter myCommand
=
new
OleDbDataAdapter(
"
select * FROM [Sheet1$]
"
+
condition, strConn);
DataSet myDataSet
=
new
DataSet();
myCommand.Fill(myDataSet);
return
myDataSet;
}
public
static
DataSet SelectExcel(
string
path,
string
sqltext)
{
string
strConn;
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+
path
+
"
;
"
+
"
Extended Properties=Excel 8.0;
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
OleDbDataAdapter myCommand
=
new
OleDbDataAdapter(sqltext, strConn);
DataSet myDataSet
=
new
DataSet();
myCommand.Fill(myDataSet);
return
myDataSet;
}
//
获取EXCEL文件的表名
public
static
string
[] ObtainTableName(
string
path)
{
string
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+
path
+
"
;Extended Properties=Excel 8.0;
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
conn.Open();
DataTable dt
=
conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
null
);
int
i
=
dt.Rows.Count;
string
[] strTableName
=
new
string
[i];
for
(
int
s
=
0
;s
<
i;s
++
)
{
strTableName[s]
=
dt.Rows[s][
"
TABLE_NAME
"
].ToString();
}
return
strTableName;
}
public
static
bool
DataSetToExcelText(DataSet ds)
{
bool
result
=
false
;
if
(ds.Tables[
0
]
!=
null
)
{
string
filename
=
OpenSaveDialog();
if
((filename
!=
null
)
&&
(filename
!=
""
))
{
StreamWriter sw
=
new
StreamWriter(filename,
false
,Encoding.Unicode);
try
{
//
StreamWriter sw=new StreamWriter(filename,false,Encoding.Unicode);
int
i
=
0
;
int
j
=
0
;
j
=
ds.Tables[
0
].Columns.Count;
//
文件列头
string
s
=
""
;
while
(i
<
j)
{
s
=
s
+
ds.Tables[
0
].Columns[i].ColumnName
+
"
\t
"
;
i
=
i
+
1
;
}
sw.WriteLine(s);
//
写数据
foreach
(DataRow r
in
ds.Tables[
0
].Rows )
{
i
=
0
;
s
=
""
;
while
(i
<
j)
{
if
(r[i].ToString().Trim()
==
""
)
{
s
=
s
+
"
"
+
"
\t
"
;
}
else
{
s
=
s
+
r[i].ToString().Trim()
+
"
\t
"
;
}
i
=
i
+
1
;
}
sw.WriteLine(s);
}
result
=
true
;
MessageBox.Show(
"
导出成功!
"
);
}
catch
{
result
=
false
;
MessageBox.Show(
"
导出失败!
"
);
}
finally
{
sw.Close();
}
}
}
return
result;
}
public
static
bool
DataSetToExcelText(DataTable dt)
{
bool
result
=
false
;
if
(dt
!=
null
)
{
string
filename
=
OpenSaveDialog();
if
((filename
!=
null
)
&&
(filename
!=
""
))
{
StreamWriter sw
=
new
StreamWriter(filename,
false
,Encoding.Unicode);
try
{
//
StreamWriter sw=new StreamWriter(filename,false,Encoding.Unicode);
int
i
=
0
;
int
j
=
0
;
j
=
dt.Columns.Count;
//
文件列头
string
s
=
""
;
while
(i
<
j)
{
s
=
s
+
dt.Columns[i].ColumnName
+
"
\t
"
;
i
=
i
+
1
;
}
sw.WriteLine(s);
//
写数据
foreach
(DataRow r
in
dt.Rows )
{
i
=
0
;
s
=
""
;
while
(i
<
j)
{
if
(r[i].ToString().Trim()
==
""
)
{
s
=
s
+
"
"
+
"
\t
"
;
}
else
{
s
=
s
+
r[i].ToString().Trim()
+
"
\t
"
;
}
i
=
i
+
1
;
}
sw.WriteLine(s);
}
result
=
true
;
MessageBox.Show(
"
导出成功!
"
);
}
catch
{
result
=
false
;
MessageBox.Show(
"
导出失败!
"
);
}
finally
{
sw.Close();
}
}
}
return
result;
}
private
static
string
OpenSaveDialog()
{
string
result
=
""
;
SaveFileDialog sfd
=
new
SaveFileDialog();
sfd.Filter
=
"
Excel文件(*.xls)|*.xls|文本文件(*.txt)|*.txt
"
;
sfd.OverwritePrompt
=
true
;
if
(sfd.ShowDialog()
==
DialogResult.OK)
{
result
=
sfd.FileName;
}
return
result;
}
}
}
查看全文
相关阅读:
SSD3 MultipleChoice Quiz 3
vivizhyy 喜欢 win7 任务栏的地方
win7 蓝屏事件
win7 窗口靠近屏幕边缘时……
写 SSD3 遇见很 囧 的事情
用上 win7 了
win7 快捷键
SSD3 : MultipleChoice Quiz 4
EasyUI——常见用法总结
JQuery——那些当时我想不到的知识点
原文地址:https://www.cnblogs.com/madgoat/p/626810.html
最新文章
javaScript输出指定的时间格式 程序员
cookies的详细使用说明 程序员
Java删除文件夹和文件 程序员
struts2里面session设置 程序员
cocos2d2.0新增功能说明
苹果企业开发和教育开发相关专题
InApp Purchase 专题
通过UINib加速table视图的cell加载
网络编程总结
Xcode中的子项目(译文)
热门文章
Present ViewController详解
watchPoint
cocos2d 2.x 实现精灵的会移动的遮罩
iOS 使用Quartz 2D画虚线 .
mysql sql语句 列取别名
datareader 里如果有给combox.selectIndex赋值,且combox的SelectedIndexChanged(object sender, EventArgs e)事件里有调数据库,是不可以的
c# winform label 内容靠右,先设置自动长度为false才行
listview列宽不能为0
win7 窗口切换
win7 关机速度比较快
Copyright © 2011-2022 走看看