VB.NET 中生成Excel文件并弹出对话框保存

前端之家收集整理的这篇文章主要介绍了VB.NET 中生成Excel文件并弹出对话框保存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

''' <summary>
''' Function:To Generate the report.
''' </summary>
''' <remarks></remarks>
Private Sub GenerateReport()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim oRng As Excel.Range

oXL = CreateObject("Excel.Application")
oXL.Visible = False

'Create New Workbook and Set Active Sheet
oWB = oXL.Workbooks.Add
oSheet = oWB.ActiveSheet

With oSheet.Range("B3","BG3")
.Font.Bold = True
.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
.Borders(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlContinuous
.Cells.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic)
.EntireRow.RowHeight = 20
.EntireColumn.ColumnWidth = 20
.EntireColumn.AutoFit()
End With

Dim SKACollection As String

For i As Integer = 0 To listBoxSelectedItem.Items.Count - 1
If i = 0 Then
SKACollection = listBoxSelectedItem.Items(i).Text
Else
SKACollection = SKACollection + "," + listBoxSelectedItem.Items(i).Text
End If
Next

‘Get the data

Dim dtGenerateReport As New DataTable
Dim bf As New COPESSGTPStaticDataBF

bf.GenerateReport(dtGenerateReport,SKACollection)

If dtGenerateReport.Rows.Count = 0 Then
PromptUserAlert("No Data","alert('There have no data about the selected SKAcc number.')")
Exit Sub
End If
Dim fldCount As Integer
Dim iCol As Integer

fldCount = dtGenerateReport.Columns.Count

For iCol = 2 To fldCount + 1
oSheet.Cells._Default(3,iCol).Value = dtGenerateReport.Columns(iCol - 2).ColumnName
Next
Dim ds As New DataSet
ds.Tables.Add(dtGenerateReport)

For i As Integer = 0 To dtGenerateReport.Rows.Count - 1
For iCol = 2 To fldCount + 1
oSheet.Cells._Default(4 + i,iCol).Value() = dtGenerateReport.Rows(i)(iCol - 2).ToString
Next
Next


oXL.Selection.CurrentRegion.Columns.AutoFit()
oXL.Selection.CurrentRegion.Rows.AutoFit()
'Set data formatting
With oSheet.UsedRange
.Borders(Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlContinuous
.Borders(Excel.XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlContinuous
.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlColorIndex.xlColorIndexAutomatic)
.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter
.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft
.EntireColumn.AutoFit()
End With

' Set Excel Invisible
oXL.Visible = False
Dim strFileName As String
strFileName = "../TPStaticDataResult.xls"
oXL.Dialogs(Excel.XlBuiltInDialog.xlDialogSaveAs).Show(strFileName)
oXL.DisplayAlerts = False

Call oWB.Close(False)
oXL.Quit()

' Release object references.
oRng = Nothing
oSheet = Nothing
oWB = Nothing
oXL = Nothing

End Sub

原文链接:/vb/263309.html

猜你在找的VB相关文章