Private Sub DataGr()
Dim l As Integer
Dim xlApp1 As Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
xlApp1 = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
xlBook = xlApp1.Workbooks.Add()
xlApp1.DisplayAlerts = False
xlBook.Worksheets.Add()
If DataSet3.Tables.Count > 3 Then
For n As Integer = 1 To DataSet3.Tables.Count - 3
xlBook.Worksheets.Add()
Next
End If
For l = 0 To DataSet3.Tables.Count - 1
Dim rawData(DataSet3.Tables(l).Rows.Count, DataSet3.Tables(l).Columns.Count - 1) As Object
Dim col As Integer = 0
Dim row As Integer = 0
For col = 0 To DataSet3.Tables(l).Columns.Count - 1
For row = 0 To DataSet3.Tables(l).Rows.Count - 1
rawData(row + 1, col) = DataSet3.Tables(l).Rows(row).ItemArray(col)
Next
Next
Dim finalColLetter As String = String.Empty
finalColLetter = ExcelColName(DataSet3.Tables(l).Columns.Count)
Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, DataSet3.Tables(l).Rows.Count + 1)
xlBook.Worksheets(l + 1).Range(excelRange, Type.Missing).Value2 = rawData
Next
xlApp1.Windows.Item(1).SplitRow = 1 'Закрепляем верхнюю строку.Для примера
xlApp1.Windows.Item(1).FreezePanes = True
xlBook.Worksheets(2).activate()
xlApp1.Windows.Item(1).SplitRow = 1
xlApp1.Windows.Item(1).FreezePanes = True
xlBook.Worksheets(3).activate()
xlApp1.Windows.Item(1).SplitRow = 1
xlApp1.Windows.Item(1).FreezePanes = True
xlBook.SaveAs(Application.StartupPath & "\Результат.xls")
xlBook.Close()
xlApp1.Quit()
End Sub
Public Function ExcelColName(ByVal Col As Integer) As String
If Col < 0 And Col > 256 Then
MsgBox("Invalid Argument", MsgBoxStyle.Critical)
Return Nothing
Exit Function
End If
Dim i As Int16
Dim r As Int16
Dim S As String
If Col <= 26 Then
S = Chr(Col + 64)
Else
r = Col Mod 26
i = System.Math.Floor(Col / 26)
If r = 0 Then
r = 26
i = i - 1
End If
S = Chr(i + 64) & Chr(r + 64)
End If
ExcelColName = S
End Function
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 24