如何使用用户输入的文件名导出Excel文件?

时间:2019-11-13 08:30:20

标签: excel vb.net

如何通过让用户选择其目标文件和文件名来从datagridview导出excel文件。

这是我在下面尝试过的内容:

 Dim xlWorkBook As Excel.Workbook
 Dim xlWorkSheet As Excel.Worksheet
 Dim misValue As Object = System.Reflection.Missing.Value

 Dim i As Int16, j As Int16

 Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
 xlWorkBook = xlApp.Workbooks.Add(misValue)
 xlWorkSheet = xlWorkBook.Sheets("sheet1")

 xlWorkSheet.Cells(1, 1) = "DATE HANDLED"
 xlWorkSheet.Cells(1, 2) = "ROUTE"
 xlWorkSheet.Cells(1, 3) = "ITIN NO"
 xlWorkSheet.Cells(1, 4) = "METER NUMBER"
 xlWorkSheet.Cells(1, 5) = "L.I.N"
 xlWorkSheet.Cells(1, 6) = "ADDRESS"
 xlWorkSheet.Cells(1, 7) = "FF DESCRIPTION AND REMARKS"
 xlWorkSheet.Cells(1, 8) = "RDG"

 For i = 0 To dt_data.RowCount - 2
     For j = 0 To dt_data.ColumnCount - 1
         xlWorkSheet.Cells(i + 3, j + 1) = dt_data(j, i).Value.ToString()
     Next
 Next

 xlWorkBook.SaveAs("f:\vb.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
  Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
 xlWorkBook.Close(True, misValue, misValue)
 xlApp.Quit()

 releaseObject(xlWorkSheet)
 releaseObject(xlWorkBook)
 releaseObject(xlApp)

 MessageBox.Show("Saved")

这是releaseObject类的

    Private Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
                obj = Nothing
            Catch ex As Exception
                obj = Nothing
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString())
            Finally
                GC.Collect()
            End Try
        End Sub

1 个答案:

答案 0 :(得分:0)

可能会帮助您:

Sub SaveCSV()
        Try
            Using saveFileDialog1 As New SaveFileDialog()
                saveFileDialog1.InitialDirectory = "C:\Documents"
                saveFileDialog1.RestoreDirectory = True
                saveFileDialog1.Filter = "Excel|*.*" ' change here for csv or xls
                saveFileDialog1.FileName = "yourDefaultfileName.xls"
                If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

                If saveFileDialog1.FileName.Length = 0 Then

                Else
                    Dim xlsPath As String = System.IO.Path.GetFullPath(saveFileDialog1.FileName)
                    Try

                        Dim xlWorkBook As Excel.Workbook
                        Dim xlWorkSheet As Excel.Worksheet
                        Dim misValue As Object = System.Reflection.Missing.Value

                        Dim i As Int16, j As Int16

                        Dim xlApp As Excel.Application = New Microsoft.Office.Interop.Excel.Application()
                        xlWorkBook = xlApp.Workbooks.Add(misValue)
                        xlWorkSheet = xlWorkBook.Sheets("sheet1")

                        xlWorkSheet.Cells(1, 1) = "DATE HANDLED"
                        xlWorkSheet.Cells(1, 2) = "ROUTE"
                        xlWorkSheet.Cells(1, 3) = "ITIN NO"
                        xlWorkSheet.Cells(1, 4) = "METER NUMBER"
                        xlWorkSheet.Cells(1, 5) = "L.I.N"
                        xlWorkSheet.Cells(1, 6) = "ADDRESS"
                        xlWorkSheet.Cells(1, 7) = "FF DESCRIPTION AND REMARKS"
                        xlWorkSheet.Cells(1, 8) = "RDG"



                        For i = 0 To dt_data.RowCount - 2
                            For j = 0 To dt_data.ColumnCount - 1
                                xlWorkSheet.Cells(i + 3, j + 1) = dt_data(j, i).Value.ToString()
                            Next
                        Next

                        xlWorkBook.SaveAs(xlsPath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
                         Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
                        xlWorkBook.Close(True, misValue, misValue)
                        xlApp.Quit()

                        releaseObject(xlWorkSheet)
                        releaseObject(xlWorkBook)
                        releaseObject(xlApp)

                        MessageBox.Show("Saved in " & xlsPath)


                    Catch
                        MsgBox("Not a valid image file.")
                    End Try
                End If
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.ToString & vbCrLf & ex.Message.ToString & vbCrLf & ex.StackTrace.ToString)
        End Try
    End Sub