C#导出excel复杂表格(单元各合并)
zhezhongyun 2025-05-28 21:41 43 浏览
一、效果展示
二、代码实现
引用dll
using Aspose.Cells;
DataTable数据保存到Excel
/// <summary>
/// DataTable数据表保存至Excel (合并行报表)
/// </summary>
/// <param name="dt"></param>
/// <param name="filePath"></param>
/// <param name="excelParm"></param>
public static void ToExcel1(DataTable dt, string filePath, ExcelParm excelParm)
{
string subTitle = string.Empty;
//新建工作簿
Workbook wb = new Workbook();
//新建工作表
Worksheet ws = wb.Worksheets[0];
ws.Name = dt.TableName;
int rowIndex = 3;
int colIndex = 0;
int colCount = dt.Columns.Count;
int rowCount = dt.Rows.Count;
ws.Cells.SetRowHeight(rowIndex, 25);//设置行高
//创建样式
Style style = wb.Styles[wb.Styles.Add()];//新增样式
style.HorizontalAlignment = TextAlignmentType.Center; //单元格内容的水平对齐方式文字居中
style.Font.Name = "宋体"; //字体
style.Font.IsBold = true; //设置粗体
//style.Font.Color = Color.White;//设置字体颜色
style.Font.Size = 10; //设置字体大小
//style.ForegroundColor = Color.FromArgb(0, 196, 180); //背景色
style.Pattern = BackgroundType.Solid;
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.TopBorder].Color = Color.Black;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.BottomBorder].Color = Color.Black;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.LeftBorder].Color = Color.Black;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.RightBorder].Color = Color.Black;
//列名的处理
for (int i = 0; i < colCount; i++)
{
ws.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
ws.Cells[rowIndex, colIndex].SetStyle(style);//给单元格关联样式
colIndex++;
}
Style style2 = wb.Styles[wb.Styles.Add()];//新增样式
style2.Font.Name = "宋体";//文字字体
style2.Font.Size = 10;//文字大小
style2.ShrinkToFit = true;
style2.VerticalAlignment = TextAlignmentType.Center;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.TopBorder].Color = Color.Black;
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.BottomBorder].Color = Color.Black;
style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.LeftBorder].Color = Color.Black;
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.RightBorder].Color = Color.Black;
Style style3 = wb.Styles[wb.Styles.Add()];//组织标题样式
style3.Font.Name = "宋体";//文字字体
style3.Font.Size = 16;//文字大小
style3.HorizontalAlignment = TextAlignmentType.Center;
ws.Cells.SetRowHeight(0, 25);//设置行高
ws.Cells.Merge(0, 0, 1, dt.Columns.Count);
ws.Cells[0, 0].PutValue(excelParm.OrganName);
ws.Cells[0, 0].SetStyle(style3);
Style style5 = wb.Styles[wb.Styles.Add()];//标题样式
style5.Font.Name = "宋体";//文字字体
style5.Font.IsBold = true; //设置粗体
style5.Font.Size = 14;//文字大小
style5.HorizontalAlignment = TextAlignmentType.Center;
ws.Cells.SetRowHeight(1, 25);//设置行高
ws.Cells.Merge(1, 0, 1, dt.Columns.Count);
ws.Cells[1, 0].PutValue(dt.TableName);
ws.Cells[1, 0].SetStyle(style5);
Style style4 = wb.Styles[wb.Styles.Add()];//新增查询条件标题样式
style4.Font.Name = "宋体";//文字字体
style4.Font.IsBold = true; //设置粗体
style4.Font.Size = 10;//文字大小
ws.Cells.SetRowHeight(2, 25);//设置行高
ws.Cells.Merge(2, 0, 1, dt.Columns.Count);
ws.Cells[2, 0].PutValue(subTitle);
ws.Cells[2, 0].SetStyle(style4);
rowIndex++;
for (int i = 0; i < rowCount; i++)
{
ws.Cells.SetRowHeight(rowIndex, 25);//设置行高
colIndex = 0;
for (int j = 0; j < colCount; j++)
{
ws.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString() == "" ? null : dt.Rows[i][j].ToString());
style2.ForegroundColor = Color.White;
style2.Pattern = BackgroundType.Solid;
ws.Cells[rowIndex, colIndex].SetStyle(style2);//给单元格关联样式
colIndex++;
}
rowIndex++;
}
//设置所有列为自适应列宽
ws.AutoFitColumns();
for (int col = 0; col < colCount; col++)
{
ws.Cells.SetColumnWidthPixel(col, ws.Cells.GetColumnWidthPixel(col) + 20);
}
#region 合并单元格
int mergeDateStart = 4;
int mergeDate = 1;
int mergeAreaStart = 4;
int mergeArea = 1;
for (var i = 0; i < dt.Rows.Count; i++)
{
if (i + 1 < dt.Rows.Count)
{
//日期
if (dt.Rows[i]["日期"].ToString() != dt.Rows[i + 1]["日期"].ToString())
{
ws.Cells.Merge(mergeDateStart, 0, mergeDate, 1);
mergeDateStart += mergeDate;
mergeDate = 1;
}
else
{
mergeDate++;
}
//区域
if (dt.Rows[i]["区域"].ToString() != dt.Rows[i + 1]["区域"].ToString())
{
ws.Cells.Merge(mergeAreaStart, 1, mergeArea, 1);
mergeAreaStart += mergeArea;
mergeArea = 1;
}
else
{
mergeArea++;
}
}
else
{
//日期
ws.Cells.Merge(mergeDateStart, 0, mergeDate, 1);
//区域
ws.Cells.Merge(mergeAreaStart, 1, mergeArea, 1);
}
}
#endregion
string fullUpLoadPath = HttpContext.Current.Server.MapPath("~/UpLoad/Excel/");
//检查本地上传的物理路径是否存在,不存在则创建
if (!System.IO.Directory.Exists(fullUpLoadPath))
{
System.IO.Directory.CreateDirectory(fullUpLoadPath);
}
filePath = GetMapPath(filePath);
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
System.IO.FileStream fs = System.IO.File.Create(filePath);
fs.Close();
wb.Save(filePath);
}
以上就完成了,快去试试吧。
- C#实现西门子S7-1200、200 SMART PLC之间通信
- C#连接FTP实现文件上传下载
- C#实现MQTT通讯
- C#实现串口通讯
- C#实现WebSocket服务和通讯
- C#实现UDP通讯
- C#实现TCP通讯
- C#调用WebApi请求常用的两种方式
- C# .NET MVC实现图片、视频上传
- 上一篇:Excel-VBA代码,合并单元格
- 下一篇:Excel vba常用语句
相关推荐
- perl基础——循环控制_principle循环
-
在编程中,我们往往需要进行不同情况的判断,选择,重复操作。这些时候我们需要对简单语句来添加循环控制变量或者命令。if/unless我们需要在满足特定条件下再执行的语句,可以通过if/unle...
- CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅
-
CHAPTER1TheThreePresentsofD'ArtagnantheElderCHAPTER2TheAntechamber...
- CHAPTER 5 The King'S Musketeers and the Cardinal'S Guards 第五章 国王的火枪手和红衣主教的卫士
-
CHAPTER3TheAudienceCHAPTER5TheKing'SMusketeersandtheCardinal'SGuard...
- CHAPTER 3 The Audience 第三章 接见
-
CHAPTER3TheAudienceCHAPTER3TheAudience第三章接见M.DeTrévillewasatt...
- 别搞印象流!数据说明谁才是外线防守第一人!
-
来源:Reddit译者:@assholeeric编辑:伯伦WhoarethebestperimeterdefendersintheNBA?Here'sagraphofStea...
- V-Day commemorations prove anti-China claims hollow
-
People'sLiberationArmyhonorguardstakepartinthemilitaryparademarkingthe80thanniversary...
- EasyPoi使用_easypoi api
-
EasyPoi的主要特点:1.设计精巧,使用简单2.接口丰富,扩展简单3.默认值多,writelessdomore4.springmvc支持,web导出可以简单明了使用1.easypoi...
- 关于Oracle数据库12c 新特性总结_oracle数据库12514
-
概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...
- 【开发者成长】JAVA 线上故障排查完整套路!
-
线上故障主要会包括CPU、磁盘、内存以及网络问题,而大多数故障可能会包含不止一个层面的问题,所以进行排查时候尽量四个方面依次排查一遍。同时例如jstack、jmap等工具也是不囿于一个方面的问题...
- 使用 Python 向多个地址发送电子邮件
-
在本文中,我们将演示如何使用Python编程语言向使用不同电子邮件地址的不同收件人发送电子邮件。具体来说,我们将向许多不同的人发送电子邮件。使用Python向多个地址发送电子邮件Python...
- 提高工作效率的--Linux常用命令,能够决解95%以上的问题
-
点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...
- linux常用系统命令_linux操作系统常用命令
-
系统信息arch显示机器的处理器架构dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/s...
- 小白入门必知必会-PostgreSQL-15.2源码编译安装
-
一PostgreSQL编译安装1.1下载源码包在PostgreSQL官方主页https://www.postgresql.org/ftp/source/下载区选择所需格式的源码包下载。cd/we...
- Linux操作系统之常用命令_linux系统常用命令详解
-
Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...
- linux网络命名空间简介_linux 网络相关命令
-
此篇会以例子的方式介绍下linux网络命名空间。此例中会创建两个networknamespace:nsa、nsb,一个网桥bridge0,nsa、nsb中添加网络设备veth,网络设备间...
- 一周热门
- 最近发表
-
- perl基础——循环控制_principle循环
- CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅
- CHAPTER 5 The King'S Musketeers and the Cardinal'S Guards 第五章 国王的火枪手和红衣主教的卫士
- CHAPTER 3 The Audience 第三章 接见
- 别搞印象流!数据说明谁才是外线防守第一人!
- V-Day commemorations prove anti-China claims hollow
- EasyPoi使用_easypoi api
- 关于Oracle数据库12c 新特性总结_oracle数据库12514
- 【开发者成长】JAVA 线上故障排查完整套路!
- 使用 Python 向多个地址发送电子邮件
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- HTML button formtarget 属性 (30)
- opacity 属性 (32)
- transition 属性 (33)