格式化某些行,其中单元值从vb.net(datagridview)导出到Excel时符合条件

发布于 2025-01-21 11:10:59 字数 2899 浏览 0 评论 0原文

我是VB语法的新手,我正在为Excel导出添加条件而努力。 该数据集填充数据集(DGVBreaks)。数据集中的数据将看起来像这样:

emp_nameclockdatetime_intime_out time_out time_outtirper_hours total_hourstotal_hours there_hours natenatenate
我有一个数据集(DS2) ,nate nate nate natethe06:42:005,84,602在:2022-04-05-
Moloto,Joseph04/05/2022 00:0007:22:00 07:22:0007:22:009,29,001 1
莫洛托,约瑟夫04/06/2022 00:0007:40:0016:31:008,9 8,90MOLOTO
,JOSEPH,JOSEPH04/07/2022 00:00:0007:25:0016:29:008,80 ,19,000
MOLOTO,JOSEPH04/08/2022 00:0007:34:0015:35:008,0 8,08,900
MOLOTO,JOSEPH,JOSEPH04/11/2022 00:0007:42:0016:33:008,98,800

我想将数据从GridView或数据集导出到基于单元格值的excel和格式化某些行。 小于

实际如果例如小时8
06:42:005,84,602在:2022-04-05上时钟 -

目前,我使用以下代码将数据导出到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)

但是我不确定如何添加条件检查。 (我如何仅检查“实际小时”的设定?)

I am new to VB syntax, and im struggling with adding conditions to an excel export.
I have a dataset (ds2) that populates a DataGridView (dgvBreaks).The data in the dataset will look something like this:

Emp_NameClockDateTime_InTime_OutTotal_HoursActual_HoursBreaksNotes
Moloto, Joseph04/04/2022 00:0007:36:0006:42:005,84,602Clocked out on: 2022-04-05 -
Moloto, Joseph04/05/2022 00:0007:22:0007:22:009,29,001
Moloto, Joseph04/06/2022 00:0007:40:0016:31:008,98,800
Moloto, Joseph04/07/2022 00:0007:25:0016:29:009,19,000
Moloto, Joseph04/08/2022 00:0007:34:0015:35:008,08,900
Moloto, Joseph04/11/2022 00:0007:42:0016:33:008,98,800

I want to export the data from the gridview or dataset to excel and format certain rows based on a cell value. FOR EXAMPLE: Highlight a row in red if the Actual hours is less than 8. so that this row would be in red:

Emp_NameClockDateTime_InTime_OutTotal_HoursActual_HoursBreaksNotes
Moloto, Joseph04/04/2022 00:0007:36:0006:42:005,84,602Clocked out on: 2022-04-05 -

Currently I export the data to an excel sheet using the code below:

    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?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2025-01-28 11:11:00

正如Johng在评论中所说的那样,这很简单:

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

As JohnG stated in the comments, it is quite simple:

xlWorkSheet.Range(xlWorkSheet.Cells(i + 2, 1), xlWorkSheet.Cells(i + 2, 8)).Interior.Color = ColorTranslator.ToOle(Color.Red)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文