从c#中的多个数据表或数据集创建自定义Excel报表的最佳方法 [英] best way to create custom excel report from multiple datatable or dataset in c#
本文介绍了从c#中的多个数据表或数据集创建自定义Excel报表的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须从多个数据库表创建一个自定义Excel报表。请告诉我哪个是从c#.i中创建自定义Excel报表的最佳方法。我是新来的报表制作
i have to create a custom excel report from multiple table of database. please let me know which is best way to create custom excel report from multiple datatables in c#.i am new to report making
推荐答案
此代码(适用于winform)允许您创建新的.csv或.txt文件,并将其保存在计算机中所需的任何位置。您可以更改过滤器以添加.xls或任何其他类型。
This Code(for winform) allow you to create a new .csv or .txt file and save it in any location you want in you Computer. you can change the Filter to add .xls or any other type.
try
{
SaveFileDialog saveDiag = new SaveFileDialog();
saveDiag.Filter = " Excel (*.csv) | *.CSV | Fichier Text (*.txt)| *.txt";
saveDiag.DefaultExt = "*.CSV";
if (saveDiag.ShowDialog() == DialogResult.OK)
{
using (Stream stream = File.Open(saveDiag.FileName, FileMode.Create,
FileAccess.ReadWrite))
{
using (StreamWriter sWriter = new StreamWriter(stream,
Encoding.UTF8))
{
//put your logic in here
sWriter.WriteLine("Name ; Last Name ; Login; Password ");
foreach(User user in ListUsers)
{
sWriter.WriteLine(user.Name + ";" + user.LastName + ";" +
user.Password);
}
}
}
MessageBox.Show("operation Completed");
}
}
catch (Exception exception)
{
MessageBox.Show(exception.InnerException.ToString());
}
这将在excel中生成一个有组织的表。 ;是列分隔符。
希望它有所帮助。
this will generate a organized table in excel. the ";" is the Column separator.
Hope it helps.
这篇关于从c#中的多个数据表或数据集创建自定义Excel报表的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文