如何在点击按钮上创建CSV文件 [英] How to create CSV file on click button

查看:81
本文介绍了如何在点击按钮上创建CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在创建一个扫描条形码的移动应用程序.扫描的条形码将输入到datagrid中,并在按钮上单击以创建CSV文件. CSV文件包含datagrid中的条形码.

为此,我正在编写以下代码,但是在CSV文件中仅输入了Datagrid的最后一条记录

Hi Everyone,

I am creating a Mobile application of scanning barcodes. Barcodes scanned will be entered in datagrid and on button click the CSV file is created. CSV file contains the barcodes which are in datagrid.

To do this I am writing the below code but only the Last record of Datagrid is entered in my CSV file

Private Sub btnsave_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles btnsave.KeyPress
        Dim createHfile, mstrHeaderdata, mWriteData As String
        Select Case e.KeyChar
            Case vbCrLf
                e.Handled = True

                createHfile = "Stock_Taking.csv"

                If File.Exists("\Application\" & createHfile) = False Then
                    Try
                        Dim SrHeader As StreamWriter = File.CreateText("\Application\" & createHfile)
                        mstrHeaderdata = "InventoryCountNo, SerialNo, DateTime"
                        SrHeader.WriteLine(mstrHeaderdata)
                        SrHeader.Close()
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                End If

                Do
                    Try
                        Dim sr As StreamWriter = File.AppendText("\Application\" & createHfile)
                        mWriteData = "" & dr(0) & "," & dr(1) & "," & Now & ""
                        sr.WriteLine(mWriteData)
                        sr.Close()
                        MessageBox.Show("Saved Successfully")
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                Loop Until Table1.Rows.Count = 0

        End Select
    End Sub



我知道我必须为datagrid运行循环,但是我不知道如何编写代码.
谁能帮我从datagrid的CSV文件中获取所有记录,因为我是datagrid的新手



I know I have to run the loop for datagrid, but I dont know how to code it.
Can anyone help me to get all the records from datagrid in my CSV file as I am new to datagrid

Any help is kindly appreciated.

推荐答案

首先想到的是:为什么要为标题和数据创建单独的流?为何每次循环时都为数据创建一个新的流?

一次创建一个新的流.如果需要,可将其用作标题,然后在再次关闭它之前附加所有记录.

第二件事是您认为完全行之有效的惊奇!显然,您直到完成后才让它运行,因为它一旦进入循环就永远不会退出循环:您不会从循环内的表中删除任何行,因此行数永远不会减少.

将来,最好在进入循环之前而不是在结束时检查计数,而不是在性能过高时依靠try catch块进行拾取.为什么?因为当您走到很远时(出于上述原因,您将不会以当前形式显示该流),流写入器永远不会关闭,因此该文件将一直使用,直到垃圾收集器清除流为止.
The first thing that springs to mind is: why are you creating a separate stream for your header, and your data? And why do you create a new stream for your data each time round the loop?

Create a new stream once. Use it for the header if you need to, then append all the records before closing it again.

The second thing is sheer amazement that you think that works at all! Clearly, you have not left it run until the finish, since it will never exit the loop once it has entered it: you are not removing any rows from the table within the loop, so the count of the number of rows will never go down.

In future, it is a very good idea to check the count before you enter the loop, not at the end, rather than relying on the try catch block to pick up when you have good too far. Why? Because when you have gone to far (which you won''t in the current form for reasons mentions above) the stream writer is never closed, so the file will remain in use until the Garbage Collector clears the stream.


这篇关于如何在点击按钮上创建CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆