using
System;
using
System.Data;
using
System.Data.OracleClient;
using
Microsoft.Office.Interop.Excel;
using
Excel
=
Microsoft.Office.Interop.Excel;
using
RedGlovePermission.Lib;
using
System.IO;
using
System.Data.OleDb;
using
System.Data;
using
System.Configuration;
using
System.Text;
using
System.Collections;
using
System.Collections.Generic;
using
System.Web.UI;
public
partial
class
YQKH_YQKH_OACL : BasePageWithLog,ICallbackEventHandler
{
private
BusinessLogicLayer.ICommonBLL _CommonBLL;
public
BusinessLogicLayer.ICommonBLL CommonBLL
{
get
{
return
_CommonBLL;
}
set
{
_CommonBLL
=
value;
}
}
public
static
string
result
=
string
.Empty;
protected
void
Page_Load(
object
sender, EventArgs e)
{
this
.DBGridConfig1.openURL
=
"
../CommData/GetDataByParameter.aspx?sqlKey=t_yqkh_oa_mx
"
;
//
加载需要进行导入的表中的数据
}
protected
void
toExcel_Click(
object
sender, EventArgs e)
{
if
(UploadExcel.HasFile)
//
如果存在上传文件
{
string
filename
=
UploadExcel.PostedFile.FileName;
String strConnectionString
=
string
.Format(
"
Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;IMEX=1'
"
, filename);
//
serverpath是Excel表格路径
using
(OleDbConnection Excel_conn
=
new
OleDbConnection(strConnectionString))
//
连接Excel,和连接MSSQL数据库的方式类似
{
Excel_conn.Open();
//
打开连接
System.Data.DataTable dtExcelSchema
=
Excel_conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new
object
[] {
null
,
null
,
null
,
"
TABLE
"
});
//
建立连接Excel的数据表
string
SheetName
=
""
;
SheetName
=
dtExcelSchema.Rows[
3
][
"
TABLE_NAME
"
].ToString();
//
取需要的工作表的名称
System.Data.DataTable dt
=
new
System.Data.DataTable();
try
{
string
strExcel
=
""
;
OleDbDataAdapter myCommand
=
null
;
strExcel
=
string
.Format(
"
select * from [{0}]
"
, SheetName);
//
取工作表中的所有数据
myCommand
=
new
OleDbDataAdapter(strExcel, Excel_conn);
myCommand.Fill(dt);
}
catch
(Exception ex){}
using
(OracleConnection oraconn
=
new
OracleConnection(ConfigurationManager.AppSettings[
"
SQLString
"
].ToString()))
//
打开oracle数据库
{
oraconn.Open();
IList
<
StringBuilder
>
_list
=
ExecInsertIntoOracleFromExcel(dt);
//
list中保存的是多个需要提交的事物段
foreach
(StringBuilder sb
in
_list)
//
循环,按照段来执行
{
OracleCommand oracmd
=
new
OracleCommand(sb.ToString(), oraconn);
oracmd.ExecuteNonQuery();
}
//
Response.Write("导入成功!");
ClientScript.RegisterClientScriptBlock(Page.GetType(),
"
alertSuccess
"
,
"
<script>alert('导入成功!')</script>
"
);
this
.DBGridConfig1.openURL
=
"
../CommData/GetDataByParameter.aspx?sqlKey=t_yqkh_oa_mx
"
;
}
}
}
}
public
int
iCount
=
0
;
private
IList
<
StringBuilder
>
ExecInsertIntoOracleFromExcel(System.Data.DataTable dt)
{
IList
<
StringBuilder
>
iList
=
new
List
<
StringBuilder
>
();
try
{
this
._CommonBLL.ExecuteQuery(
"
delete from db_zgfz.t_yqkh_oa
"
);
}
catch
(Exception ex)
{
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),
"
alertFail
"
,
"
<script>删除数据失败!</script>
"
);
}
if
(dt
!=
null
)
{
if
(dt.Rows.Count
>
0
)
{
StringBuilder sql
=
new
StringBuilder();
sql.Append(
"
begin
"
);
foreach
(DataRow dr
in
dt.Rows)
{
iCount
++
;
sql.Append(
"
insert into db_zgfz.t_yqkh_oa values('
"
+
dr[
"
受理号
"
].ToString()
+
"
','
"
+
dr[
"
企业名称
"
].ToString()
+
"
','
"
+
dr[
"
事项名称
"
].ToString()
+
"
','
"
+
dr[
"
事项所属类别
"
].ToString()
+
"
','
"
+
dr[
"
事项ID
"
].ToString()
+
"
','
"
+
dr[
"
工作环节
"
].ToString()
+
"
','
"
+
dr[
"
部门
"
].ToString()
+
"
','
"
+
dr[
"
流经部门开始日期
"
].ToString()
+
"
','
"
+
dr[
"
流经部门结束日期
"
].ToString()
+
"
');
"
);
if
(iCount
%
1000
==
0
)
//
每隔1000条数据,当做一段来执行,缩小出错范围
{
sql.Append(
"
commit; end;
"
);
iList.Add(sql);
sql
=
new
StringBuilder();
sql.Append(
"
begin
"
);
}
else
if
(dt.Rows.Count
==
iCount)
{
sql.Append(
"
commit; end;
"
);
iList.Add(sql);
}
}
}
}
return
iList ;
}
#region
ICallbackEventHandler 成员
public
string
GetCallbackResult()
//
主要显示当前插入了多少条数据
{
return
"
当前共导入:
"
+
result
+
"
条数据!
"
;
}
public
void
RaiseCallbackEvent(
string
eventArgument)
{
string
sql
=
"
SELECT COUNT(*) FROM t_yqkh_oa
"
;
System.Data.DataTable dtt
=
this
._CommonBLL.ADONETHelper.GetDataTableBySQL(CommandType.Text, sql);
if
(dtt
!=
null
)
{
if
(dtt.Rows.Count
>
0
)
{
result
=
dtt.Rows[
0
][
0
].ToString();
}
}
}
#endregion
}