格式一定要統一,要不然程式就不好以什麼為標準進行判斷,
#region 返回開啟的試算表中的某個表並形成datatable
/// <summary>
/// 返回開啟的試算表中的某個表並形成datatable
/// </summary>
/// <param name="path">檔案絕對路徑</param>
/// <param name="target">要開啟的表名</param>
/// <returns>返回DATATABLE資料庫集</returns>
private void ReadExcel()
{
try
{
DataTable rs=new DataTable();
rs=null;
DataSet rDs=null;
string strConn="";
int count=0;
string id =Request["id"];
DateTime createtime=DateTime.Now ;
string date=createtime.Year.ToString()+"-"+createtime.Month.ToString ()+"-"+createtime.Day.ToString ()+" "+createtime.Hour.ToString()+":"+createtime.Minute.ToString()+":"+createtime.Second.ToString();
string sqlstr=" select * from tab_uppart_file where id ="+id+"";
string userid=data1.readout(sqlstr,1);
string fileName=data1.readout(sqlstr,3);
string path = Server.MapPath("../upparts/"+fileName);
string filepath="..\\upparts\\"+fileName;
if (File.Exists (Server.MapPath(filepath)))//判斷檔案是否存在
{
strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';"; //IMEX=1 用來轉換文本
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]",strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet,"[Sheet1$]");
foreach(DataRow row in myDataSet.Tables[0].Rows)
{
count++;
string classid="";
string name="";
string model="";
string price="";
string num="";
string mfg="";
string package="";
string remark="";
classid=row["產品類型"].ToString();
name=row["產品名稱"].ToString();
model=row["產品型號"].ToString();
price=row["產品價格"].ToString();
num=row["數 量"].ToString();
mfg=row["生產廠商"].ToString();
package=row["封 裝"].ToString();
remark=row["交易說明"].ToString();
if (name!="" && model!="")
{
sqlstr="insert into tab_part values('"+name+"','"+model+"','"+num+"','"+package+"','"+mfg+"','"+price+"','"+remark+"','"+classid+"','"+createtime+"','"+userid+"','0')";
data1.moddata(sqlstr);
}
}
string updatesql="update tab_uppart_file set flag='1',uploadtime='"+date+"' where id='"+id+"'";
data1.moddata(updatesql);
data1.Alert2 ("上傳記錄成功!","BatchAddParts.aspx",Page);
}
else
{
data1.Alert2("沒有找到相應的模板檔案","BatchAddParts.aspx",Page);
}
}
catch(System.Data.OleDb.OleDbException ex)
{
Response.Write(ex.Message );
}
}
#endregion