protected void btn_Import_Click(object sender, EventArgs e)
{
try
{
string fileName = FileUpload1.FileName;
if (string.IsNullOrEmpty(fileName)) throw new Exception("请选择要上传的文件!");
if (Path.GetExtension(fileName).ToLower() != ".xls") throw new Exception("请上传正确的文件类型(xls)");
string date = "(" + DateTime.Now.ToShortDateString() + ")";
FileUpload1.SaveAs(Server.MapPath("excel/" + date + fileName));
string filePath = Server.MapPath("excel/" + date + fileName);
string mystring = string.Format("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '{0}';Extended Properties=Excel 8.0", filePath);
OleDbConnection cnnxls = new OleDbConnection(mystring);
DataTable dt = new DataTable();
try
{
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
myDa.Fill(dt);
}
catch (Exception ole)
{
throw new Exception("数据源获取数据失败:" + ole.Message);
}
finally
{
cnnxls.Dispose();
cnnxls.Close();
}
int itemCount = 0;
Import(dt, ref itemCount);
Page.RegisterClientScriptBlock("", "<script>returnOk(" + itemCount + ")</script>");
}
catch (Exception err)
{
lab_Msg.Text = "错误:" + err.Message;
}
}
protected void Import(DataTable DT, ref int num)
{
string sql = string.Empty;
string hth = string.Empty;
DataTable dt;
try
{
for (int i = 3; i < DT.Rows.Count; i++)
{
dt = null;
string sys_guid= System.Guid.NewGuid().ToString();
// 插入数据库
num++;
}
ida.Close(true);
}
catch (Exception e)
{
ida.Close(false);
throw new Exception("数据导入失败:" + e.Message);
}
}