国产化Excel开发组件Spire.XLS教程:C# 轻松将 DataSet 导出到 Excel
<p><img src="https://image.evget.com/attachment/keditor/image/20250918/094409_9.png"></p><p>在 C# 开发中,<strong>DataSet</strong> 常用于管理内存中的数据,通常来源于数据库查询或系统集成过程。在很多情况下,你可能需要<strong>将 DataSet 导出为 Excel 文件</strong>——例如生成报表、与非开发人员共享结构化数据,或将记录归档以便后续参考。</p>
<p>E-iceblue旗下Spire系列产品是国产文档处理领域的优秀产品,<strong><em>支持国产化</em></strong>,帮助企业高效构建文档处理的应用程序。本文将介绍如何使用 <strong>Spire.XLS for .NET </strong><strong>在 C# 中导出 DataSet 到 Excel</strong>,包括创建 Excel 文件、将多个 DataTable 分别写入不同工作表、应用格式化,以及处理大数据量导出等场景。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Spire.XLS for .NET试用下载,请联系E-iceblue Spire官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>欢迎加入Spire技术交流Q群(125237868),与更多小伙伴一起提升文档开发技能~</strong></span></p>
<h2>1. DataSet 基础与 Excel 导出环境搭建</h2>
<p><strong>什么是 DataSet?</strong></p>
<p>C# 中的 <strong>DataSet</strong> 是内存中对结构化数据的表示,可以包含多个 <strong>DataTable</strong>,并保存其行、列和关系。这使得开发者在无需直接连接数据库的情况下,也能方便地处理关系型数据。</p>
<p><strong>为什么要将 DataSet 导出到 Excel?</strong></p>
<ul>
<li><strong>数据共享</strong> —— Excel 文件易于团队间传递和查看。</li>
<li><strong>数据分析</strong> —— 分析人员可在 Excel 中直接使用公式、数据透视表和图表进行处理。</li>
<li><strong>归档保存</strong> —— 将查询结果或处理过的数据存储为可读性强的便携格式。</li>
</ul>
<p>相比纯文本或 CSV,Excel 具备<strong>丰富的格式化功能</strong>、<strong>多工作表支持</strong>以及更佳的可读性。</p>
<h3>环境搭建</h3>
<p>本文使用 <strong>Spire.XLS for .NET</strong> 来导出 DataSet 到 Excel。通过 NuGet 安装:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>Install-Package Spire.XLS</code></pre>
<p>引用命名空间:</p>
<pre class="prettyprint lang-cs highlighter-hljs"><code>using Spire.Xls;
using System.Data;
using System.Drawing; // 用于颜色</code></pre>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Spire.XLS for .NET试用下载,请联系E-iceblue Spire官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>欢迎加入Spire技术交流Q群(125237868),与更多小伙伴一起提升文档开发技能~</strong></span></p>
<h2>2. 在 C# 中用 DataSet 数据创建 Excel 文件</h2>
<p>将 DataSet 导出为 Excel 包含两个关键步骤:准备数据和写入工作簿。实际应用中,DataSet 通常来自数据库或 API 查询。为了演示清晰,这里我们先构建一个内存中的 DataSet,再将其导出为 Excel 文件,每个 DataTable 对应一个工作表。</p>
<h3>2.1 使用示例数据初始化 DataSet</h3>
<p>下面的代码示例构建了一个包含多个业务表的 DataSet,表中包含多种常见数据类型(int、string、DateTime、decimal)。</p>
<pre class="prettyprint lang-cs highlighter-hljs"><code>using System;
using System.Data;
class Program
{
static DataSet CreateSampleDataSet()
{
DataSet ds = new DataSet("公司数据");
// 员工表
DataTable employees = new DataTable("员工");
employees.Columns.Add("编号", typeof(int));
employees.Columns.Add("姓名", typeof(string));
employees.Columns.Add("部门编号", typeof(int));
employees.Columns.Add("入职日期", typeof(DateTime));
employees.Columns.Add("薪资", typeof(decimal));
employees.Rows.Add(1, "张伟", 101, new DateTime(2020, 5, 12), 5500.00m);
employees.Rows.Add(2, "李娜", 102, new DateTime(2019, 3, 8), 7200.50m);
employees.Rows.Add(3, "王强", 103, new DateTime(2021, 11, 2), 4800.75m);
// 部门表
DataTable departments = new DataTable("部门");
departments.Columns.Add("部门编号", typeof(int));
departments.Columns.Add("部门名称", typeof(string));
departments.Rows.Add(101, "人事部");
departments.Rows.Add(102, "信息技术部");
departments.Rows.Add(103, "财务部");
// 项目表
DataTable projects = new DataTable("项目");
projects.Columns.Add("项目编号", typeof(int));
projects.Columns.Add("项目名称", typeof(string));
projects.Columns.Add("负责人编号", typeof(int));
projects.Columns.Add("开始日期", typeof(DateTime));
projects.Rows.Add(1001, "招聘系统", 1, new DateTime(2023, 1, 15));
projects.Rows.Add(1002, "ERP升级", 2, new DateTime(2023, 4, 10));
projects.Rows.Add(1003, "预算规划", 3, new DateTime(2023, 7, 5));
ds.Tables.Add(employees);
ds.Tables.Add(departments);
ds.Tables.Add(projects);
return ds;
}
}</code></pre>
<h3>2.2 将 DataSet 导出为 Excel 文件</h3>
<p>有了 DataSet 后,接下来创建 Excel 文件:实例化 <strong>Workbook</strong>,遍历 DataTable,将其插入到工作表中,最后保存文件。</p>
<pre class="prettyprint lang-cs highlighter-hljs"><code>using Spire.Xls;
using System.Data;
class Program
{
static void Main()
{
DataSet ds = CreateSampleDataSet();
Workbook workbook = new Workbook();
workbook.Worksheets.Clear();
// 每个 DataTable 导出为一个单独的工作表
for (int i = 0; i < ds.Tables.Count; i++)
{
Worksheet sheet = workbook.Worksheets.Add(ds.Tables.TableName);
sheet.InsertDataTable(ds.Tables, true, 1, 1);
sheet.Name = ds.Tables.TableName;
}
workbook.SaveToFile("DatasetToExcel.xlsx", ExcelVersion.Version2016);
}
}</code></pre>
<p><strong>导出要点说明</strong></p>
<ul>
<li>每个 <strong>DataTable</strong> 会被写入单独的工作表。</li>
<li><strong>InsertDataTable(DataTable, bool, int, int)</strong> 可指定数据插入的起始单元格。</li>
<li><strong>SaveToFile()</strong> 将工作簿保存为指定格式的文件。</li>
</ul>
<p>除了将 DataTable 导出到不同工作表,你也可以通过调整 <strong>InsertDataTable</strong> 方法的起始行列参数,将多个表写入同一个工作表。</p>
<p><strong>效果预览</strong></p>
<p>下图展示了从 DataSet 导出的 Excel 文件,其中包含 Employees、Departments、Projects 三个工作表。</p>
<p><img src="https://image.evget.com/attachment/keditor/image/20250918/094644_0.png"></p>
<h2>3. 使用 C# 为 Excel 工作表添加格式</h2>
<p>原始数据往往不够直观,格式化可以提升可读性,使报表更专业。通过 <strong>Spire.XLS</strong>,可以设置字体、背景色、边框,并对数字和日期应用格式。</p>
<pre class="prettyprint lang-cs highlighter-hljs"><code>using System.Drawing;
using Spire.Xls;
// 获取第一个工作表
Worksheet sheet1 = workbook.Worksheets["员工"];
// 1) 表头样式 (A1:E1)
CellRange header = sheet1.AllocatedRange.Rows;
header.Style.Font.IsBold = true;
header.Style.Font.Size = 12;
header.Style.Font.Color = Color.White;
header.Style.Color = Color.SteelBlue;
header.BorderAround(LineStyleType.Thin);
// 2) 设置列的数字格式 (D: HireDate, E: Salary)
sheet1.AllocatedRange.Columns.Style.NumberFormat = "yyyy-mm-dd";
sheet1.AllocatedRange.Columns.Style.NumberFormat = "$#,##0.00";
// 3) 数据区域设置浅色背景 (A2:E4 示例)
CellRange data = sheet1.Range["A2:E4"];
data.Style.Color = Color.FromArgb(245, 247, 250);
data.BorderAround(LineStyleType.Thin);
// 设置字体名
sheet1.AllocatedRange.Style.Font.FontName = "微软雅黑";
// 自动调整行高列宽
sheet1.AllocatedRange.AutoFitColumns();
sheet1.AllocatedRange.AutoFitRows();</code></pre>
<p><strong>格式化功能说明</strong></p>
<ul>
<li><strong>Style.Font</strong> —— 设置字体属性(加粗、大小、颜色)。</li>
<li><strong>Style.Color</strong> —— 设置单元格背景色。</li>
<li><strong>Borders/BorderAround</strong> —— 为范围绘制边框。</li>
<li><strong>NumberFormat</strong> —— 应用 Excel 内置格式(日期、货币、百分比等)。</li>
<li><strong>AutoFitColumns/Rows</strong> —— 根据内容自动调整列宽和行高。</li>
</ul>
<p><strong>格式化效果预览</strong></p>
<p>下图展示了蓝色背景的加粗表头、带边框的区域,以及日期和货币列的正确格式。</p>
<p><img src="https://image.evget.com/attachment/keditor/image/20250918/094756_5.png"></p>
<h2>4. 处理大型 DataSet 导出</h2>
<p>当 DataSet 数据量较大时,性能与内存消耗尤为关键。常见优化方式包括:</p>
<ul>
<li><strong>拆分到多个工作表</strong> —— 避免超过 Excel 行数限制,同时方便分类。</li>
<li><strong>分批写入</strong> —— 按表或按范围逐步插入数据。</li>
<li><strong>轻量化格式</strong> —— 减少复杂格式以降低文件大小和处理时间。</li>
<li><strong>流式处理</strong> —— 避免一次性加载全部数据。</li>
</ul>
<h2>5. 在 C# 中读取 Excel 数据到 DataSet</h2>
<p>除了导出,很多场景下还需要将 Excel 文件读取到 DataSet 中,用于数据处理或迁移。这在导入外部报表、系统集成或数据库预处理时非常实用。</p>
<pre class="prettyprint lang-cs highlighter-hljs"><code>using System.Data;
using Spire.Xls;
class Program
{
static DataSet ReadExcelIntoDataSet(string filePath)
{
DataSet ds = new DataSet();
Workbook workbook = new Workbook();
workbook.LoadFromFile(filePath);
foreach (Worksheet sheet in workbook.Worksheets)
{
DataTable dt = sheet.ExportDataTable();
dt.TableName = sheet.Name;
ds.Tables.Add(dt);
}
return ds;
}
}</code></pre>
<p><strong>ExportDataTable</strong> 方法可以将每个工作表转换为 <strong>DataTable</strong>,保留其结构和单元格数据。通过设置 <strong>TableName</strong> 并添加到 DataSet,即可将多个工作表整合为一个内存数据容器,便于进一步处理。</p>
<h2>总结</h2>
<p>在 C# 中<strong>将 DataSet 导出到 Excel</strong>,可以方便地生成报表、共享数据,并提升数据的可分析性与展示效果。通过<strong> Spire.XLS for .NET</strong>,你不仅能直接从 DataSet 创建 Excel 文件,还能进行格式化、管理多工作表,并高效处理大数据量。同时也支持将 Excel 数据导入 DataSet,便于与应用或数据库集成。</p>
<h2>常见问题:C# DataSet 与 Excel 集成</h2>
<h3>Q1: 如何将 DataSet 中的多个 DataTable 分别导出到不同工作表?</h3>
<p>遍历 <strong>ds.Tables</strong>,为每个 DataTable 调用 <strong>InsertDataTable</strong>,并新建工作表即可。</p>
<h3>Q2: 可以将 DataSet 导出到已存在 Excel 文件的指定工作表吗?</h3>
<p>可以。使用 <strong>Workbook.LoadFromFile()</strong> 打开文件,再选择目标工作表并调用 <strong>InsertDataTable</strong>。</p>
<h3>Q3: 导出 DataSet 到 Excel 时能保留列格式和数据类型吗?</h3>
<p>数据类型会被保留。你还可以在导出后对列应用日期、货币、对齐等格式。</p>
<h3>Q4: 如果 DataSet 超过 10 万行,如何优化导出性能?</h3>
<p>可拆分到多个工作表,分批写入,并减少复杂格式,以提升性能。</p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>Spire.XLS for .NET试用下载,请联系E-iceblue Spire官方授权代理商慧都科技</strong></span></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><strong>欢迎加入Spire技术交流Q群(125237868),与更多小伙伴一起提升文档开发技能~</strong></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/19098240
頁:
[1]