Without office in windows 2016 server, how to open excel file on web form?

Kerry Ou 226 Reputation points
2021-05-25T08:51:16.997+00:00

Hi all,
I want to import excel data in to web form gridview . I can run it on my PC ( vs 2019 + win 10 + office 2010) . But can't run at windows 2016 server (no office).
Is it possible not to install office and run these codes ?
Thank you so much.

    Protected Sub Excel_To_GridView2()

        Try
            Dim fileName As String = Server.HtmlEncode(ExcelUpload.FileName)
            Dim strConn As String = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=D:\Temp inport files\" & fileName & ";Extended Properties='Excel 12.0;HDR=YES'"

            'MsgBox(strConn)

            Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn)

            Dim ds As DataSet = New DataSet

            da.Fill(ds, "dw")

            Me.GridView2.DataSource = ds
            Me.GridView2.DataMember = "dw"
            Me.GridView2.DataBind()


        Catch ex As Exception
            WebMessageBox(Me.Page, ex.Message)
            'MsgBox(ex.Message)
        End Try

    End Sub
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,249 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2021-05-25T14:12:49.183+00:00

    Your options if the above is not working is to use OpenXML (which take sometime to learn), a free library such as EPPlus or a third party paid for library.

    EPPlus example taken from here.

    Option Infer On
    
    Public Shared Function getDataTableFromExcel(ByVal path As String) As DataTable
        Using pck = New OfficeOpenXml.ExcelPackage()
            Using stream = File.OpenRead(path)
                pck.Load(stream)
            End Using
            Dim ws = pck.Workbook.Worksheets.First()
            Dim tbl As New DataTable()
            Dim hasHeader As Boolean = True ' adjust it accordingly( i've mentioned that this is a simple approach)
            For Each firstRowCell In ws.Cells(1, 1, 1, ws.Dimension.End.Column)
                tbl.Columns.Add(If(hasHeader, firstRowCell.Text, String.Format("Column {0}", firstRowCell.Start.Column)))
            Next firstRowCell
            Dim startRow = If(hasHeader, 2, 1)
            For rowNum = startRow To ws.Dimension.End.Row
                Dim wsRow = ws.Cells(rowNum, 1, rowNum, ws.Dimension.End.Column)
                Dim row = tbl.NewRow()
                For Each cell In wsRow
                    row(cell.Start.Column - 1) = cell.Text
                Next cell
                tbl.Rows.Add(row)
            Next rowNum
            Return tbl
        End Using
    End Function
    

0 additional answers

Sort by: Most helpful