excel - 如何从控制台应用程序(.Net Core)导出 excel/pdf
问题描述
我试图从 .Net Core 创建一个控制台应用程序。希望能够进行一些 SQL 查询和计算,然后生成一些摘要报告并导出为 excel/pdf 格式。
我已经尝试过能够从 SQL 服务器数据库中选择一些 sql 查询的控制台应用程序。但是不确定如何在 .Net Core 的控制台应用程序中保存结果并导出为 excel/pdf 格式
using System;
using System.Data.SqlClient;
namespace SQLconnectionApp
{
class Program
{
static void Main(string[] args)
{
using (var SqlConnection = new SqlConnection("Server=myserver;Database=myDatabase;User Id=username;Password =password"))
{
using (var command = new SqlCommand("Select * from table", SqlConnection))
{
SqlConnection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"id: {reader["id"]}, Name: {reader["Name"]}");
}
}
#the job want to do
#collect the result from SQL server
#output some reports file in the folder
}
}
}
}
}
解决方案
安装
dotnet add package Magicodes.IE.Pdf
代码
创建 Dto
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
导出到文件
public async Task ExportPdf()
{
var exporter = new PdfExporter();
var result = await exporter.ExportListByTemplate("test.pdf", new List<Student>()
{
new Student
{
Name = "MR.A",
Age = 18
},
new Student
{
Name = "MR.B",
Age = 19
},
new Student
{
Name = "MR.B",
Age = 20
}
});
}
推荐阅读
- security - Appian 的低代码系统建议的基于文件夹层次结构的安全性是否在其他地方使用?
- or-tools - 如何在 ORTools 中创建协作 VRP?
- python - Tkinter grid_size() 函数行为不当
- flutter - Flutter MIUI 通知设置
- python - 有没有办法增加matplotlib中所有子图的轴的厚度,Python
- typescript - 如何使用“忘记”属性类型(“任何”而不是“字符串”|“布尔”等)获取类/接口类型克隆?
- python - 遍历 YAML 对象
- go - Golang SFTP 目录观察器执行 Lstat 失败
- magento2 - 从 Magento 2.3.3 升级到 Magento 2.4.2 会导致引用损坏错误
- php - PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in ... on line 84