在 Visual Studio 中使用 C# 从 DGView 数据创建多个文本文件

Creation of multiple text files using C# in Visual Studio from DGView data

大家好,我的数据网格视图中有以下结果,我想将这些数据打印到多个文本文件中。

文本文件格式应以1stLine(列值请看图)开始,LastLine(列值请看图)结束。

将 filename1 列值视为该文件的文件名。

同样,我想创建多个文件。我不确定是否根据范围创建多个文件。

            private void Create_Click(object sender, EventArgs e)
    {
        int FileStart = 0;
        int FileEnd = 0;
        string FileName = "";
        String MyDir = "C:\Test\";
        StringBuilder builder = new StringBuilder();
        int rowcount = DGV.Rows.Count;
        int columncount = DGV.Columns.Count;
        for (int fn = 0; fn < rowcount - 1; fn++)// fro running down all rows
        {

                if (string.IsNullOrEmpty(DGV.Rows[fn].Cells[0].Value.ToString()) == false) //checking for the filename                  
               {
                    FileName = DGV.Rows[fn].Cells[0].Value.ToString();
                    builder.Clear();

                    FileStart = fn;

                if (string.IsNullOrEmpty(DGV.Rows[fn].Cells[4].Value.ToString()) == false) // this is where i tried to check the last line
                {
                    for (int row = fn; row < rowcount - 1; row++)
                    {
                        FileEnd = row;                                             
                    }
                }

                    for (int i = FileStart; i < rowcount - 1; i++) // here start generating the file based on first and last line .. but as of now i took rowcount as last line.. if i put last line returns nothing...i know this is where i am wrong, but i tried lot of options 
                    {
                        List<string> cols = new List<string>();
                        for (int j = 1; j < columncount; j++)
                        {
                            cols.Add(DGV.Rows[i].Cells[j].Value.ToString());
                        }

                        builder.AppendLine(string.Join("\n", cols.ToArray()));

                        if (!Directory.Exists(MyDir))
                        {
                            Directory.CreateDirectory(MyDir);
                        }
                        System.IO.File.WriteAllText(MyDir + FileName, builder.ToString());
                        MessageBox.Show(@"Text file was created.");

                    }
                }
                else
                {
                    fn = fn + 1;
                }
            }
        }

需要像

这样的输出文件

文件名1

1stLine Bodytext1 BodyText2
正文 1 正文 2
BodyText2 LastLine

文件名2

1stLine Bodytext1 BodyText2
正文 1 正文 2
正文 1 正文 2
正文 1 正文 2
Bodytext1 BodyText2 LastLine

文件名3

1stLine Bodytext1 BodyText2
正文 1 正文 2
正文 1 正文 2
Bodytext1 BodyText2 LastLine

下面的代码将 DataGrid.DataSource 数据表分成块,这些块将进入每个文件。

private void SaveToFiles(DataTable srcData)
        {
            if (srcData.Rows.Count > 0)
            {
                int FileStart = 0;
                int FileEnd = -1;

                while (FileStart < srcData.Rows.Count)
                {
                    for (int row = FileStart; row < srcData.Rows.Count; row++)
                    {
                        if (string.IsNullOrEmpty(srcData.Rows[row].Field<string>(0)) == false)
                        {
                            FileStart = row;
                        }
                        if (string.IsNullOrEmpty(srcData.Rows[row].Field<string>(4)) == false)
                        {
                            FileEnd = row;
                            break;
                        }
                    }

                    var data = srcData.AsEnumerable().Skip(FileStart).Take(FileEnd - FileStart + 1);
                    SaveFile(data.ToList());
                    FileStart = FileEnd + 1;
                }
            }
        }

您需要添加代码以您希望的格式将文本写入文件 - 代替注释。

private void SaveFile(List<DataRow> fileData)
        {
            if (fileData != null && fileData.Count > 0)
            {
                string fileName = fileData[0].ItemArray[0].ToString();

                using (var fileStrm = File.Create(fileName + ".txt"))
                {
                    using (StreamWriter wrtr = new StreamWriter(fileStrm))
                    {
                       // handle writing to file here
                    }
                }
            }
        }

有时用文字写下算法会有所帮助。想象一下,您需要手工完成。行动的秘诀会是什么样子?这是一个例子:

1) 你开始沿着你拥有的所有行走。 (对于所有行)

2) IF 你在 1 列中遇到一个非空项,你知道这是你的起点并且你记住了那个值。

3) 现在您知道在同一行中索引为 1 的列中有 1stLine 值。您也接受并记住该值。

4) 现在,您可以从 BodyText 值集合开始。 从你所在的同一行开始,然后沿着行走。在每一行中,您都从 2 列和 3 列中获取值,然后签入 4 列。 IF 它是空的,你放一个 \n 和你的行尾,然后继续下一行再次执行第 4 步。 ELSE表示你已经到了LastLine,你现在可以把你记住的内容写入文件了

5) 现在您将清除最后一个文件的内存,获取下一行并继续步骤 2

希望能帮助您解决问题。如果您有任何疑问,请在评论中提问。祝你好运

编辑:

好的,你手边的代码让我们看看:

1) 在开始第二个循环之前检查最后一行是没有用的。如果您查看我的算法的第 4 步),您需要首先收集第 3 列和第 4 列的值,然后检查第 4 列!

if (string.IsNullOrEmpty(DGV.Rows[fn].Cells[4].Value.ToString()) == false)
    break; // This will cancel the inner loop and you return to the outer loop