如何为读入的excel文件的每一行输出单个文件?

时间:2015-06-16 17:08:56

标签: vb.net excel visual-studio-2010

如何为每行excel数据输出文件?现在它输出正确数量的文件,但数据行增加,因此文件1正确,文件2有行1和行2等。

 Dim smNum As Integer = 0

        If rowct > 0 Then

            For rr As Integer = 10 To rowct
                For cc As Integer = 1 To colct
                    val = CType(r.Cells(rr, cc), Excel.Range).Value

                    If val = "" Then Exit For

                    str.Append(ht((cc - 1).ToString)).Append(",")
                    str.Append(val)

                    'assigning the sample managaer num in column 1
                    If cc = 1 Then
                        smNum = val
                        file_name = selectedFile.Substring(13, 16) & "_" & smNum & "_" & todays_date & file_count & ".csv"
                        full_path = save_file_path & file_name
                        MessageBox.Show("Sample Manager Number: " & val & full_path)
                    End If

                    If cc < colct Then
                        str.Append(",")
                    End If

                    My.Computer.FileSystem.WriteAllText(full_path, str.ToString, False)
                    lstFileOut.Items.Add(file_name)

                Next
                'str.AppendLine()
            Next

        End If

1 个答案:

答案 0 :(得分:2)

在每行前使用StringBuilder.Clear

id

我还建议每行调用一次My.Computer.FileSystem.WriteAllText,因为你要写入每列上的文件。