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;
}
}
}
查看全文
相关阅读:
Python【每日一问】38
Python【每日一问】37
Shell~echo -e 颜色输出
Python【每日一问】36
Python【每日一问】35
聊聊、Java 命令 第二篇
聊聊、RabbitMQ 配置文件
聊聊、Java 命令 第一篇
聊聊、CA机构认证CSR生成
聊聊、Tomcat中文乱码和JVM设置
原文地址:https://www.cnblogs.com/madgoat/p/626810.html
最新文章
Python-OpenCV中的cv2.threshold
Python-OpenCV中的图像模糊
Python-OpenCV中的图像轮廓检测
Python-OpenCV中图像颜色空间转换
OpenCV Face Detection
Python练习题
云计算之KVM虚拟化实战
Rsync备份服务部署
NFS文件系统存储服务部署
Redhat 6.4 linux系统不重启识别热添加的硬盘方法
热门文章
RedHat6.4安装图形行化界面
linux系统批量创建用户和生成8位随机密码
windows_Bat_Scripts查看系统IP-更改regedit-更新系统补丁
Window_Bat_Scripts—检测特定网段未使用的IP地址
CentOS 7 bonding模式双网卡绑定
VS Code 提示‘未找到Git。请安装Git,或在“git.path”设置中配置’
find命令常用场景
Shell脚本——添加和删除用户
Shell脚本——显示系统上的登录用户数
Shell脚本——求随机数的最值
Copyright © 2011-2022 走看看