以前,我試圖將gridview值導出到excel中。但與下面給出的代碼我能夠導出數據到Excel中。但還是不能自動保存那個excel文件變成固定文件夾假設在C:/驅動器。下面給出我寫出來導入excel的代碼。如何在vb.net中保存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文件?
你甚至嘗試保存它嗎?檢查[這裏](http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.saveas(v = vs.80).aspx) – 2013-05-14 06:23:14
是的,我可以通過外部保存保存爲excel的選項。但是我想在完成導出後自動保存它。 – SoumitaP 2013-05-14 06:25:46
是的,請檢查我的第一條評論的鏈接。 – 2013-05-14 06:26:35