如何在数据集中获取Excel单元格颜色以及数据VB.net

时间:2016-03-10 05:02:07

标签: vb.net excel

我想读取所有Excel单元格的颜色和数据,并使用数据集在数据网格视图中显示。我使用以下代码:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

 Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=C:\Documents\Visual Studio 2015\WebSites\WebSite7\Employees.xlsx;" &
                      Dim oledbConn As OleDbConnection = New OleDbConnection(connString)
        Try

            oledbConn.Open()
            Console.WriteLine("conneection opeened")

            Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn)

            Dim oleda As OleDbDataAdapter = New OleDbDataAdapter()
                oleda.SelectCommand = cmd
                      Dim ds As DataSet = New DataSet()
                oleda.Fill(ds)
                             gvEmployees.DataSource = ds
                    gvEmployees.DataBind()
        Catch e1 As Exception
                         Console.WriteLine(e1.Message)
            Finally
                            oledbConn.Close()
        End Try

    End Sub
    Public Function colorchange(ByVal sender As Object, ByVal e As EventArgs) As Object
        Dim excel_app As New Excel.ApplicationClass()
        Dim workbook As Excel.Workbook =
excel_app.Workbooks.Open(Filename:="C:\Documents\Visual Studio 2015\WebSites\WebSite7\Employees.xlsx")
                    Dim sheet = workbook.Sheets("Sheet1")
                  Dim colorindex As Integer
            colorindex = sheet.Cells(1, 1).Interior.Color
             MsgBox(colorindex)
            Return colorindex
    End Function

在default.aspx页面中:

 <div style ="height:600px; width:100%; overflow:auto;">
        <asp:GridView HeaderStyle-ForeColor="White" CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"  AutoGenerateColumns ="True"  Width="100%" ID="gvEmployees" runat="server" AllowPaging="False" AllowSorting="True" DataKeyNames="Id" OnRowDataBound="colorchange">
            </asp:GridView>     

我想读取所有单元格颜色并在数据网格视图中显示,但此代码不显示任何内容。

0 个答案:

没有答案