将winform数据导出到.txt文件

发布于 2024-09-02 01:59:33 字数 158 浏览 8 评论 0原文

我有一个带有两个数据网格和多个文本框的 winform。我想让用户选择将此数据导出到驱动器上他们选择的位置的文本文档中。我还希望预先格式化文本文档,并插入文本框和数据网格中的值。

是否可以使用 StreamWriter 预先格式化 txt 文档?我该如何让用户选择保存此导出文件的位置?

I have a winform with two data grids, and multiple text boxes. I want to give the user the option to export this data to a text document in a location of their choice on their drive. I also want the text document to be pre-formatted, and the values from the text boxes and datagrids to be plugged in.

Is it possible to pre-format a txt document using StreamWriter? And how to I go about giving the user the option of where to save this exported file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-09-09 01:59:33

您必须格式化要通过 StreamWriter 写入的字符串。

using(StreamWriter sw = new StreamWriter(filePath)) {
    string firstLine = string.Concat("\n", string.Format(@"Customer number: {0}, Customer name: {1}", textBox1.Text, textBox2.Text));
    string secondLine = string.Format(@"Address: {0}", textBox3.Text);

    sw.WriteLine(firstLine);
    sw.WriteLine(secondLine);

    // Loop through your DataGridView.Rows or Cells and do the same.

    sw.Flush();
    sw.Close();
}

输出到文件

客户编号:[12345678] 客户名称:[客户名称]
地址:[地址]

其中方括号内的信息是用户通过文本框输入的信息。

You will have to format the string you want to write throught the StreamWriter.

using(StreamWriter sw = new StreamWriter(filePath)) {
    string firstLine = string.Concat("\n", string.Format(@"Customer number: {0}, Customer name: {1}", textBox1.Text, textBox2.Text));
    string secondLine = string.Format(@"Address: {0}", textBox3.Text);

    sw.WriteLine(firstLine);
    sw.WriteLine(secondLine);

    // Loop through your DataGridView.Rows or Cells and do the same.

    sw.Flush();
    sw.Close();
}

Output to file

Customer number: [12345678] Customer name: [customer name]
Address: [address]

Where the information between square brackets is the information input by the user through the TextBoxes.

≈。彩虹 2024-09-09 01:59:33

我不完全确定预格式化文本文档是什么意思。 StreamWriter 可用于以您指定的任何格式写出数据。这实际上取决于您如何向 StreamWriter 提供数据。例如,如果您希望网格行显示为 csv 的写入每个项目,请添加一个逗号(最后一个元素除外),然后在最后一个元素后面写入一个新行,对所有行重复此操作。如果我遗漏了什么,请告诉我。

至于如何让用户选择保存位置,您应该使用 SaveFileDialog 控件(它应该位于 Visual Studio 的工具箱中)。这将打开资源管理器视图,允许用户选择位置和名称)。有关如何实际使用该类的详细信息,请参阅链接的文档。这是相当简单的

I'm not entirely sure what you mean by pre-format a text document. The StreamWriter can be used to write out in whatever format you specify the data. Its really going to come down to how you provide the data to the StreamWriter. For example if you want your grid rows to appear as csv's write each one out item, add a comma (except for the last element) and then after the last element write a new line, repeat for all rows. If I'm missing something please let me know.

As far as how to give the user the option of where to save you should use the SaveFileDialog control (it should be in your toolbox in Visual Studio). That will open the explorer view that will allow the user to select the location and name). See the linked documentation for details on how to actually use the class. It is fairly straight forward

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文