引言
在现代数据驱动的应用开发中,将程序内存中的数据以结构化的形式呈现给用户是一个普遍的需求。其中,将c# list集合中的数据导出到excel文件,因其良好的可读性、易分享性以及强大的数据分析能力,成为了开发者们经常需要实现的功能。
无论是生成统计报表、导出用户数据、进行数据迁移,还是为bi工具提供数据源,高效且格式友好的excel导出功能都至关重要。本文将介绍如何使用 spire.xls for .net,通过清晰直观的方式,将 list<t> 数据写入excel文件。
c#数据导出excel的典型场景
在实际项目中,常见需求包括:
- 导出订单数据供财务核对
- 导出客户信息供销售分析
- 导出统计结果供管理层查看
excel作为数据展示与处理的通用工具,其格式控制能力和易操作性,使其成为最常见的数据输出形式之一。
环境准备
在 visual studio 中通过 nuget 安装 spire.xls:
- 右键项目
- 选择“管理nuget程序包”
- 搜索
spire.xls - 安装
安装完成后即可在代码中引用:
using spire.xls;
示例数据模型
public class product
{
public int id { get; set; }
public string name { get; set; }
public decimal price { get; set; }
public datetime manufacturedate { get; set; }
public bool isavailable { get; set; }
}
核心实现:将 list 数据写入 excel
using spire.xls;
using system;
using system.collections.generic;
public class excelexporter
{
public static void exportproductstoexcel(list<product> products, string filepath)
{
workbook workbook = new workbook();
worksheet sheet = workbook.worksheets[0];
sheet.name = "产品列表";
int row = 1;
// 写入表头
sheet.range[row, 1].value2 = "id";
sheet.range[row, 2].value2 = "name";
sheet.range[row, 3].value2 = "price";
sheet.range[row, 4].value2 = "manufacturedate";
sheet.range[row, 5].value2 = "isavailable";
row++;
// 写入数据
foreach (var product in products)
{
sheet.range[row, 1].value2 = product.id;
sheet.range[row, 2].value2 = product.name;
sheet.range[row, 3].value2 = (double)product.price;
sheet.range[row, 4].value2 = product.manufacturedate;
sheet.range[row, 5].value2 = product.isavailable;
row++;
}
// 自动调整列宽
sheet.allocatedrange.autofitcolumns();
// 设置标题样式
var headerrange = sheet.range[1, 1, 1, 5];
headerrange.style.font.isbold = true;
headerrange.style.horizontalalignment = horizontalaligntype.center;
workbook.savetofile(filepath, excelversion.version2016);
workbook.dispose();
}
public static void main(string[] args)
{
list<product> productlist = new list<product>
{
new product { id = 1, name = "笔记本电脑", price = 8999.00m, manufacturedate = new datetime(2023, 1, 15), isavailable = true },
new product { id = 2, name = "无线鼠标", price = 129.50m, manufacturedate = new datetime(2023, 3, 10), isavailable = true },
new product { id = 3, name = "机械键盘", price = 599.00m, manufacturedate = new datetime(2022, 11, 20), isavailable = false },
new product { id = 4, name = "显示器", price = 1999.99m, manufacturedate = new datetime(2023, 2, 5), isavailable = true },
new product { id = 5, name = "ssd硬盘", price = 650.00m, manufacturedate = new datetime(2023, 4, 1), isavailable = true }
};
string outputpath = "productsexport.xlsx";
exportproductstoexcel(productlist, outputpath);
}
}
导出结果演示:

格式控制与扩展
在实际项目中,往往需要对单元格格式进行控制。例如:
设置日期格式:
sheet.range[row, 4].style.numberformat = "yyyy-mm-dd";
设置货币格式:
sheet.range[row, 3].style.numberformat = "¥#,##0.00";
通过单元格对象可以精确控制:
- 字体
- 对齐方式
- 数字格式
- 背景颜色
- 边框
实践建议
- 处理完毕后调用
dispose()释放资源 - 建议使用
try-catch进行异常处理 - 大数据量导出时可考虑分批处理
- 注意数值类型转换(如 decimal 转 double)
总结
通过遍历 list<t> 并使用 sheet.range[row, column].value2 写入数据,可以实现清晰、可控且易维护的excel导出逻辑。
这种方式结构直观,便于扩展格式控制,适用于大多数常见业务场景。结合 spire.xls 提供的丰富功能,可以进一步实现图表生成、公式计算、数据验证等高级需求,从而满足更复杂的报表开发场景。
以上就是c#使用spire.xls for .net将list数据导出excel的操作指南的详细内容,更多关于c#将list数据导出excel的资料请关注代码网其它相关文章!
发表评论