Upload Excel file and extract data - asp.net mvc 3

前端 未结 4 445
终归单人心
终归单人心 2021-02-04 14:51

I am wondering how do I extract data out of a 2007 excel file? I am using asp.net mvc 3. My plan is to have a upload section that you choose a file and hit upload. I have no clu

4条回答
  •  佛祖请我去吃肉
    2021-02-04 14:59

    I've used NPOI and it's quite simple to use:

    Using Xlfile As FileStream = New FileStream(FileName, FileMode.Open, FileAccess.Read)
        Using XLBook As HSSFWorkbook = New HSSFWorkbook(Xlfile)
            Using XLSheet As NPOI.SS.UserModel.Sheet = XLBook.GetSheetAt(0)
    
                Dim CurrentRow As NPOI.HSSF.UserModel.HSSFRow
                            Dim CurrentCell As NPOI.SS.UserModel.Cell
                            Dim RowEnum As IEnumerator = XLSheet.GetRowEnumerator()
    
                While RowEnum.MoveNext
                              If (RowEnum.Current IsNot Nothing) Then
                                  CurrentRow = TryCast(RowEnum.Current, NPOI.HSSF.UserModel.HSSFRow)
                        Select Case CurrentCell.CellType
                            Case NPOI.SS.UserModel.CellType.STRING
                                ' CurrentCell.StringCellValue
                            Case NPOI.SS.UserModel.CellType.NUMERIC
                                ' CurrentCell.NumericCellValue.ToString()
                        End Select
                End While
            End Using
        End Using
        Xlfile.Close()
    End Using
    

提交回复
热议问题