//读取excel中内容
private DataSet ReadExcel()
{
string lj = FileUpload1.PostedFile.FileName;
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + lj + "; Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter("Select * FROM [Sheet1$]", strCon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
Extended Properties :设置 Excel 特定的属性。
“HDR=Yes;”指示第一行中包含列名,而不是数据;
“HDR=NO;”指示第一行中不包含列名,而是数据;
IMEX=1;”通知驱动程序始终将“互混”数据列作为文本读取;
//将excel中值显示在Gridview1中
protected void Button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(this.FileUpload1.PostedFile.FileName))
{
if (FileUpload1.FileName.EndsWith(".xls"))
{
this.Gridview1.DataSourse=ReadExcel;
this.Gridview1.DataBind();
}
else
{
MessagerShow.MsgBox(this, "请输入正确的Excel文件");
}
}
else
{
MessagerShow.MsgBox(this, "请选择要导入的Excel文件");
}
}
221

被折叠的 条评论
为什么被折叠?



