Read Excel File Data From HttpPostedFileBase object

Tag: excel Author: BOSSZHANG_REN Date: 2009-08-25

Uploaded Excel file Is In HttpPostedFileBase object

HttpPostedFileBase hpf = Request.Files["ExcelFileeUploader"];

Want To Read Excel Data From This Object. thanks.

Best Answer

If you use EPPlus, it's as simple as this:

public void ImportExcelXls(HttpPostedFileBase fileBase)
{
    using (var package = new ExcelPackage(fileBase.InputStream))
    {
        // get the first worksheet in the workbook
        ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
        int col = 1;

        for (int row = 1; worksheet.Cells[row, col].Value != null; row++)
        {
            // do something with worksheet.Cells[row, col].Value                    
        }
    } // the using 
}