public void upLoadFile()
{
if (!txtFileUpload.FileName.ToLower().EndsWith(".xls"))
{
showAlert("请选择Excel文件!");
return;
}
if (txtFileUpload.HasFile)
{
String path = Server.MapPath("~/UpLoadPicture/");
DateTime now = DateTime.Now;
String fileName = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + ".xls";
txtFileUpload.PostedFile.SaveAs(path + fileName);
StringBuilder connectString = new StringBuilder();
connectString.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data source=");
connectString.Append(path + fileName);
connectString.Append(";Extended Properties=/"Excel 8.0;HDR=Yes;IMEX=1;/"");
OleDbConnection conn = new OleDbConnection(connectString.ToString());
OleDbCommand comm = conn.CreateCommand();
DataSet ds = new DataSet();
try
{
comm.CommandText = "select 手机号码 as Num from [sheet1$]";
OleDbDataAdapter oda = new OleDbDataAdapter(comm);
if (comm.Connection.State == ConnectionState.Closed)
comm.Connection.Open();
oda.Fill(ds);
}
catch
{
showAlert("您导入的数据不正确,请重新下载模板后填写数据导入!");
return;
}
finally
{
if (comm.Connection.State == ConnectionState.Open)
comm.Connection.Close();
}
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
txtDestNumber.Text += dr["Num"].ToString() + ",/r/n";
}
txtDestNumber.Text = txtDestNumber.Text.Substring(0, txtDestNumber.Text.Length - 3);
}
System.IO.FileInfo file = new System.IO.FileInfo(path + fileName);
file.Delete();
}