格式化某些行,其中单元值从vb.net(datagridview)导出到Excel时符合条件
我是VB语法的新手,我正在为Excel导出添加条件而努力。 该数据集填充数据集(DGVBreaks)。数据集中的数据将看起来像这样:
emp_name | clockdate | time_in | time_out time_out time_out | tirper_hours total_hours | total_hours there_hours nate | nate | nate |
---|---|---|---|---|---|---|---|
我有一个数据集(DS2) , | nate nate nate nate | the | 06:42:00 | 5,8 | 4,60 | 2 | 在:2022-04-05- |
Moloto,Joseph | 04/05/2022 00:00 | 07:22:00 07:22:00 | 07:22:00 | 9,2 | 9,00 | 1 1 | |
莫洛托,约瑟夫 | 04/06/2022 00:00 | 07:40:00 | 16:31:00 | 8,9 8,9 | 0 | MOLOTO | |
,JOSEPH,JOSEPH | 04/07/2022 00:00:00 | 07:25:00 | 16:29:00 | 8,80 ,1 | 9,00 | 0 | |
MOLOTO,JOSEPH | 04/08/2022 00:00 | 07:34:00 | 15:35:00 | 8,0 8,0 | 8,90 | 0 | |
MOLOTO,JOSEPH,JOSEPH | 04/11/2022 00:00 | 07:42:00 | 16:33:00 | 8,9 | 8,80 | 0 |
我想将数据从GridView或数据集导出到基于单元格值的excel和格式化某些行。 小于
实际 | 如果 | : | 例如 | 小时 | 8 | 。 | |
---|---|---|---|---|---|---|---|
| | | 06:42:00 | 5,8 | 4,60 | 2 | 在: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_Name | ClockDate | Time_In | Time_Out | Total_Hours | Actual_Hours | Breaks | Notes |
---|---|---|---|---|---|---|---|
Moloto, Joseph | 04/04/2022 00:00 | 07:36:00 | 06:42:00 | 5,8 | 4,60 | 2 | Clocked out on: 2022-04-05 - |
Moloto, Joseph | 04/05/2022 00:00 | 07:22:00 | 07:22:00 | 9,2 | 9,00 | 1 | |
Moloto, Joseph | 04/06/2022 00:00 | 07:40:00 | 16:31:00 | 8,9 | 8,80 | 0 | |
Moloto, Joseph | 04/07/2022 00:00 | 07:25:00 | 16:29:00 | 9,1 | 9,00 | 0 | |
Moloto, Joseph | 04/08/2022 00:00 | 07:34:00 | 15:35:00 | 8,0 | 8,90 | 0 | |
Moloto, Joseph | 04/11/2022 00:00 | 07:42:00 | 16:33:00 | 8,9 | 8,80 | 0 |
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_Name | ClockDate | Time_In | Time_Out | Total_Hours | Actual_Hours | Breaks | Notes |
---|---|---|---|---|---|---|---|
Moloto, Joseph | 04/04/2022 00:00 | 07:36:00 | 06:42:00 | 5,8 | 4,60 | 2 | Clocked 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如Johng在评论中所说的那样,这很简单:
As JohnG stated in the comments, it is quite simple: