在页面完成读取EXCEL
Posted 男孩IT小菜鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在页面完成读取EXCEL相关的知识,希望对你有一定的参考价值。
protected void btnUpload_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string sFILENAME = string.Empty;
string sFILE_EXT = string.Empty;
string sFILE_MIME_TYPE = string.Empty;
HttpPostedFile pstIMPORT = fileIMPORT.PostedFile;
if (pstIMPORT != null)
{
if (pstIMPORT.FileName.Length > 0)
{
sFILENAME = Path.GetFileName(pstIMPORT.FileName);
sFILE_EXT = Path.GetExtension(sFILENAME);
sFILE_MIME_TYPE = pstIMPORT.ContentType;
}
if (sFILE_EXT.ToLower() != ".xls")
{
lblError.Text = "The file format is incorrect, please select excel 2003 format file.";
return;
}
ExcelDataReader.ExcelDataReader spreadsheet = new ExcelDataReader.ExcelDataReader(pstIMPORT.InputStream);
if (spreadsheet.WorkbookData.Tables.Count > 0)
{
dt = spreadsheet.WorkbookData.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
if (dt != null && dt.Rows.Count > 0)
{
lblError.Text = "Data read successfully";
}
}
}
}
}
以上是关于在页面完成读取EXCEL的主要内容,如果未能解决你的问题,请参考以下文章