注册 登录
编程论坛 ASP.NET技术论坛

网站发布后,excel表中的信息不能导入sql数据库

kafeidou 发布于 2007-09-28 15:52, 1278 次点击

网站未发布时,我可以把excel表中的数据成功的导入到sql数据库中;发布后,不能导入,这是怎么回事??;
难道是取文件路径时出错了?做程序时,我取excel文件路径用的是以下语句,请大家帮忙看看啊
string filePath = this.FileUpload1.PostedFile.FileName;
filePath = this.FileUpload1.PostedFile.FileName;

if (filePath == "")
{
Response.Write("<script>alert('请先选择文件')</script>");
return;
}
string connExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0";

{
OleDbConnection oleDbConnection = new OleDbConnection(connExcel);
oleDbConnection.Open();
..................
}

5 回复
#2
爱编程的小猪2007-09-29 09:58

给你个我写的仅供参考:

protected void Button1_Click(object sender, EventArgs e)
{
string fullFileName = this.FileUpload1.PostedFile.FileName;//获取本地机文件的路径名

string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);//获取文件名

string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);//获取文件名中的扩展名

if (type == "xls")//判断扩展名
{

this.FileUpload1.PostedFile.SaveAs(Server.MapPath("up") + "\\" + fileName);//记录文件名到服务器相对应的文加中
string myCount = this.FileUpload1.PostedFile.ContentLength.ToString();//计算上传文件的长度
this.Label2.Text = "所上传的字节数:" + myCount + "k";
this.Label3.Text = "您的本地文件路径为" + fullFileName;

}
else
{
Response.Write("<script language='javascript'>alert('格式有误!')</script>");//脚本对话框框
return;
}
string strpath = Server.MapPath("up") + "\\" + fileName;
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source='" + strpath + "';" +
"Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet ds = new DataSet();
myCommand.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
int rows = ds.Tables[0].Rows.Count;
int cols = ds.Tables[0].Columns.Count;
tbAdapter = new TableCodeTableAdapter();

for (int i = 0; i < rows; i++)
{
str1=ds.Tables[0].Rows[i].ItemArray[0].ToString();
str2=ds.Tables[0].Rows[i].ItemArray[1].ToString();
tbAdapter.Insert(str1, str2);
}

#3
爱编程的小猪2007-09-29 09:59
需要配合强类型DataSet使用。
#4
kafeidou2007-10-05 10:05

谢谢楼上兄弟了,我用了上面代码中的提示,终于把问题解决了,非常谢谢哦

#5
kafeidou2007-10-05 10:57

对了,我想问一下,我们这儿也要结贴吗?怎么一直没找到要结贴的地方哦

1