回 帖 发 新 帖 刷新版面

主题:文件导入导出问题  内附代码 请高手修改

文件导出代码:
Sub DCSJ()

        If myCon.State = ConnectionState.Closed Then
            myCon.Open()
        End If

        Try
            Dim sFilename As String
            With Me.SaveFileDialog1
                .Title = "保存文件"
                .CheckFileExists = False
                .CheckPathExists = False
                .CreatePrompt = True
                .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
                .ShowDialog()
                sFilename = .FileName
            End With

            If sFilename = "" Then
                MessageBox.Show("请输入您要保存的文件名。", "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            Else

                Dim myStreamWriter As StreamWriter
                myStreamWriter = File.CreateText(sFilename)


                Dim myOutput As String

                If myCon.State = ConnectionState.Closed Then
                    myCon.Open()
                End If

                Dim MyCmd As New OleDb.OleDbCommand("SELECT * FROM PACKLIST ORDER BY ID", myCon)

                MyDa.SelectCommand = MyCmd
                myDataSet.Clear()
                MyDa.Fill(myDataSet, "table")
                If myDataSet.Tables(0).Rows.Count > 0 Then
                    Dim row As DataRow
                    For Each row In myDataSet.Tables(0).Rows()

                        myStreamWriter.WriteLine(row!CTN_NO & "")
                        myStreamWriter.WriteLine(row!MAT_NM & "")
                        myStreamWriter.WriteLine(row!MAT_SPC & "")
                        myStreamWriter.WriteLine(row!EUNIT & "")
                        myStreamWriter.WriteLine(row!ENT_QNT & "")
                        myStreamWriter.WriteLine(row!DEL_QNT & "")
                        myStreamWriter.WriteLine(row!PK_QNT & "")
                        myStreamWriter.WriteLine(row!SPK_QNT & "")
                        myStreamWriter.WriteLine(row!N_WT & "")
                        myStreamWriter.WriteLine(row!G_WT & "")
                        myStreamWriter.WriteLine(row!ID & "")

                    Next

                    myStreamWriter.Flush()
                    myStreamWriter.Close()

                End If

            End If
        Catch ex As Exception
            MessageBox.Show(Err.Description)
        End Try

    End Sub

回复列表 (共4个回复)

沙发

文件导入代码:
    Sub DRSJ()

        If myCon.State = ConnectionState.Closed Then
            myCon.Open()
        End If

        Dim Result As DialogResult
        Dim filename As String
        With Me.OpenFileDialog1
            .Title = "读取文件"
            .CheckFileExists = True
            .CheckPathExists = True
            .Filter = "txt files (*.xls)|*.xls|All files (*.*)|*.*"
            .ShowDialog()
            filename = .FileName
        End With

        If Result = Me.OpenFileDialog1.ShowDialog.Cancel Then
            Exit Sub
        End If

        If filename = "" Then
            MessageBox.Show("该文件不存在,请检查您选取的文件名和路径。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        Else

            Dim myStreamRead As StreamReader

            myStreamRead = File.OpenText(filename)

            If Not (myStreamRead Is Nothing) Then

                While myStreamRead.Peek <> -1

                    If myCon.State = ConnectionState.Closed Then
                        myCon.Open()
                    End If

                    Dim mySet As New DataSet
                    myCmd.CommandText = "SELECT top 1 * FROM PACKLIST"
                    myCmd.Connection = myCon
                    myCmd.ExecuteNonQuery()
                    MyDa.SelectCommand = myCmd
                    mySet.Clear()
                    MyDa.Fill(mySet, "A")

                    Dim row2 As DataRow = mySet.Tables("A").NewRow

                    row2!CTN_NO = myStreamRead.ReadLine
                    row2!MAT_NM = myStreamRead.ReadLine
                    row2!MAT_SPC = myStreamRead.ReadLine
                    row2!EUNIT = myStreamRead.ReadLine
                    row2!ENT_QNT = Val(myStreamRead.ReadLine)
                    row2!DEL_QNT = Val(myStreamRead.ReadLine)
                    row2!PK_QNT = Val(myStreamRead.ReadLine)
                    row2!SPK_QNT = Val(myStreamRead.ReadLine)
                    row2!N_WT = Val(myStreamRead.ReadLine)
                    row2!G_WT = Val(myStreamRead.ReadLine)
                    row2!ID = Val(myStreamRead.ReadLine)


                    mySet.Tables("A").Rows.Add(row2)
                    Dim myBuilder As New OleDb.OleDbCommandBuilder(MyDa)
                    MyDa.Update(mySet, "A")
                End While

            End If


        End If

        MessageBox.Show("文件读取完毕,已经读到文件末尾,请按打印按钮打印。", "读取信息", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Call showdata("SELECT * FROM PACKLIST ORDER BY ID DESC")
        Call ShowTOTAL()
    End Sub

板凳

若导出的文件格式为如下所示,则结果正确:

TOPTEX 24472 XIAMEN PRODUCE OF MALAYSIA CENTRIGEC LATEX CC 645 SS 996 1/80
LATEX ADHSIVE 天然胶乳(白乳胶)
1
1
1

1

1
1
1
若为如下所示,则导入到数据库的数据不符合要求:
TOPTEX
24472
XIAMEN PRODUCE OF
MALAYSIA
CENTRIGEC
LATEX
CC 645
SS 996
1/80.
LATEX ADHSIVE 天然胶乳(白乳胶)
1
1
1

1

1
1
1

3 楼

PS:导入数据库(ACCESS)中,数据必须为如下格式才正确。因为只有这样,打印报表时CTN_NO字段的显示格式才能跟数据库的保存格式一样:(抓图无法贴到帖子上,故以此格式显示给大家)
----------------------------------------------------------------------
|           CTN_NO            |               MAT_NM                |
---------------------------------------------------------------------
| TOPTEX                      | LATEX ADHSIVE 天然胶乳(白乳胶)      |
| 24472                       |                                     |
| XIAMEN PRODUCE OF           |                                     |
| MALAYSIA                    |                                     |
| CENTRIGEC                   |                                     |
| LATEX                       |                                     |
| CC 645                      |                                     |
| SS 996                      |                                     |
| 1/80                        |                                     |
|                             |                                     |
请问:我要怎么把
TOPTEX
24472
XIAMEN PRODUCE OF
MALAYSIA
CENTRIGEC
LATEX
CC 645
SS 996
1/80.
LATEX ADHSIVE 天然胶乳(白乳胶)
1
1
1

1

1
1
1
这些导出的数据读到数据库中以如上表格形式显示?

4 楼

之前我的解决办法是:用myStreamRead.ReadLine和 myStreamWriter.WriteLine来导入导出数据,可这样做只能一行一行的读入读出,当遇到各别字段要按回车分行显示时可正常导出数据到文本文件但行数据就多出了好几行,当要导入数据导回到数据库时,就没办法把好几行按要求导入在同一个字段中,请问各位师傅,要怎么解决这个问题?

我来回复

您尚未登录,请登录后再回复。点此登录或注册