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;
}
}
}
查看全文
相关阅读:
make_shared和shared_ptr的区别
bitcoined
ofstream的使用方法--超级精细。C++文件写入、读出函数(转)
visual studio 安装过程
Socket通信原理探讨(C++为例)
以太坊的记录和关注点
以太坊的应用
以太坊
比特币、以太坊
c++
原文地址:https://www.cnblogs.com/madgoat/p/626810.html
最新文章
自添加LUCI菜单及编译为ipk
OpenWrt超时检测
mt7620a拓展串口
Libcurl交叉编译
OpenWrt-Git依赖报错
【C++】设置c语言回调
【C++】 string变量不能使用memset
【C++】string通过key获取值
怎么找出标注中没有任何目标的图片?
训练loss不下降原因集合
热门文章
如何读/写论文???
自动车牌识别(ALPR)---https://blog.csdn.net/ELEVEN_ZOU/article/details/80893579
C++实现RTMP协议发送H.264编码及AAC编码的音视频,摄像头直播
H264音视频直播系统 服务器端+客户端源码 可用于视频聊天、视频会议
采集音频和摄像头视频并实时H264编码及AAC编码
ASP.NET开发在JavaScript有中文汉字时出现乱码时简单有效的解决方法
javascript GB2312转UTF8
网站部署后,ie不能显示本地的图片
WEB开发时Browser控件得到C:fakepath 的解决方式
RAII和unique_ptr
Copyright © 2011-2022 走看看