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

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强制返回为文本 --------------------------...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办法, Format 或者FormatDateTime 竟然结果和...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace My ‘全局错误处理,新的解决方案直接...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用的爽呀,这篇文章写与2011年,看来我以前没...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选中的单元格进行处理 Dim m As Range, t...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integ...