微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/vb/263309.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐