首页 > 解决方案 > 如何在文件中写入 C# 中的查询结果。例如(姓名工资)

问题描述

我需要在 .doc 文件中写入 C# 代码中的查询结果

SqlCommand command = new SqlCommand("SELECT Id,Name,Price FROM [Products] WHERE [Id]=@Id ", sqlConnection);
While (await sqlReader.ReadAsync()){
    listBox3.Items.Add(Convert.ToString(sqlReader["id"]) + "  " + 
    Convert.ToString(sqlReader["Name"]) + "  " + (1 + ((readSearch1 - f) * 1.2) / f));
}

标签: c#sql

解决方案


您可以使用 Docx 库来执行此操作。 https://cathalscorner.blogspot.com/search?q=create

 // Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
    // Create a new Paragraph with the text "Hello World".
    Paragraph p = document.InsertParagraph("Hello World.");

    // Make this Paragraph flow right to left. Default is left to right.
    p.Direction = Direction.RightToLeft;

    // Save all changes made to this document.
    document.Save();
}

推荐阅读