使用VB.net更改Excel文件的单元格颜色

时间:2015-09-02 11:18:36

标签: vb.net excel

我搜索了很多并尝试了许多不同的代码来实现这一点,但目前还没有成功的迹象。这是我的代码:

Imports Excel= Microsoft.Office.Interop.Excel
Public Class Form1
    Dim APP As New Excel.Application
    Dim worksheet As Excel.Worksheet
    Dim workbook As Excel.Workbook
    Dim style As Excel.Style = worksheet.Application.ActiveWorkbook.Styles.Add("NewStyle")

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        If Val(MeterCount.Text) >= Val(S1.Text) Then
            DC1.BackColor = Color.LightGreen
            DN1.BackColor = Color.LightGreen
            S1.BackColor = Color.LightGreen
            T1.BackColor = Color.LightGreen
            style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
            worksheet.Cells(2, 1).Style = "NewStyle"
        Else
            DC1.BackColor = Color.Transparent
            DN1.BackColor = Color.Transparent
            S1.BackColor = Color.Transparent
            T1.BackColor = Color.Transparent
        End If
    End Sub

上面的代码返回错误:System.InvalidOperationException未处理   Message =创建表单时出错。有关详细信息,请参阅Exception.InnerException。错误是:对象引用未设置为对象的实例。

1 个答案:

答案 0 :(得分:0)

试试这个:

   <item android:id="@+id/shopping_cart"
        android:title="cart"
        android:icon="@drawable/ic_add_shopping_cart_white_24dp"
        app:showAsAction="always" />