将Datagridview导出到文本文件 [英] Export Datagridview to text file
问题描述
我有一个小程序可以帮助我将Excel工作表导入Datagridview,以便稍后将其导出到文本文件,但问题是文本文件被覆盖了。我希望每次导出datagridview的内容时,都会创建一个使用不同名称的新文件以避免覆盖它。我非常感谢他的帮助。我在一个按钮中使用以下代码将datagrid导出到文本文件:
如果DataGridView1.RowCount = 0那么
MessageBox.Show(datagridview is emptly)
Else
如果Directory.Exists(C:\Foldertxt)= False那么< br $>
Directory.CreateDirectory(C:\Foldertxt)
结束如果
Dim sFile As String =C:\Foldertxt \ file.txt
如果File.Exists(sFile)= True那么
My.Computer.FileSystem.DeleteFile(sFile,FileIO.UIOption.OnlyErrorDialogs,_
FileIO.RecycleOption.DeletePermanently,FileIO.UICancelOption.DoNothing)
结束如果
使用f作为新的IO.StreamWriter(sFile,True)
Dim col As String =
Dim row As String =
Dim i As Integer = 0
For each r作为DataGridViewRow In DataGridView1.Rows
For Each c As DataGridViewColumn In DataGridView1.Columns
row = row&'&Convert.ToString(r.Cells(c.HeaderText) ).Value)&'
下一页
如果我< DataGridView1.Rows.Count - 1然后row&= Environment.NewLine
下一页
f.WriteLine (行)
MessageBox.Show(文件创建)
结束使用
结束如果
我用Visual Studio 2013
非常感谢和抱歉我的英语
I have a small program that helps me to import excel sheets to a Datagridview to later export it to a text file but the problem is that the text file is overwritten. I would like that every time you export the contents of datagridview a new file is created with a different name to avoid overwriting it . I would be very grateful for his help. I use the following code in a button to export the datagrid to text file:
If DataGridView1.RowCount = 0 Then
MessageBox.Show("the datagridview is emptly")
Else
If Directory.Exists("C:\Foldertxt") = False Then
Directory.CreateDirectory("C:\Foldertxt")
End If
Dim sFile As String = "C:\Foldertxt\file.txt"
If File.Exists(sFile) = True Then
My.Computer.FileSystem.DeleteFile(sFile, FileIO.UIOption.OnlyErrorDialogs, _
FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
End If
Using f As New IO.StreamWriter(sFile, True)
Dim col As String = ""
Dim row As String = ""
Dim i As Integer = 0
For Each r As DataGridViewRow In DataGridView1.Rows
For Each c As DataGridViewColumn In DataGridView1.Columns
row = row & "'" & Convert.ToString(r.Cells(c.HeaderText).Value) & "' "
Next
If i < DataGridView1.Rows.Count - 1 Then row &= Environment.NewLine
Next
f.WriteLine(row)
MessageBox.Show("file created")
End Using
End If
I use Visual Studio 2013
Thank so much and sorry for my english
推荐答案
获取要保存到的目录中的文件计数。将此新数字加到文件名末尾。
get the file count in the directory you are saving to. add 1 to this count, then concantate this new number to the end of the file name.
解决方案:
solution:
If DataGridView1.RowCount = 0 Then
MessageBox.Show("the datagridview is emptly")
Else
If Directory.Exists("C:\Foldertxt") = False Then
Directory.CreateDirectory("C:\Foldertxt")
End If
Dim sFile As String = "C:\Foldertxt\file.txt"
Dim count As Integer = 1
Dim fileNameOnly As String = Path.GetFileNameWithoutExtension(sFile)
Dim extension As String = Path.GetExtension(sFile)
Dim path__1 As String = Path.GetDirectoryName(sFile)
Dim newFullPath As String = sFile
While File.Exists(newFullPath)
Dim tempFileName As String = String.Format("{0}({1})", fileNameOnly, System.Math.Max(System.Threading.Interlocked.Increment(count),count - 1))
newFullPath = Path.Combine(path__1, tempFileName & extension)
End While
Using f As New IO.StreamWriter(newFullPath, True)
Dim col As String = ""
Dim row As String = ""
Dim i As Integer = 0
For Each r As DataGridViewRow In DataGridView1.Rows
For Each c As DataGridViewColumn In DataGridView1.Columns
row = row & "'" & Convert.ToString(r.Cells(c.HeaderText).Value) & "' "
Next
If i < DataGridView1.Rows.Count - 1 Then row &= Environment.NewLine
Next
f.WriteLine(row)
MessageBox.Show("file created")
End Using
End If
无论如何谢谢!!!
Thanks anyway!!!
这篇关于将Datagridview导出到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!