|
一个上传excel表格数据的功能,目前将excel写入到datatable中,可是结果显示datatable为空,下面代码,有问题吗?
//********************************************************上传按钮的onclick事件函数**********************************************************
protected void btnShangChuan_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName;
fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
string filePath = Context.Server.MapPath("./upload/" + fileName);
FileUpload1.SaveAs(filePath);
labResult.Text = "保存成功!保存路径为:" +filePath;
DataTable dt = ExcelDt(filePath,fileName);//调用下面定义的ExcleDt函数,建立datatbale存储数据
if(dt!=null)
{
Grid_LinShi.DataSource = dt;//将存至datatable的数据暂时显示在下方形成预览效果
Grid_LinShi.DataBind();
}
}
}
//*************************************************命名exceldt函数将excel读入到datatable dt中去*********************************************
public DataTable ExcelDt(string filePath, string fileName)
{
//string strConn = "Provider=Microsoft.Jet.OleDb.4.0;"+"data source="+filePath+";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";//2003版本
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";//2007版本
string sql = "select * from [Sheet1$]";
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter odda = new OleDbDataAdapter(sql,conn);
DataTable dt = new DataTable();
odda.Fill(dt);
return dt;
}
|
|