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;
}
}
}
查看全文
相关阅读:
百度Hi之CSRF蠕虫攻击
Portlet之讲解
try-catch语句讲解
unset之讲解
MySQL bin-log 日志清理方式
php数组array_push()和array_pop()以及array_shift()函数
php中的func_num_args、func_get_arg与func_get_args函数
PHP is_callable 方法
如何实现php异步处理
Mysql并发时经典常见的死锁原因及解决方法
原文地址:https://www.cnblogs.com/madgoat/p/626810.html
最新文章
mysql 的存储引擎介绍
mysql 源代码目录及安装目录介绍
【MySQL】查看MySQL配置文件路径及相关配置
MySQL入门02-MySQL二进制版本快速部署
Linux shell编程 4 ---- shell中的循环
HashMap的使用方法及注意事项
链表list容器中通过splice合并链表与merge的不同,及需要注意的问题
(HYSBZ)BZOJ 1588 营业额统计
智能家居项目
设计模式原则
热门文章
黑马程序员 Java正则表达式,详解反斜线在Java中的作用
uva11624
结构体对齐
fragment的入门DEMO
JavaScript之With语句讲解
JavaScript之表单验证讲解
JavaScript之isNaN()函数讲解
JavaScript之Throw、Try 、Catch讲解
p3p之讲解
CSRF攻击之原理讲解
Copyright © 2011-2022 走看看