tocsv.vb

来自「项目是为日本瑞萨工作所做的BAKE炉温控系统 整个文件夹包括设计文档」· VB 代码 · 共 49 行

VB
49
字号
Imports System.IO
Module ToCSV
    '**************************************************************************
    '功能:将记录集DataTB中的内容导出致.cvs文件
    '入口:查询标志flag记录集行数rowcount记录集列数colcount,文件名filename,记录集DataTB(DataSet)
    '出口:文件名说明信息filename
    '**************************************************************************
    Public Function DataSetToCSV(ByVal rowcount As Integer, ByVal colcount As Integer, _
    ByVal filename As String, ByVal DataTB As DataTable) As String

        Dim i As Integer, j As Integer

        REM 对话框
        Dim dialog1 As New SaveFileDialog  '创建对话框
        dialog1.AddExtension = True : dialog1.CheckPathExists = True
        dialog1.FileName = filename & System.DateTime.Now.Date     '文件名后自动加当前日期
        dialog1.Filter = "Excel Workbooks (*.csv) | *.csv"  '默认扩展名
        dialog1.OverwritePrompt = True
        dialog1.Title = "保存记录"
        If (dialog1.ShowDialog() <> DialogResult.OK) Then GoTo DataSetToCSV_exit
        filename = dialog1.FileName '记录文件名
        If (File.Exists(filename)) Then File.Delete(filename) '文件名存在,则删除

        REM 写csv文件
        Dim str As New System.IO.FileStream(filename, System.IO.FileMode.Create)
        Dim streamwriter As New System.io.StreamWriter(str, System.Text.Encoding.GetEncoding("gb2312"))

        REM 写列名
        streamwriter.Write("记录日期,")
        streamwriter.Write("当前时间,") : streamwriter.Write("设定时间,")
        streamwriter.Write("当前温度,") : streamwriter.Write("设定温度,")
        streamwriter.Write(vbCrLf)

        REM 写记录
        For i = 0 To rowcount - 1
            For j = 0 To colcount - 1
                streamwriter.Write(DataTB.Rows(i).Item(j)) : streamwriter.Write(",")
                Application.DoEvents()
            Next
            streamwriter.Write(vbCrLf)
        Next

        REM 
        streamwriter.Close()
        str.Close()
DataSetToCSV_exit:
        Return filename
    End Function
End Module

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?