在for循环中计数条目,然后使用整数来格式化Excel电子表格vb.net

我目前正在从VB.NET中的数据表导出到Excel表。 我想格式化单元格,我正在尝试使用基于整数的variables来select每次下载所使用的最大行数。 问题是尝试将for循环运行的次数或数据表的行传递到代码的格式部分。 代码示例:

Dim EffMaxCellX As Integer Dim EffmaxcellY As Integer Dim JobsMaxCellX As Integer Dim JobsMaxCellY As Integer For I = 0 To dtEff.Rows.Count - 1 For J = 0 To dtEff.Columns.Count - 1 xlworksheet.Cells(1, J + 1) = dtEff.Columns(J).ToString xlworksheet.Cells(I + 2, J + 1) = dtEff.Rows(I).Item(J).ToString EffMaxCellX = I EffmaxcellY = J Next Next For I = 0 To dtJobs.Rows.Count - 1 For J = 0 To dtJobs.Columns.Count - 1 xlworksheet.Cells(1, J + 8) = dtJobs.Columns(J).ToString xlworksheet.Cells(I + 2, J + 8) = dtJobs.Rows(I).Item(J).ToString JobsMaxCellX = I JobsMaxCellY = J Next Next '--------------Format excel sheet---------- Dim formatRange As excel.Range formatRange = xlworksheet.Range("a1") formatRange.EntireRow.Font.Bold = True formatRange.EntireRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow) Dim FormatRangeBorderEff As excel.Range FormatRangeBorderEff = xlworksheet.Range("A1", "F" & EffMaxCellX.ToString & "") FormatRangeBorderEff.BorderAround(excel.XlLineStyle.xlContinuous, excel.XlBorderWeight.xlMedium, excel.XlColorIndex.xlColorIndexAutomatic, excel.XlColorIndex.xlColorIndexAutomatic) Dim FormatRangeBorderJobs As excel.Range FormatRangeBorderJobs = xlworksheet.Range("H1", "J" & JobsMaxCellX.ToString & "") FormatRangeBorderJobs.BorderAround(excel.XlLineStyle.xlContinuous, excel.XlBorderWeight.xlMedium, excel.XlColorIndex.xlColorIndexAutomatic, excel.XlColorIndex.xlColorIndexAutomatic) 

在上面的代码中,我循环了两个数据表,然后使用整数,并试图将它们构build到Excel格式代码中。 我可以这样做,还是我需要find另一种方法? 代码错误

  FormatRangeBorderEff = xlworksheet.Range("A0", "F" & EffMaxCellX.ToString & "") 

好吧,我会尝试解决这个代码

 Dim EffMaxCellX, EffmaxcellY, JobsMaxCellX, JobsMaxCellY As Integer For i = 0 To dtEff.Rows.Count - 1 For j = 0 To dtEff.Columns.Count - 1 xlworksheet.Cells(1, j + 1) = dtEff.Columns(j).ToString xlworksheet.Cells(i + 2, j + 1) = dtEff.Rows(i).Item(j).ToString EffMaxCellX = i EffmaxcellY = j Next Next For x = 0 To dtJobs.Rows.Count - 1 For z = 0 To dtJobs.Columns.Count - 1 xlworksheet.Cells(1, z + 8) = dtJobs.Columns(z).ToString xlworksheet.Cells(x + 2, z + 8) = dtJobs.Rows(x).Item(z).ToString JobsMaxCellX = x JobsMaxCellY = z Next Next '--------------Format excel sheet---------- Dim FormatRangeBorderJobs, formatRange, FormatRangeBorderEff As excel.Range formatRange = xlworksheet.Range("a1") formatRange.EntireRow.Font.Bold = True formatRange.EntireRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow) FormatRangeBorderEff = xlworksheet.Range("A1", String.Format("F{0}", EffMaxCellX.ToString) FormatRangeBorderEff.BorderAround(excel.XlLineStyle.xlContinuous, excel.XlBorderWeight.xlMedium, excel.XlColorIndex.xlColorIndexAutomatic, excel.XlColorIndex.xlColorIndexAutomatic) FormatRangeBorderJobs = xlworksheet.Range("H1", String.Format("J{0}", JobsMaxCellX.ToString) FormatRangeBorderJobs.BorderAround(excel.XlLineStyle.xlContinuous, excel.XlBorderWeight.xlMedium, excel.XlColorIndex.xlColorIndexAutomatic, excel.XlColorIndex.xlColorIndexAutomatic) 

清洁版本:

 Dim EffMaxCellX, EffmaxcellY, JobsMaxCellX, JobsMaxCellY As Integer For i = 0 To dtEff.Rows.Count - 1 For j = 0 To dtEff.Columns.Count - 1 xlworksheet.Cells(1, j + 1) = dtEff.Columns(j).ToString xlworksheet.Cells(i + 2, j + 1) = dtEff.Rows(i).Item(j).ToString Next Next For x = 0 To dtJobs.Rows.Count - 1 For z = 0 To dtJobs.Columns.Count - 1 xlworksheet.Cells(1, z + 8) = dtJobs.Columns(z).ToString xlworksheet.Cells(x + 2, z + 8) = tJobs.Rows(x).Item(z).ToString Next Next EffMaxCellX = dtEff.Rows.Count - 1 EffmaxcellY = dtEff.Columns.Count - 1 JobsMaxCellX = dtJobs.Rows.Count -1 JobsMaxCellY = dtJobs.Columns.Count -1 

“我相信你在上面的代码中犯了一个错误,但是你所拥有的代码没有什么不同

  '--------------Format excel sheet---------- Dim formatRange As excel.Range formatRange = xlworksheet.Range("a1") formatRange.EntireRow.Font.Bold = True formatRange.EntireRow.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow) GetBorder("A1", String.Format("F{0}", EffMaxCellX.ToString) GetBorder("H1", String.Format("J{0}", JobsMaxCellX.ToString) Sub GetBorder(ByVal startCell as String, ByVal endCell as String) Dim FormatRangeBorderEff As excel.Range FormatRangeBorderEff = xlworksheet.Range(startCell, endCell) FormatRangeBorderEff.BorderAround(excel.XlLineStyle.xlContinuous, excel.XlBorderWeight.xlMedium, excel.XlColorIndex.xlColorIndexAutomatic, excel.XlColorIndex.xlColorIndexAutomatic) End Sub