zoukankan
html css js c++ java
excelhelp
using
System.Data.OleDb;
using
System;
using
System.IO;
using
System.Data;
using
System.Text;
using
System.Collections;
namespace
Wxy.Util
{
/**/
///
<summary>
///
ExcelHelper 的摘要说明。
///
</summary>
public
class
ExcelHelper
{
public
ExcelHelper()
{
//
//
TODO: 在此处添加构造函数逻辑
//
}
/**/
///
<summary>
///
读取Excel文档
///
</summary>
///
<param name="Path">
文件名称
</param>
///
<returns>
返回一个数据集
</returns>
public
static
DataSet ExcelToDS(
string
Path)
{
try
{
string
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+
Path
+
"
;
"
+
"
Extended Properties=Excel 8.0;
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
conn.Open();
string
strExcel
=
""
;
OleDbDataAdapter myCommand
=
null
;
DataSet ds
=
null
;
strExcel
=
"
select * from [Sheet1$]
"
;
myCommand
=
new
OleDbDataAdapter(strExcel, strConn);
ds
=
new
DataSet();
myCommand.Fill(ds,
"
table1
"
);
return
ds;
}
catch
(System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine (
"
写入Excel发生错误:
"
+
ex.Message );
return
null
;
}
}
public
static
void
DataTableToExcel(System.Data.DataView dataview,
string
Path,Hashtable NameMap)
{
try
{
string
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+
Path
+
"
;
"
+
"
Extended Properties=Excel 8.0;
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbCommand cmd
=
new
OleDbCommand ();
cmd.Connection
=
conn;
string
strSql
=
string
.Empty ,strSql1
=
string
.Empty ;
int
i , j ;
for
( i
=
0
; i
<
dataview.Count ; i
++
)
{
strSql
=
"
INSERT INTO [sheet1$] (
"
;
strSql1
=
"
) values(
"
;
for
( j
=
0
; j
<
dataview.Table.Columns.Count ; j
++
)
{
if
( NameMap.ContainsKey( dataview.Table.Columns[j].ColumnName ) )
{
strSql
+=
NameMap[dataview.Table.Columns[j].ColumnName]
+
"
,
"
;
//
2414210
strSql1
+=
"
'
"
+
dataview[i][j].ToString()
+
"
',
"
;
}
}
try
{
if
( strSql.EndsWith(
"
,
"
) )
strSql
=
strSql.Substring(
0
,strSql.Length
-
1
) ;
if
( strSql1.EndsWith(
"
,
"
) )
strSql1
=
strSql1.Substring(
0
,strSql1.Length
-
1
) ;
strSql1
=
strSql1
+
"
)
"
;
strSql
=
strSql
+
strSql1 ;
cmd.CommandText
=
strSql ;
cmd.ExecuteNonQuery();
}
catch
(Exception ex)
{
System.Diagnostics.Debug.WriteLine (
"
写入Excel发生错误:
"
+
strSql
+
strSql1
+
ex.Message );
throw
new
Exception(strSql
+
ex.Message);
}
}
conn.Close ();
}
catch
(System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine (
"
写入Excel发生错误:
"
+
ex.Message );
}
}
public
static
void
DataTableToExcel(System.Data.DataView dataview,
string
Path)
{
try
{
string
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+
Path
+
"
;
"
+
"
Extended Properties=Excel 8.0;
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbCommand cmd
=
new
OleDbCommand ();
cmd.Connection
=
conn;
string
strSql
=
string
.Empty ,strSql1
=
string
.Empty ;
int
i , j ;
for
( i
=
0
; i
<
dataview.Count ; i
++
)
{
strSql
=
"
INSERT INTO [sheet1$] (
"
;
strSql1
=
"
) values(
"
;
for
( j
=
0
; j
<
dataview.Table.Columns.Count ; j
++
)
{
strSql
+=
dataview.Table.Columns[j].ColumnName
+
"
,
"
;
strSql1
+=
"
'
"
+
dataview[i][j].ToString()
+
"
',
"
;
}
//
try
{
if
( strSql.EndsWith(
"
,
"
) )
strSql
=
strSql.Substring(
0
,strSql.Length
-
1
) ;
if
( strSql1.EndsWith(
"
,
"
) )
strSql1
=
strSql1.Substring(
0
,strSql1.Length
-
1
) ;
strSql1
=
strSql1
+
"
)
"
;
strSql
=
strSql
+
strSql1 ;
cmd.CommandText
=
strSql ;
cmd.ExecuteNonQuery();
}
catch
(Exception ex)
{
System.Diagnostics.Debug.WriteLine (
"
写入Excel发生错误:
"
+
strSql
+
ex.Message );
throw
new
Exception(strSql
+
ex.Message);
}
}
conn.Close ();
}
catch
(System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine (
"
写入Excel发生错误:
"
+
ex.Message );
}
}
/**/
///
<summary>
///
写入Excel文档
///
</summary>
///
<param name="Path">
文件名称
</param>
public
bool
SaveFP2toExcel(
string
Path)
{
try
{
string
strConn
=
"
Provider=Microsoft.Jet.OLEDB.4.0;
"
+
"
Data Source=
"
+
Path
+
"
;
"
+
"
Extended Properties=Excel 8.0;
"
;
OleDbConnection conn
=
new
OleDbConnection(strConn);
conn.Open();
System.Data.OleDb.OleDbCommand cmd
=
new
OleDbCommand ();
cmd.Connection
=
conn;
cmd.CommandText
=
"
UPDATE [sheet1$] SET 姓名='2005-01-01' WHERE 工号='日期'
"
;
cmd.ExecuteNonQuery ();
/**/
/*
for(int i=0;i<fp2.Sheets [0].RowCount -1;i++)
{
if(fp2.Sheets [0].Cells[i,0].Text!="")
{
cmd.CommandText ="INSERT INTO [sheet1$] (工号,姓名,部门,职务,日期,时间) VALUES('"+fp2.Sheets [0].Cells[i,0].Text+ "','"+
fp2.Sheets [0].Cells[i,1].Text+"','"+fp2.Sheets [0].Cells[i,2].Text+"','"+fp2.Sheets [0].Cells[i,3].Text+
"','"+fp2.Sheets [0].Cells[i,4].Text+"','"+fp2.Sheets [0].Cells[i,5].Text+"')";
cmd.ExecuteNonQuery ();
}
}
*/
conn.Close ();
return
true
;
}
catch
(System.Data.OleDb.OleDbException ex)
{
System.Diagnostics.Debug.WriteLine (
"
写入Excel发生错误:
"
+
ex.Message );
}
return
false
;
}
}
}
keim,毕业于安徽科技学院理学院,2003年开始对Web开发有浓厚的兴趣,并专注于C#/java Web开发,软件架构设计、分布式相关、项目管理、前端设计等等,实战派...
查看全文
相关阅读:
Eclipse给方法添加注释
隐式等待和显示等待和固定等待
用例执行完后切换到指定的页面
HTTP常见响应状态码
Java类初始化顺序问题
博客园!我来了
MySQL的存储引擎
String声明为NULL和""的区别
Mysql入门
MySQL查询与结构
原文地址:https://www.cnblogs.com/zqmingok/p/1529710.html
最新文章
《程序员》二月刊摘录
使用Java和R搭建统计分析server端
上海Python技术聚会
[转载] 生命是一种长期而持续的积累过程
JSObject的一个bug: 无法正确弹出html窗口
摘要:Information Storage Management, module 2, Storage Environment
html5 postMessage解决跨域、跨窗口消息传递
持续集成工具TeamCity配置使用
HTML代码中<%%>、<%=%>、<%:%>各是什么意思?分别用来实现什么的?
持续集成:TeamCity 的安装和使用
热门文章
lock与monitor的区别
asp.net怎样解决高并发问题
Excel找出两列相同部分
多线程传递参数
红包算法——最大最小值范围
Linux常用命令大全
本页面添加至收藏夹
asp.net 面试题(附答案)
APPium+java如果找到这个元素则继续执行下面的元素,实现方法
java解析xlsx方法
Copyright © 2011-2022 走看看