讀取excel
/// <summary>
/// 從選擇的excel導入
/// </summary>
/// <returns></returns>
public DataSet ImportExcel()
{
//文件路徑
string ExcelName = ofd_in.FileName;
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1';";//連接excel文件的字符串
if (ExcelName == null)
{
return null;
}
OleDbConnection odcon = new OleDbConnection(strcon);//建立連接
odcon.Open();//打開連接
System.Data.DataTable sTable = odcon.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
//Sheets Name
string tableName = sTable.Rows[0][2].ToString().Trim();
if (tableName == "")
{
return null;
}
else
{
tableName = "[" + tableName + "]";
}
OleDbDataAdapter odda = new OleDbDataAdapter("select * from " + tableName, odcon);
DataSet ds = new DataSet();
try
{
odda.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return ds;
}
為了解決導入超時問題,把邏輯寫到webserice中,把dataset傳到webservice中,同時把每1000條數據提交一次到數據庫中,返回字符串到客戶端再導出未excel.
try
{
CouponDetail ltw = new CouponDetail();
string[] StrArr = new string[ds.Tables[0].Rows.Count];
int icnt = ds.Tables[0].Rows.Count;
ltw.CouCpnSeq = strCpnseq;
string tmpsql = "select startdate,enddate from gcmis.crm_coupon where cpnseq='" + strCpnseq + "'";
DataSet dsdate = OraHelper.Adapter(OraHelper.CONN_STRING, CommandType.Text, tmpsql, null);
//根據會員數量分幾次批量提交(1000個提交一次)
int itmp = icnt / 1000;
for (int j = 0; j < itmp + 1; j++)
{
using (OracleConnection conn = OraHelper.PrepareConn())
{
using (OracleTransaction trans = conn.BeginTransaction())
{
#region for 循環
//中間變量
int tmpcnt = 0;
if (icnt < 1000 || j == itmp) //(j + 1)*1000
{
tmpcnt = icnt;
}
else
{
tmpcnt = (j + 1) * 1000;
}
//for循環
for (int i = j * 1000; i < tmpcnt; i++)
{
//string tmpsql = "select Crm_CouponDetail_CpnDSeq.nextval from dual";
//DataSet dscpnd = OraHelper.Adapter(OraHelper.CONN_STRING, CommandType.Text, tmpsql, null);
//第一行為標題跳出循環
if (i == 0)
{
continue;
}
tmpsql = "select name from crm_customer cm,mis_member mb ";
tmpsql += " where cm.customersn=mb.customersn ";
tmpsql += " and memberid='" + ds.Tables[0].Rows[i][0].ToString() + "'";
DataSet dsname = OraHelper.Adapter(OraHelper.CONN_STRING, CommandType.Text, tmpsql, null);
if (dsname.Tables[0].Rows.Count > 0 && dsdate.Tables[0].Rows.Count > 0)
{
#region 會員卡號有效,執行插入操作
//判斷是否激活,'是'不需要綁定時間,'否'需要綁定時間
if (ds.Tables[0].Rows[i][1].ToString() == "是")
{
#region 是激活
//生成隨機優惠券編碼
string strCpnCode = string.Empty;
while (true)
{
string dt = DateTime.Today.ToString("yyMMdd");
Random ran = new Random();
string number = ran.Next(10000000, 99999999).ToString();
strCpnCode = dt + number;
//判斷數據庫中是否有生成的優惠券編碼
tmpsql = "select count(*) from crm_coupondetail where cpncode='" + strCpnCode + "'";
DataSet dscode = OraHelper.Adapter(OraHelper.CONN_STRING, CommandType.Text, tmpsql, null);
//不存在跳出循環,存在的話重新生成
if (dscode.Tables[0].Rows[0][0].ToString() == "0")
{
//之前生成的優惠券中含有現生成的優惠券編碼
if (tmpCode.IndexOf(strCpnCode) > 0)
{
//找到重復的,重新生成
continue;
}
else
{
tmpCode += strCpnCode + ",";
//未找到重復的,跳出循環,該編碼可以使用
break;
}
}
}
ltw.CouCpnSeq = strCpnseq;
ltw.CpnCode = strCpnCode;
ltw.MemberID = ds.Tables[0].Rows[i][0].ToString();
ltw.Member = dsname.Tables[0].Rows[0][0].ToString();
ltw.GeneratedOn = DateTime.Now.ToString();
if (dsdate.Tables[0].Rows[0]["startdate"].ToString() != "")
{
ltw.Startdate = DateTime.Parse(dsdate.Tables[0].Rows[0]["startdate"].ToString());
}
else
{
ltw.Startdate = DateTime.Parse(DateTime.Now.ToShortDateString());
}
if (dsdate.Tables[0].Rows[0]["enddate"].ToString() != "")
{
ltw.Enddate = DateTime.Parse(dsdate.Tables[0].Rows[0]["enddate"].ToString());
}
else
{
ltw.Enddate = DateTime.Parse(DateTime.Now.ToShortDateString());
}
ltw.IFACTIVE = "1";
CouponMgr cm = new CouponMgr(null, trans);
ir = cm.InsertCouponDetail(ltw);
//插入不成功,取不成功的memberid
if (ir <= 0)
{
tmpMemberid += ds.Tables[0].Rows[i][0].ToString() + ",";
}
#endregion
}
else
{
#region 不激活
//生成隨機優惠券編碼
string strCpnCode = string.Empty;
while (true)
{
string dt = DateTime.Today.ToString("yyyyMMdd");
Random ran = new Random();
string number = ran.Next(10000000, 99999999).ToString();
strCpnCode = dt + number;
//判斷數據庫中是否有生成的優惠券編碼
tmpsql = "select count(*) from crm_coupondetail where cpncode='" + strCpnCode + "'";
DataSet dscode = OraHelper.Adapter(OraHelper.CONN_STRING, CommandType.Text, tmpsql, null);
//不存在跳出循環,存在的話重新生成
if (dscode.Tables[0].Rows[0][0].ToString() == "0")
{
//之前生成的優惠券中含有現生成的優惠券編碼
if (tmpCode.IndexOf(strCpnCode) > 0)
{
//找到重復的,重新生成
continue;
}
else
{
tmpCode += strCpnCode + ",";
//未找到重復的,跳出循環,該編碼可以使用
break;
}
}
}
ltw.CouCpnSeq = strCpnseq;
ltw.CpnCode = strCpnCode;
ltw.MemberID = ds.Tables[0].Rows[i][0].ToString();
ltw.Member = dsname.Tables[0].Rows[0][0].ToString();
ltw.BandingOn = DateTime.Now.ToString();
ltw.GeneratedOn = DateTime.Now.ToString();
if (dsdate.Tables[0].Rows[0]["startdate"].ToString() != "")
{
ltw.Startdate = DateTime.Parse(dsdate.Tables[0].Rows[0]["startdate"].ToString());
}
else
{
ltw.Startdate = DateTime.Parse(DateTime.Now.ToShortDateString());
}
if (dsdate.Tables[0].Rows[0]["enddate"].ToString() != "")
{
ltw.Enddate = DateTime.Parse(dsdate.Tables[0].Rows[0]["enddate"].ToString());
}
else
{
ltw.Enddate = DateTime.Parse(DateTime.Now.ToShortDateString());
}
ltw.IFACTIVE = "0";
CouponMgr cm = new CouponMgr(null, trans);
ir = cm.InsertCouponDetail(ltw);
//插入不成功,取不成功的memberid
if (ir <= 0)
{
tmpMemberid += ds.Tables[0].Rows[i][0].ToString() + ",";
}
#endregion
}
#endregion
}
else
{
//會員卡號無效記錄下錯誤卡號返回到客戶端,生成錯誤報表
//tmptb.Rows.Add(new object[] { ds.Tables[0].Rows[i-1][0].ToString() });
tmpMemberid += ds.Tables[0].Rows[i][0].ToString() + ",";
}
}
#endregion
trans.Commit();
}
}
}
}catch(Exception ex)
{
ir = -1;
throw ex;
}
//賦值到傳出參數
strMemberid = tmpMemberid;
return ir;