如何将条件置于循环中? [英] How can put a condition in loop?
本文介绍了如何将条件置于循环中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新手,也是自学成才
我打算将我的数据从我的datagridview导出到excel使用循环,但我想要的是什么时候循环到达设置条件,其他数据应放入excel中的另一行单元格
这里是我将数据导出到excel中的示例
I am a newbie and a self taught anyway
I am planning to export my data from my datagridview into excel using loop but what i want is when when the loop reach the set condition the other data should put into another row of cells in excel
here is the example when i export my data into excel
|___A__|___B_____|____C___|__D____|___E___|_________F___________|
_
1 I want to go out inside my house
_
2 He want to go out From his School
_
3
_
4
_
但我想要的就是这样在我的excel中
But what I want is like this in my excel
|___A ____|___B___|___C___|___D_____________|____E____|__F__|
_
1 I want
_
2 He want
_
3
_
4 to go out inside my house
_
5 to go out From his School
_
这是我的代码你能不能请帮助我。
here is my code Can you please help me.
Private Sub ExportingEdited()
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
Exit Sub
End If
Dim columnCollection As DataGridViewColumnCollection = DataGridView1.Columns
Dim currentVisibleColumn As DataGridViewColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
Dim lastColumnExported As DataGridViewColumn = currentVisibleColumn
Dim visibleColumnCount As Integer = columnCollection.GetColumnCount(DataGridViewElementStates.Visible)
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
xlApp = New Microsoft.Office.Interop.Excel.Application With {.Visible = True}
xlWorkBook = xlApp.Workbooks.Add(xlPath)
xlWorkSheet = CType(xlWorkBook.Sheets("sheet1"), Microsoft.Office.Interop.Excel.Worksheet)
For r = 0 To DataGridView1.Rows.Count - 1
'Reset values'
currentVisibleColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
lastColumnExported = currentVisibleColumn
For c = 2 To visibleColumnCount + 1
Dim value = DataGridView1.Rows(r).Cells(currentVisibleColumn.Index).Value
If value IsNot vbNullString Then
xlWorkSheet.Cells(r + 58, c) = value.ToString()
End If
currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
lastColumnExported = currentVisibleColumn
Next
Next
xlWorkSheet.SaveAs(CType(xlPath, String))
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Dim p As ProcessStartInfo = New ProcessStartInfo(CType(xlPath, String))
Process.Start(p)
End Sub
我尝试了什么:
我试过这个
What I have tried:
I tried this
Private Sub ExportingEditedspecificrow()
If ((DataGridView1.Columns.Count = 0) Or (DataGridView1.Rows.Count = 0)) Then
Exit Sub
End If
Dim columnCollection As DataGridViewColumnCollection = DataGridView1.Columns
Dim currentVisibleColumn As DataGridViewColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
Dim lastColumnExported As DataGridViewColumn = currentVisibleColumn
Dim visibleColumnCount As Integer = columnCollection.GetColumnCount(DataGridViewElementStates.Visible)
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
xlApp = New Microsoft.Office.Interop.Excel.Application With {.Visible = True}
xlWorkBook = xlApp.Workbooks.Add(xlPath)
xlWorkSheet = CType(xlWorkBook.Sheets("sheet2"), Microsoft.Office.Interop.Excel.Worksheet)
For r = 0 To DataGridView1.Rows.Count - 1
'Reset values'
currentVisibleColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
lastColumnExported = currentVisibleColumn
For c = 2 To visibleColumnCount + 1
Dim value = DataGridView1.Rows(r).Cells(currentVisibleColumn.Index).Value
If value IsNot vbNullString Then
xlWorkSheet.Cells(r + 58, c) = value.ToString()
If c = 7 Then
For a = 0 To DataGridView1.Rows.Count - 1
'Reset values'
currentVisibleColumn = columnCollection.GetFirstColumn(DataGridViewElementStates.Visible)
lastColumnExported = currentVisibleColumn
For b = 2 To visibleColumnCount + 1
value = DataGridView1.Rows(a).Cells(currentVisibleColumn.Index).Value
If value IsNot vbNullString Then
xlWorkSheet.Cells(a + 67, b) = value.ToString()
End If
Next
Next
End If
End If
currentVisibleColumn = columnCollection.GetNextColumn(lastColumnExported, DataGridViewElementStates.Visible, DataGridViewElementStates.None)
lastColumnExported = currentVisibleColumn
Next
Next
xlWorkSheet.SaveAs(CType(xlPath, String))
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
Dim p As ProcessStartInfo = New ProcessStartInfo(CType(xlPath, String))
Process.Start(p)
End Sub
但结果是这样的结果
but what happen is the result became like this
|___A__|___B_____|____C___|__D____|___E___|_________F___________|
_
1 I want to go out inside my house
_
2 He want to go out From his School
_
3
_
4 I I I I I I
_
5 I I I I I I
_
推荐答案
肯定会更好地将源数据转换为正确的格式,然后所有代码都需要将数据输出到Excel中。在事实结束之后弄乱了数据并且容易出错。
Surely it would be better to get the source data into the correct format, then all your code needs to do it output the data into Excel. Messing around with the data after the fact is over complicating things and error prone.
这篇关于如何将条件置于循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文