简体   繁体   English

vb.net更新Excel Workbook时的条件格式

[英]conditional format while update Excel Workbook using vb.net

I have a Problem while updating my Excel Workbook with vb.net. 使用vb.net更新Excel工作簿时出现问题。

Basically I will create a timetable in Excel. 基本上,我将在Excel中创建一个时间表。 So at my first line in Excel I have on each cell a date. 因此,在Excel的第一行,我在每个单元格上都有一个日期。

This is my Excel 这是我的Excel

For Cells C2 to I2 I have a conditional format with a formula: 对于单元格C2I2我有一个带公式的条件格式:

=AND(C$1>=$A2;C$1<=$B2)

This works great. 这很好。 When I change the value of B2 using vb.net Microsoft.Office.Interop.Excel 当我使用vb.net更改B2的值时Microsoft.Office.Interop.Excel

    Dim ExcelFolder As String
    Dim selectedfile As String
    Dim excel As Application
    Dim workbook As Workbook
    Dim sheet As Worksheet

    ExcelFolder = "C:\temp"

    selectedfile = ExcelFolder & "\" & "test.xlsx"
    excel = New Application
    excel.Workbooks.Open(selectedfile)
    workbook = excel.ActiveWorkbook
    sheet = workbook.Worksheets(2)

    'change value on B2
    sheet.Cells(2, 2) = "07.10.2017"

    workbook.SaveAs("test.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, False, False, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
    workbook.Close()
    excel.Quit()
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel) : excel = Nothing
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook) : workbook = Nothing
    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet) : sheet = Nothing

It will change the value, but the background wouldn't change. 它将更改值,但背景不会更改。 I have to go manually to cell B2 and press ENTER again, after that the conditional format will work. 我必须手动进入单元格B2并再次按ENTER,之后条件格式才能工作。 I have tried to write a Date to the cell or change the cell format before and after inserting to Date, without success. 我试图将日期写入单元格或更改插入日期前后的单元格格式,但均未成功。 I also tried to add a conditional format with vb.net after inserting the new data, without any success. 插入新数据后,我还尝试使用vb.net添加条件格式,但未成功。 Have you any ideas? 你有什么想法吗?

Ensure that the EnableCalculation property on your Worksheet object is set to true: 确保您的工作表对象上的EnableCalculation属性设置为true:

sheet.EnableCalcuation = True
sheet.Cells(2, 2) = "07.10.2017"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM