简体   繁体   English

从 VB.NET (DataGridView) 导出到 Excel 时格式化单元格值满足条件的某些行

[英]Format certain rows where a cell value meets a condition when exporting from VB.NET (DataGridView) to Excel

I am new to VB syntax, and im struggling with adding conditions to an excel export.我是 VB 语法的新手,我正在努力为 excel 导出添加条件。 I have a dataset (ds2) that populates a DataGridView (dgvBreaks).The data in the dataset will look something like this:我有一个填充 DataGridView (dgvBreaks) 的数据集 (ds2)。数据集中的数据将如下所示:

Emp_Name企业名称 ClockDate时钟日期 Time_In时间_输入 Time_Out暂停 Total_Hours全部小时数 Actual_Hours实际小时数 Breaks休息时间 Notes笔记
Moloto, Joseph莫洛托,约瑟夫 04/04/2022 00:00 04/04/2022 00:00 07:36:00 07:36:00 06:42:00 06:42:00 5,8 5,8 4,60 4,60 2 2个 Clocked out on: 2022-04-05 -下班时间:2022-04-05 -
Moloto, Joseph莫洛托,约瑟夫 04/05/2022 00:00 04/05/2022 00:00 07:22:00 07:22:00 07:22:00 07:22:00 9,2 9,2 9,00 9,00 1 1个
Moloto, Joseph莫洛托,约瑟夫 04/06/2022 00:00 04/06/2022 00:00 07:40:00 07:40:00 16:31:00 16:31:00 8,9 8,9 8,80 8,80 0 0
Moloto, Joseph莫洛托,约瑟夫 04/07/2022 00:00 04/07/2022 00:00 07:25:00 07:25:00 16:29:00 16:29:00 9,1 9,1 9,00 9,00 0 0
Moloto, Joseph莫洛托,约瑟夫 04/08/2022 00:00 04/08/2022 00:00 07:34:00 07:34:00 15:35:00 15:35:00 8,0 8,0 8,90 8,90 0 0
Moloto, Joseph莫洛托,约瑟夫 04/11/2022 00:00 04/11/2022 00:00 07:42:00 07:42:00 16:33:00 16:33:00 8,9 8,9 8,80 8,80 0 0

I want to export the data from the gridview or dataset to excel and format certain rows based on a cell value.我想将数据从 gridview 或数据集导出到 excel 并根据单元格值格式化某些行。 FOR EXAMPLE: Highlight a row in red if the Actual hours is less than 8. so that this row would be in red:例如:如果实际小时数小于 8,则以红色突出显示一行。这样该行将显示为红色:

Emp_Name企业名称 ClockDate时钟日期 Time_In时间_输入 Time_Out暂停 Total_Hours全部小时数 Actual_Hours实际小时数 Breaks休息时间 Notes笔记
Moloto, Joseph莫洛托,约瑟夫 04/04/2022 00:00 04/04/2022 00:00 07:36:00 07:36:00 06:42:00 06:42:00 5,8 5,8 4,60 4,60 2 2个 Clocked out on: 2022-04-05 -下班时间:2022-04-05 -

Currently I export the data to an excel sheet using the code below:目前我使用以下代码将数据导出到 excel 工作表:

    Dim xlApp As Microsoft.Office.Interop.Excel.Application
    Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
    Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value
    Dim i As Integer
    Dim j As Integer
    xlApp = New Microsoft.Office.Interop.Excel.Application
    xlWorkBook = xlApp.Workbooks.Add(misValue)
    xlWorkSheet = xlWorkBook.Sheets("sheet1")
    For i = 0 To dgvDataBreaks.RowCount - 2
        For j = 0 To dgvDataBreaks.ColumnCount - 1
            For k As Integer = 1 To dgvDataBreaks.Columns.Count
                xlWorkSheet.Cells(1, k) = dgvDataBreaks.Columns(k - 1).HeaderText
                xlWorkSheet.Cells(i + 2, j + 1) = dgvDataBreaks(j, i).Value.ToString()
            Next
        Next
    Next
    Dim path As SaveFileDialog = New SaveFileDialog
    path.ShowDialog()
    If DialogResult.OK Then
        xlWorkSheet.SaveAs(path.FileName)
    End If
    xlWorkBook.Close()
    xlApp.Quit()
    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)
    MsgBox("You can find the file at: " + path.FileName)

But I am not sure how to add the conditional check.但我不确定如何添加条件检查。 (How would I check only the 'Actual Hours' for instace?) (我如何只检查实例的“实际时间”?)

As JohnG stated in the comments, it is quite simple:正如 JohnG 在评论中所说,这很简单:

xlWorkSheet.Range(xlWorkSheet.Cells(i + 2, 1), xlWorkSheet.Cells(i + 2, 8)).Interior.Color = ColorTranslator.ToOle(Color.Red)

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

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