zoukankan
html css js c++ java
ExcelHelper代码
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
;
}
}
}
以下文章中用到的工具类
发布一个Excel导入数据到GridView的类
欢迎光临:
http://shiningrise.cnblogs.com
查看全文
相关阅读:
测开之路二十七:Flask基础之动态路由
测开之路二十六:Flask基础之最小web程序
测开之路二十五:彩票游戏
测开之路二十四:常见的排序
CSS快速入门-浮动(float)
CentOS下安装Tomcat环境
CSS快速入门-前端布局2(唯品会1)
CSS快速入门-前端布局1(抽屉)
CSS快速入门-后端布局
Delphi 中 函数参数中的 const 修饰符的本质以及注意事项
原文地址:https://www.cnblogs.com/shiningrise/p/817840.html
最新文章
通过id设置的css属性和通过元素设置的css属性冲突了,优先级哪个高?
长时间没有操作putty就会断开连接是怎么回事?
人生三大幸事
如何让浏览器打开一个网站的时候执行一个自定义脚本
案例5-条件查询商品
008-PageBean类模板
案例4-修改商品
案例3-删除商品
案例2-添加商品
案例1-后台商品列表的展示
热门文章
完成转账功能
使用三层架构完成商品列表的显示
Session笔记
Cookie笔记
测开之路三十三:Flask实现扎金花游戏
测开之路三十二:Flask基础之错误与重定向
测开之路三十一:Flask基础之请求与相应
测开之路三十:Flask基础之jinja2模板继承
测开之路二十九:Flask基础之jinja2模板
测开之路二十八:Flask基础之静态资源
Copyright © 2011-2022 走看看