[英]vb.net how to Save excel Workbook to Specific Folder using Save Dialog Box
i want to save my datagridview into excel but i found code in some web is no use specific folder , i want to Save excel Workbook to Specific Folder using Save Dialog Box. 我想将datagridview保存到excel,但是我发现某些Web中的代码没有使用特定文件夹,我想使用“保存”对话框将excel工作簿保存到特定文件夹。
and this my code but this no save with specific folder. 这是我的代码,但是没有保存到特定的文件夹。
Dim xlApp As Excel.Application
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
'xlApp = New Excel.ApplicationClass
xlApp = New Excel.Application()
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For c As Integer = 0 To dgv1.Columns.Count - 1
xlWorkSheet.Cells(1, c + 1).Value = dgv1.Columns(c).HeaderText
Next
For i = 0 To dgv1.RowCount - 2
For j = 0 To dgv1.ColumnCount - 1
xlWorkSheet.Cells(i + 2, j + 1) = dgv1(j, i).Value.ToString()
Next
Next
xlWorkBook.SaveAs("d:\kehadiran.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("Your File is successfully saved d:\kehadiran.xls")
If you want to let the user pick where they save the file, use the save file dialog. 如果要让用户选择他们保存文件的位置,请使用“保存文件”对话框。 Right now, you're passing a hard-coded path to Workbook.SaveAs().
现在,您正在将硬编码路径传递给Workbook.SaveAs()。 Use a SaveFileDialog to get a path, and replace the "d:\\kehadiran.xls"'s with "savePath":
使用SaveFileDialog获取路径,并将“ d:\\ kehadiran.xls”替换为“ savePath”:
Dim xlApp As Excel.Application
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
'xlApp = New Excel.ApplicationClass
xlApp = New Excel.Application()
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For c As Integer = 0 To dgv1.Columns.Count - 1
xlWorkSheet.Cells(1, c + 1).Value = dgv1.Columns(c).HeaderText
Next
For i = 0 To dgv1.RowCount - 2
For j = 0 To dgv1.ColumnCount - 1
xlWorkSheet.Cells(i + 2, j + 1) = dgv1(j, i).Value.ToString()
Next
Next
Dim savePath As String = Nothing
Using sd As New SaveFileDialog
With sd
.RestoreDirectory = True
.Filter = "Excel XLS Files(*.xls)|*.xls|Excel Macro Embedded Files(*.xlsm)|*.xlsm|Excel XLSX Files(*.xlsx)|*.xlsx"
.FilterIndex = 3
If .ShowDialog = DialogResult.OK Then
savePath = .FileName
End If
End With
End Using
If savePath IsNot Nothing AndAlso savePath.Trim <> "" Then
xlWorkBook.SaveAs(savePath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
End If
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
MessageBox.Show("Your File is successfully saved " & savePath)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.