using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string asd = "C:\Users\owlcity33\Desktop\222.xlsx";
DataSet ds = ExcelToDS(asd);
DataTable dt = ds.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
}
public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + Path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel,strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
return ds;
}
protected void Button1_Click(object sender, EventArgs e)
{
string PH = FileUpload1.PostedFile.FileName.ToString();
DataSet ds1= ExcelToDS(PH);
DataTable dt1 = ds1.Tables[0];
g.DataSource = dt1;
g.DataBind();
}
}
我在pageload事件里导了一次excel表,在网页上gridview能显示excel表的内容,
但是我在buTTON事件里用fileupload获取的路径导入excel表的时候就回报“外部表不是预期格式”这个错误,请问是为什么呢???
导入excel表的方法都一样,参数格式也都是string 但是后面button那个就会报错。。。
gridview里面没做什么转换吧,做转换的话自己看数据源excel文件格式是否正确的了