SqlC sc = new SqlC();
public string fileName;
public string serverPath;
public string filePath;
public void EtoG(string sql)
{
filePath = this.FileUpload1.PostedFile.FileName;//获取上传文件的路经;
fileName = filePath.Substring(filePath.LastIndexOf("//") + 1);//或得文件名;
string fileExtend = filePath.Substring(filePath.LastIndexOf(".") + 1);//或得扩展名;
if (fileExtend != "xls")//判断是否是xls文件
{
Response.Write("不是xls文件,请选择xls文件!!");
Response.End();
}
serverPath = Server.MapPath("excel") + "//" + fileName;//把文件保存到服务器上指定的路经
this.FileUpload1.SaveAs(serverPath);//保存文件到服务器上
serverPath = Server.MapPath("excel") + "//" + fileName;//把文件保存到服务器上指定的路经
this.FileUpload1.SaveAs(serverPath);//保存文件到服务器上
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + serverPath + "; Extended Properties=Excel 8.0;";
OleDbConnection ole = new OleDbConnection(strCon);
ole.Open();
OleDbDataAdapter da = new OleDbDataAdapter(sql, strCon);
DataSet ds = new DataSet();
da.Fill(ds);
ole.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + serverPath + "; Extended Properties=Excel 8.0;";
string strExcel = string.Format("select * from [sheet$]", fileName);
OleDbDataAdapter da1 = new OleDbDataAdapter(strExcel, strCon);
DataSet ds1 = new DataSet();
da1.Fill(ds1, fileName);
SqlBulkCopy bcp = new SqlBulkCopy("Server=localhost;DataBase=Keyantongji ;Integrated Security =SSPI");
bcp.SqlRowsCopied += new SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;
bcp.NotifyAfter = 100;
bcp.DestinationTableName = 目标表
bcp.WriteToServer(ds1.Tables[0]);
void bcp_SqlRowsCopied(object sender, SqlRowsCopiedEventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}