【发布时间】:2013-05-08 07:58:25
【问题描述】:
以前,我试图将 gridview 值导出到 excel 中。但使用下面给出的代码,我可以将数据导出到 excel 中。但仍然无法自动保存该 excel 文件到 固定文件夹(假设位于 C:/drive 中)。下面给出了我编写的导出到 excel 的代码。
Private Sub ButtonExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExport.Click
Dim rowsTotal, colsTotal As Short
Dim I, j, iC As Short
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Dim xlApp As New Excel.Application
Try
Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
xlApp.Visible = True
rowsTotal = DataGridView1.RowCount - 1
colsTotal = DataGridView1.Columns.Count - 1
With excelWorksheet
.Cells.Select()
.Cells.Delete()
For iC = 0 To colsTotal
.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
Next
For I = 0 To rowsTotal - 1
For j = 0 To colsTotal
.Cells(I + 2, j + 1).value = DataGrid1.Rows(I).Cells(j).Value
Next j
Next I
.Rows("1:1").Font.FontStyle = "Bold"
.Rows("1:1").Font.Size = 10
.Cells.Columns.AutoFit()
.Cells.Select()
.Cells.EntireColumn.AutoFit()
.Cells(1, 1).Select()
End With
Catch ex As Exception
MsgBox("Export Excel Error " & ex.Message)
Finally
'RELEASE ALLOACTED RESOURCES
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
xlApp = Nothing
End Try
End Sub
谁能帮我解决这个问题,如何在VB.NET中自动保存那个excel文件??
【问题讨论】:
-
你有没有尝试保存它?检查here
-
是的,我可以通过另存为 excel 选项将其保存在外部。但我想在完成导出后自动保存。
-
是的,请查看我第一条评论中的链接。
-
我没有收到该代码。在哪里写? Me.FileFormat、Me.SaveAs 和 Me.Path 显示错误
标签: vb.net excel gridview directory