C#导出excel复杂表格(单元各合并)
csdh11 2025-05-25 11:46 2 浏览
一、效果展示
二、代码实现
引用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实现图片、视频上传
相关推荐
- Aspose.Cells新版上线,实现了更有效的格式转换功能!
-
概述:使用Aspose.Cells您无需MSExcel就可实现类似Excel电子表格的功能。Aspose.Cells新版上线,支持智能标记接受一个通用列表作为嵌套对象,还可以从StyleColle...
- Aspose.Pdf新版来袭,精准的分页功能带给您全新的阅读体验!
-
概述:Aspose.Pdf是一个PDF文档创建组件,用户无需使用AdobeAcrobat也可读写和操作PDF文件。Aspose.Pdf新版增加了对标题实例中UserLabel属性的支持,而且分页时...
- 15个最强大的STL模型修复工具
-
如果你进行3D打印,可能遇到过可怕的“无法打印STL”问题:你的STL文件看起来很棒,但它会导致切片机出现问题或导致奇怪的打印错误或完全失败。无论确切原因是什么,这些问题的根源通常归结为...
- Aspose.Slides for Cloud是一个让你高效处理演示文稿的应用程序接口!
-
Aspose.SlidesforCloud可以让你提取演示文稿中的幻灯片、文字、颜色、字体格式、形状和图像等不同的元素。它拥有强大的API可以让你处理云端的MicrosoftPowerPoint...
- Aspose.Words for .NET使用教程(四):渲染和打印及文档内容功能
-
Aspose.Words无需MicrosoftWord也可在任何平台上满足Word文档的一切操作需求。本文将以表格的形式与大家分享Aspose.Wordsfor.NET的渲染和打印及文档内容功能...
- Aspose.BarCode新版发布条码识别更准确
-
Aspose.BarCodefor.NETv7.4.0新增:BARCODENET-34297识别条码的尺寸较小的图像BARCODENET-34265新增对ITF14条码顶部和底部水平条的重置...
- Aspose.BarCode 更新至v7.1.0
-
Aspose.BarCode是一个功能强大,且稳健的条形码生成和识别组件,其使用托管的C#编写,能帮助开发者快速简便的向其Microsoft应用程序(WinForms,ASP.NET和.NETC...
- Aspose.Words 14.9.0发布,涵盖120多项更新
-
Aspose.Words14.9主要更新内容:新的报告引擎允许在报告模板使用LINQ方法语法。图像sdt支持数据绑定。DrawingML现在是一个复合节点。DrawingML支持链接的文本框。改善...
- Aspose.Email V6.6.0发布
-
Aspose.Emailfor.NET6.6.0更新Aspose.Email是一个类库,使得应用程序可以操纵包括MicrosoftOutlook在内的流行消息格式。它支持IMAP、SMTP、P...
- Aspose.Slides新版上线,更流畅地读取演示文稿!
-
Aspose.Slidesfor.NET15.9.0问题修复:SLIDESNET-36905-文本的颜色值错误SLIDESNET-36898-不能创建线形图报告SLIDESNET-368...
- 电子表格管理控件Aspose.Cells新版本v8.7.2发布!
-
表格控件Aspose.Cells支持所有Excel格式类型的操作,在没有MicrosoftExcel的环境下,用户也可为其应用程序嵌入类似Excel的强大数据管理功能。Aspose.Cells可以对...
- Aspose.Words for .NET使用教程(十一):检测文件格式和兼容性
-
Aspose.Words无需MicrosoftWord也可在任何平台上满足Word文档的一切操作需求。本文将与大家分享如何检测文件格式和检查格式兼容性。...
- Aspose.Total 6折,单品85折 史上最低仅剩10天
-
12月“百厂约惠”活动,ASPOSE迎来史上最低折扣6折(Aspose.Total6折,单品85折),现在活动进入10天倒计时,活动结束立即恢复原价。活动截止:12月31日活动内容:Aspose....
- Aspose.slide 批量替换母版背景图
-
收到一个业务需求,需要批量将pptx的母版的背景图进行替换,如果人工做的话,每个文件将需要花半小时到1个小时,每期100多个,每期将多要花费10多个人天,我们来看看怎么高效优化。直接祭起aspose....
- 一周热门
- 最近发表
-
- Aspose.Cells新版上线,实现了更有效的格式转换功能!
- Aspose.Pdf新版来袭,精准的分页功能带给您全新的阅读体验!
- 15个最强大的STL模型修复工具
- Aspose.Slides for Cloud是一个让你高效处理演示文稿的应用程序接口!
- Aspose.Words for .NET使用教程(四):渲染和打印及文档内容功能
- Aspose.BarCode新版发布条码识别更准确
- Aspose.BarCode 更新至v7.1.0
- Aspose.Words 14.9.0发布,涵盖120多项更新
- Aspose.Email V6.6.0发布
- Aspose.Slides新版上线,更流畅地读取演示文稿!
- 标签列表
-
- mydisktest_v298 (34)
- document.appendchild (35)
- 头像打包下载 (61)
- acmecadconverter_8.52绿色版 (39)
- word文档批量处理大师破解版 (36)
- server2016安装密钥 (33)
- mysql 昨天的日期 (37)
- parsevideo (33)
- 个人网站源码 (37)
- centos7.4下载 (33)
- mysql 查询今天的数据 (34)
- intouch2014r2sp1永久授权 (36)
- 先锋影音源资2019 (35)
- jdk1.8.0_191下载 (33)
- axure9注册码 (33)
- pts/1 (33)
- spire.pdf 破解版 (35)
- shiro jwt (35)
- sklearn中文手册pdf (35)
- itextsharp使用手册 (33)
- 凯立德2012夏季版懒人包 (34)
- 冒险岛代码查询器 (34)
- 128*128png图片 (34)
- jdk1.8.0_131下载 (34)
- dos 删除目录下所有子目录及文件 (36)