C#导出excel复杂表格(单元各合并)
itomcoil 2025-04-29 01:23 23 浏览
一、效果展示
二、代码实现
引用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实现图片、视频上传
相关推荐
- 最强聚类模型,层次聚类 !!_层次聚类的优缺点
-
哈喽,我是小白~咱们今天聊聊层次聚类,这种聚类方法在后面的使用,也是非常频繁的~首先,聚类很好理解,聚类(Clustering)就是把一堆“东西”自动分组。这些“东西”可以是人、...
- python决策树用于分类和回归问题实际应用案例
-
决策树(DecisionTrees)通过树状结构进行决策,在每个节点上根据特征进行分支。用于分类和回归问题。实际应用案例:预测一个顾客是否会流失。决策树是一种基于树状结构的机器学习算法,用于解决分类...
- Python教程(四十五):推荐系统-个性化推荐算法
-
今日目标o理解推荐系统的基本概念和类型o掌握协同过滤算法(用户和物品)o学会基于内容的推荐方法o了解矩阵分解和深度学习推荐o掌握推荐系统评估和优化技术推荐系统概述推荐系统是信息过滤系统,用于...
- 简单学Python——NumPy库7——排序和去重
-
NumPy数组排序主要用sort方法,sort方法只能将数值按升充排列(可以用[::-1]的切片方式实现降序排序),并且不改变原数组。例如:importnumpyasnpa=np.array(...
- PyTorch实战:TorchVision目标检测模型微调完
-
PyTorch实战:TorchVision目标检测模型微调完整教程一、什么是微调(Finetuning)?微调(Finetuning)是指在已经预训练好的模型基础上,使用自己的数据对模型进行进一步训练...
- C4.5算法解释_简述c4.5算法的基本思想
-
C4.5算法是ID3算法的改进版,它在特征选择上采用了信息增益比来解决ID3算法对取值较多的特征有偏好的问题。C4.5算法也是一种用于决策树构建的算法,它同样基于信息熵的概念。C4.5算法的步骤如下:...
- Python中的数据聚类及可视化分析实践
-
探索如何通过聚类分析揭露糖尿病预测数据集的特征!我们将运用Python的强力工具,深入挖掘数据,以直观的可视化揭示不同特征间的关系。一同探索聚类分析在糖尿病预测中的实践!所有这些可视化都可以通过数据操...
- 用Python来统计大乐透号码的概率分布
-
用Python来统计大乐透号码的概率分布,可以按照以下步骤进行:导入所需的库:使用Python中的numpy库生成数字序列,使用matplotlib库生成概率分布图。读取大乐透历史数据:从网络上找到大...
- python:支持向量机监督学习算法用于二分类和多分类问题示例
-
监督学习-支持向量机(SVM)支持向量机(SupportVectorMachine,简称SVM)是一种常用的监督学习算法,用于解决分类和回归问题。SVM的目标是找到一个最优的超平面,将不同类别的...
- 25个例子学会Pandas Groupby 操作
-
groupby是Pandas在数据分析中最常用的函数之一。它用于根据给定列中的不同值对数据点(即行)进行分组,分组后的数据可以计算生成组的聚合值。如果我们有一个包含汽车品牌和价格信息的数据集,那么可以...
- 数据挖掘流程_数据挖掘流程主要有哪些步骤
-
数据挖掘流程1.了解需求,确认目标说一下几点思考方法:做什么?目的是什么?目标是什么?为什么要做?有什么价值和意义?如何去做?完整解决方案是什么?2.获取数据pandas读取数据pd.read.c...
- 使用Python寻找图像最常见的颜色_python 以图找图
-
如果我们知道图像或对象最常见的是哪种颜色,那么可以解决图像处理中的几个用例,例如在农业领域,我们可能需要确定水果的成熟度。我们可以简单地检查一下水果的颜色是否在预定的范围内,看看它是成熟的,腐烂的,还...
- 财务预算分析全网最佳实践:从每月分析到每天分析
-
原文链接如下:「链接」掌握本文的方法,你就掌握了企业预算精细化分析的能力,全网首发。数据模拟稍微有点问题,不要在意数据细节,先看下最终效果。在编制财务预算或业务预算的过程中,通常预算的所有数据都是按月...
- 常用数据工具去重方法_数据去重公式
-
在数据处理中,去除重复数据是确保数据质量和分析准确性的关键步骤。特别是在处理多列数据时,保留唯一值组合能够有效清理数据集,避免冗余信息对分析结果的干扰。不同的工具和编程语言提供了多种方法来实现多列去重...
- Python教程(四十):PyTorch深度学习-动态计算图
-
今日目标o理解PyTorch的基本概念和动态计算图o掌握PyTorch张量操作和自动求导o学会构建神经网络模型o了解PyTorch的高级特性o掌握模型训练和部署PyTorch概述PyTorc...
- 一周热门
- 最近发表
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)