使用SavefileDialog函数时文件保存不完美 [英] File is not saved perfectly while using SavefileDialog function
问题描述
全部,
我正在VS 2010中开发一个VB应用程序.在这里,我正在使用SaveFileDialog函数生成报告,以将Access记录导出到Excel工作表.
虽然记录不能完美保存.在这里,我提供了用于获取访问记录并将其保存到Excel文件的代码..
Hai All,
I am Developing one VB Application in VS 2010. And here i am generating report by using SaveFileDialog function for export Access records to Excel sheet.
While the records are not save perfectly. Here i provide my code for get the access records and save the records to excel file..
Try
If ((DGView.Columns.Count = 0) Or (DGView.Rows.Count = 0)) Then
Exit Sub
End If
''Creating dataset to export
Dim dset As New DataSet
''add table to dataset
dset.Tables.Add()
''add column to that table
For i As Integer = 0 To DGView.ColumnCount - 1
dset.Tables(0).Columns.Add(DGView.Columns(i).HeaderText)
Next
''add rows to the table
Dim dr1 As DataRow
For i As Integer = 0 To DGView.RowCount - 1
dr1 = dset.Tables(0).NewRow
For j As Integer = 0 To DGView.Columns.Count - 1
dr1(j) = DGView.Rows(i).Cells(j).Value
Next
dset.Tables(0).Rows.Add(dr1)
Next
Dim excel As New Microsoft.Office.Interop.Excel.Application
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
Dim myStream As System.IO.FileStream
''Dim saveFileDialog1 As New SaveFileDialog()
SaveFileDialog1.Filter = "Excel files (*.xls)|*.xls|All files(*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = SaveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
'' Code to write the stream goes here.
wBook.Save()
excel.Workbooks.Open(SaveFileDialog1.FileName)
myStream.Close()
End If
End If
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
任何人都请帮助我.........
Any body please help mee........
推荐答案
我们不知道您所说的完美保存"是什么意思.另外,您使用SaveFileDialog的唯一目的是获取要保存到的文件路径.您正在让它打开文件(不必要!),然后对返回的流对象不执行任何操作,除了关闭它.
We have no idea what you mean by "save perfectly". Also, the only thing you''re using the SaveFileDialog for is getting the filepath to save to . You''re having it open a file (unnecessary!) and then doing nothing with the stream object that is returned, other than closing it.
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
wBook.Save()
excel.Workbooks.Open(SaveFileDialog1.FileName)
myStream.Close()
End If
FileStream
的目的是什么?为什么要打开Excel工作簿而不是保存它?您是要使用SaveAs()
吗?
What is the purpose of the FileStream
here, and why are you opening the Excel workbook rather than saving it; did you mean to use SaveAs()
?
这篇关于使用SavefileDialog函数时文件保存不完美的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!