Read excel file from a stream

后端 未结 5 1464
粉色の甜心
粉色の甜心 2020-11-30 12:43

I need a way to read a Excel file from a stream. It doesn\'t seem to work with the ADO.NET way of doing things.

The scenario is that a user uploads a file through a

5条回答
  •  借酒劲吻你
    2020-11-30 13:05

    This can be done easily with EPPlus.

    //the excel sheet as byte array (as example from a FileUpload Control)
    byte[] bin = FileUpload1.FileBytes;
    
    //gen the byte array into the memorystream
    using (MemoryStream ms = new MemoryStream(bin))
    using (ExcelPackage package = new ExcelPackage(ms))
    {
        //get the first sheet from the excel file
        ExcelWorksheet sheet = package.Workbook.Worksheets[1];
    
        //loop all rows in the sheet
        for (int i = sheet.Dimension.Start.Row; i <= sheet.Dimension.End.Row; i++)
        {
            //loop all columns in a row
            for (int j = sheet.Dimension.Start.Column; j <= sheet.Dimension.End.Column; j++)
            {
                //do something with the current cell value
                string currentCellValue = sheet.Cells[i, j].Value.ToString();
            }
        }
    }
    

提交回复
热议问题