保存Excel时需要提示

时间:2014-05-05 20:25:08

标签: .net vb.net excel interop

我正在尝试使用以下函数将两个数据集的结果excel。目前我可以将它保存到某个位置。我想在用户单击“导出”按钮并显示以下函数时显示提示。目前它保存到我的文档。

Imports System.Text
Imports Microsoft.Office.Interop.Excel

Public Class ExportToExcel

    Public Sub DataSetsToExcel(dataSets As List(Of DataSet), fileName As String)
        Dim xlApp As New Microsoft.Office.Interop.Excel.Application()
        Dim xlWorkbook As Workbook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
        Dim xlSheets As Sheets = Nothing
        Dim xlWorksheet As Worksheet = Nothing

        For Each dataSet As DataSet In dataSets
            Dim dataTable As System.Data.DataTable = dataSet.Tables(0)
            Dim rowNo As Integer = dataTable.Rows.Count
            Dim columnNo As Integer = dataTable.Columns.Count
            Dim colIndex As Integer = 0



            'Create Excel Sheets
            xlSheets = xlWorkbook.Sheets
            xlWorksheet = DirectCast(xlSheets.Add(xlSheets(1), Type.Missing, Type.Missing, Type.Missing), Worksheet)
            xlWorksheet.Name = dataSet.DataSetName

            'Generate Field Names
            For Each dataColumn As DataColumn In dataTable.Columns
                colIndex += 1
                xlApp.Cells(1, colIndex) = dataColumn.ColumnName
            Next

            Dim objData As Object(,) = New Object(rowNo - 1, columnNo - 1) {}

            'Convert DataSet to Cell Data
            For row As Integer = 0 To rowNo - 1
                For col As Integer = 0 To columnNo - 1
                    objData(row, col) = dataTable.Rows(row)(col)
                Next
            Next

            'Add the Data
            Dim range As Range = xlWorksheet.Range(xlApp.Cells(2, 1), xlApp.Cells(rowNo + 1, columnNo))
            range.Value2 = objData

            'Format Data Type of Columns 
            colIndex = 0
            For Each dataColumn As DataColumn In dataTable.Columns
                colIndex += 1
                Dim format As String = "@"
                Select Case dataColumn.DataType.Name
                    Case "Boolean"
                        Exit Select
                    Case "Byte"
                        Exit Select
                    Case "Char"
                        Exit Select
                    Case "DateTime"
                        format = "dd/mm/yyyy"
                        Exit Select
                    Case "Decimal"
                        format = "$* #,##0.00;[Red]-$* #,##0.00"
                        Exit Select
                    Case "Double"
                        Exit Select
                    Case "Int16"
                        format = "0"
                        Exit Select
                    Case "Int32"
                        format = "0"
                        Exit Select
                    Case "Int64"
                        format = "0"
                        Exit Select
                    Case "SByte"
                        Exit Select
                    Case "Single"
                        Exit Select
                    Case "TimeSpan"
                        Exit Select
                    Case "UInt16"
                        Exit Select
                    Case "UInt32"
                        Exit Select
                    Case "UInt64"
                        Exit Select
                    Case Else
                        'String
                        Exit Select
                End Select
                'Format the Column accodring to Data Type
                xlWorksheet.Range(xlApp.Cells(2, colIndex), xlApp.Cells(rowNo + 1, colIndex)).NumberFormat = format
            Next
        Next

        'Remove the Default Worksheet
        DirectCast(xlApp.ActiveWorkbook.Sheets(xlApp.ActiveWorkbook.Sheets.Count), Worksheet).Delete()


        'Save
        xlWorkbook.SaveAs(fileName, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, _
         XlSaveAsAccessMode.xlNoChange, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value)

        xlWorkbook.Close()
        xlApp.Quit()
        GC.Collect()
    End Sub

End Class

在导出按钮CLick

 Dim objExportExcel As New ExportToExcel
  objExportExcel.DataSetsToExcel(dataSets, TotalPAHComponentListDropDown.SelectedItem.Text)

如果有人知道解决方案,请帮助我,我一直在浪费很多时间。

2 个答案:

答案 0 :(得分:0)

如果您知道路径,则可以使用输入框:

iReply = MsgBox(Prompt:="Do you wish to run the 'update' Macro", _
        Buttons:=vbYesNoCancel, Title:="UPDATE MACRO")

OR

FileDialogs (or Directory):
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'get the number of the button chosen    
Dim FileChosen As Integer
FileChosen = fd.Show

If FileChosen <> -1 Then

'didn't choose anything (clicked on CANCEL)
MsgBox "You chose cancel"

Else

'display name and path of file chosen

 MsgBox fd.SelectedItems(1)

End If

(在http://www.wiseowl.co.uk/blog/s209/type-filedialog.htm找到)

答案 1 :(得分:0)

首先将SaveFileDialog放入表单然后编写一个函数,该函数将获取“导出到Excel”所需的所有参数

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim Location As String = Nothing
    SaveFileDialog1.Filter = "Csv files (*.csv)|"
    SaveFileDialog1.ShowDialog()
    Location = SaveFileDialog1.FileName
    If Location <> "" Then
          'YOUR EXPORT TO EXCEL FUNCTION
          Export_Data(Parameter1,Parameter2,Parameter3)
    End If
End Sub