加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 23:35:24 所属栏目:大数据 来源:网络整理
导读:''' 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(

''' <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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读