在页面完成读取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";
}
}
}
}
}