C#用Aspose.Cells导出Excel,.NET导出Excel

ASP.NET MVC 控制器里面Action处理,下载文件,输出文件流

   public async Task<ActionResult> ExportNewsAuthorFee(string deptId, DateTime? startDate, DateTime? endDate){if (startDate == null){startDate = DateTime.Parse(DateTime.Now.Year + "-1-1");}string title = startDate.Value.ToString("yyyy-MM-dd");if (endDate.HasValue){title += "至"+endDate.Value.ToString("yyyy-MM-dd");}var list = await newsStatiscBll.GetUserChannelRoyaltiesListAsync(deptId, startDate, endDate);List<List<string>> excelData = new List<List<string>>();//标题设置,第一行List<string> titleList = new List<string>();           titleList.Add("姓名");titleList.Add("员工编号");titleList.Add("部门");titleList.Add("院");titleList.Add("");titleList.Add("小计");titleList.Add("分");titleList.Add("");titleList.Add("");titleList.Add("小计");titleList.Add("门");titleList.Add("网");for (int q = 0; q < 6; q++){titleList.Add("");}titleList.Add("小计");titleList.Add("电视");titleList.Add("");titleList.Add("");titleList.Add("小计");titleList.Add("绿色1");titleList.Add("绿色2");titleList.Add("绿色3");titleList.Add("西1");titleList.Add("西2");titleList.Add("西3");titleList.Add("川3");titleList.Add("川4");titleList.Add("川5");titleList.Add("铁6");titleList.Add("铁7");titleList.Add("铁8");titleList.Add("省9");titleList.Add("省12");titleList.Add("省11");titleList.Add("小计");titleList.Add("总计");excelData.Add(titleList);//第二行List<string> titleList2 = new List<string>();titleList2.Add("");titleList2.Add("");titleList2.Add("");titleList2.Add("摄1");titleList2.Add("文2");titleList2.Add("");titleList2.Add("气1");titleList2.Add("川2");titleList2.Add("摄3");titleList2.Add("");titleList2.Add("");titleList2.Add("地简讯");titleList2.Add("地非简讯");titleList2.Add("省简讯");titleList2.Add("省非简讯");titleList2.Add("简讯1");titleList2.Add("非简讯2");titleList2.Add("国");titleList2.Add("");titleList2.Add("地市1");titleList2.Add("省部2");titleList2.Add("国家3");titleList2.Add("");for (int n = 0; n< 17; n++){titleList2.Add("");}excelData.Add(titleList2);//数据封装foreach (var item in list){List<string> cellDataList  = new List<string>();// 小计decimal xj_yw1 = item.FeeAnPhotography + item.FeeAnLiterary;// 小计decimal xj_fgs = item.FeeFmGasField + item.FeeFmChuanyouFigures + item.FeeFmPhotography;//报刊小计decimal xj_wl = item.FeeNewspapers_disShort+ item.FeeNewspapers_disImgTxt+ item.FeeNewspapers_provinceShort+ item.FeeNewspapers_provinceShort+ item.FeeNewspapers_nationalShort+ item.FeeNewspapers_nationalImgTxt+ item.FeeNewspapers_PetroChina;//电视新闻,小计decimal xj_tv = item.FeeTV_dis + item.FeeTV_province + item.FeeTV_national;//【小计】  decimal xj_mt2 = item.FeeGreen_author+ item.FeeGreen_edit+ item.FeeGreen_img+ item.FeeXi_author+ item.FeeXi_edit+ item.FeeXi_img+ item.FeeZChuanyouFigures_author+ item.FeeZChuanyFigures_edit+ item.FeeZChuanyouFigures_img+ item.FeeZIronMan_author+ item.FeeZIronMan_edit+ item.FeeZIronMan_img+ item.FeeZProvinNewMedia_author+ item.FeeZProvinNewMedia_edit+ item.FeeZProvinNewMedia_img;decimal total = xj_yw1 + xj_fgs + xj_wl + xj_tv + xj_mt2;cellDataList.Add(item.UserRealName); /* 姓名*/cellDataList.Add(item.UserNo); /* 员工编号*/cellDataList.Add(item.DeptName); /* 部门*/cellDataList.Add(item.FeeAnPhotography.ToString()); /*  作品*/cellDataList.Add(item.FeeAnLiterary.ToString()); /* 作品*/cellDataList.Add(xj_yw1.ToString()); /*  小计*/cellDataList.Add(item.FeeFmGasField.ToString()); /*  气田风采*/cellDataList.Add(item.FeeFmChuanyouFigures.ToString()); /*  川油人物*/cellDataList.Add(item.FeeFmPhotography.ToString()); /*  摄影作品*/cellDataList.Add(xj_fgs.ToString()); /*  小计 */cellDataList.Add(item.FeeGpHome.ToString()); /*  门户  */cellDataList.Add(item.FeeNewspapers_disShort.ToString()); /*   刊_简讯*/cellDataList.Add(item.FeeNewspapers_disImgTxt.ToString()); /*  报刊_非简讯 */cellDataList.Add(item.FeeNewspapers_provinceShort.ToString()); /*  报刊_简讯 */cellDataList.Add(item.FeeNewspapers_provinceImgTxt.ToString()); /*  报刊_非简讯 */cellDataList.Add(item.FeeNewspapers_nationalShort.ToString()); /*  报刊_简讯 */cellDataList.Add(item.FeeNewspapers_nationalImgTxt.ToString()); /*  报刊_非简讯 */cellDataList.Add(item.FeeNewspapers_PetroChina.ToString()); /* 油*/cellDataList.Add(xj_wl.ToString()); /* 报刊小记 *///地cellDataList.Add(item.FeeTV_dis.ToString()); /* 地市级 */cellDataList.Add(item.FeeTV_province.ToString()); cellDataList.Add(item.FeeTV_national.ToString());cellDataList.Add(xj_tv.ToString()); /* 报刊小记 *///绿色cellDataList.Add(item.FeeGreen_author.ToString());cellDataList.Add(item.FeeGreen_edit.ToString());cellDataList.Add(item.FeeGreen_img.ToString());//西cellDataList.Add(item.FeeXi_author.ToString());cellDataList.Add(item.FeeXi_edit.ToString());cellDataList.Add(item.FeeXi_img.ToString());//川cellDataList.Add(item.FeeZChuanyouFigures_author.ToString());cellDataList.Add(item.FeeZChuanyFigures_edit.ToString());cellDataList.Add(item.FeeZChuanyouFigures_img.ToString());//铁cellDataList.Add(item.FeeZIronMan_author.ToString());cellDataList.Add(item.FeeZIronMan_edit.ToString());cellDataList.Add(item.FeeZIronMan_img.ToString());//省cellDataList.Add(item.FeeZProvinNewMedia_author.ToString());cellDataList.Add(item.FeeZProvinNewMedia_edit.ToString());cellDataList.Add(item.FeeZProvinNewMedia_img.ToString());cellDataList.Add(xj_mt2.ToString()); /*  小计 */cellDataList.Add(total.ToString()); /*  总计 */excelData.Add(cellDataList);}//单元格设置合并,标题第1行Action<Worksheet> ac = sheet =>{    sheet.Cells.Merge(0, 0, 2, 1); /*姓名,单元格合并*/sheet.Cells.Merge(0, 1, 2, 1); /*员工编号,单元格合并*/sheet.Cells.Merge(0, 2, 2, 1); /*部门,单元格合并*/sheet.Cells.Merge(0, 3, 1, 2); /*院网员工天地,单元格合并*/sheet.Cells.Merge(0, 5, 2, 1);/*小计,单元格合并*/sheet.Cells.Merge(0, 6, 1, 3);/*分公司门户,单元格合并*/sheet.Cells.Merge(0, 9,2, 1);/*小计,单元格合并*/sheet.Cells.Merge(0, 10,2, 1);/*集,单元格合并*/sheet.Cells.Merge(0, 11,1, 7);/*网,单元格合并*/sheet.Cells.Merge(0, 18,2,1);/*网,单元格合并*/sheet.Cells.Merge(0, 19,1,3);/*电,单元格合并*/sheet.Cells.Merge(0, 22,2,1);/*电,单元格合并*/sheet.Cells.Merge(0, 23,2,1);/*绿色,单元格合并*/for (int i = 24; i < 40; i++) {sheet.Cells.Merge(0, i, 2, 1);/* 单元格合并*/}};Stream stream = new MemoryStream();Export.ExportExcelHelpter.ExportAuthorExcelBig(excelData, ref stream, ac, null);string fileName = "作者稿费统计(" + title + ")" + Guid.NewGuid().ToString("N").Substring(0,4) + ".xlsx";return File(stream, "application/ocelet-stream", fileName);}

处理导出

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using Aspose.Cells;/// <summary>
/// 导出Excel,ac传入委托设置Cells格式,比如合并;比如单元格宽度设置等。
/// </summary>
/// <param name="data">数据</param> 
/// <param name="stream"></param>
/// <param name="stream">委托,设置Cells格式,比如合并;比如单元格宽度设置等</param>
/// <param name="afterAc">委托,数据已经插入后,设置单元格样式,比如列宽、列高配置</param>
/// 创建时间:2024-7-25 17:41:56, xxx
public static void ExportAuthorExcelBig(List<List<string>> data , ref Stream stream, Action<Worksheet> ac, Action<Cells> afterAc)
{Workbook wb = new Workbook();Worksheet sheet = wb.Worksheets[0];sheet.Name = "作者统计分析";Cells cells = sheet.Cells;Style style = wb.Styles[wb.Styles.Add()];style.Font.Size = 12;cells.ApplyStyle(style, new StyleFlag() { All = true });int cols = data[0].Count;//int cols = colsCount;//标题样式Style styleTitle = wb.Styles[wb.Styles.Add()];styleTitle.Font.IsBold = true;styleTitle.Font.Size = 12;//styleTitle.Font.Color = Color.Blue;/* 字体颜色*/styleTitle.ForegroundColor = Color.SpringGreen;/* 背景色 */styleTitle.Pattern = BackgroundType.Solid;/* 背景色 *///边框设置styleTitle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;styleTitle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;styleTitle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;styleTitle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;//styleTitle.Borders.SetStyle(CellBorderType.Thick);/*单元格边框有线,中间被叉叉封起*/styleTitle.HorizontalAlignment = TextAlignmentType.Center;styleTitle.VerticalAlignment = TextAlignmentType.Center;Range range = cells.CreateRange(0, 0, 2, cols);range.ApplyStyle(styleTitle, new StyleFlag() { All = true });#region 合并表头单元格//调用传入委托,ac?.Invoke(sheet);第一行//sheet.Cells.Merge(0, 0, 3, 1);//合并第一行第一列开始 合并3行1列//sheet.Cells.Merge(0, 1, 3, 1);//sheet.Cells.Merge(0, 2, 3, 1);//sheet.Cells.Merge(0, 3, 3, 1);//sheet.Cells.Merge(0, 4, 3, 1);//sheet.Cells.Merge(0, 5, 3, 1);//sheet.Cells.Merge(0, 6, 1, 64);第二行//sheet.Cells.Merge(1, 6, 1, 4);//sheet.Cells.Merge(1, 10, 1, 4);//sheet.Cells.Merge(1, 14, 1, 4);//sheet.Cells.Merge(1, 18, 1, 4);//sheet.Cells.Merge(1, 22, 1, 4);//sheet.Cells.Merge(1, 26, 1, 4);//sheet.Cells.Merge(1, 30, 1, 4);//sheet.Cells.Merge(1, 34, 1, 4);//sheet.Cells.Merge(1, 38, 1, 4);//sheet.Cells.Merge(1, 42, 1, 4);//sheet.Cells.Merge(1, 46, 1, 4);//sheet.Cells.Merge(1, 50, 1, 4);//sheet.Cells.Merge(1, 54, 1, 4);//sheet.Cells.Merge(1, 58, 1, 4);//sheet.Cells.Merge(1, 62, 1, 4);//sheet.Cells.Merge(1, 66, 1, 4);#endregionobject[,] dataArr2 = new object[data.Count, cols];for (int n = 0; n < data.Count; n++){var rowLine = data[n];for (int j = 0; j < rowLine.Count; j++){dataArr2[n, j] = rowLine[j];}}cells.ImportTwoDimensionArray(dataArr2, 0, 0);//自适应宽sheet.AutoFitColumns();//自适应行高sheet.AutoFitRows();//cells.SetRowHeight(0, 20);//cells.SetRowHeight(1, 20);//cells.SetRowHeight(2, 20);//for (int i = 0; i < cols; i++)//{//    cells.SetColumnWidth(i, 15);//}afterAc?.Invoke(cells);//输出文件流          stream.Seek(0, SeekOrigin.Begin);wb.Save(stream, SaveFormat.Xlsx);stream.Seek(0, SeekOrigin.Begin);//保存文件到本地    //string fileName = Guid.NewGuid().ToString("N") + ".xls";//string filePath = AppDomain.CurrentDomain.BaseDirectory + fileName;//string fileFolderDeire = Path.GetDirectoryName(filePath);//目录信息//if (!Directory.Exists(fileFolderDeire))//{//    Directory.CreateDirectory(fileFolderDeire);//}   //如果是大文件,建议返回文件磁盘路径//wb.Save(filePath);//stream = new FileStream(filePath, FileMode.Open);//Task.Run(() =>//{//    //删除生成的文件//    System.Threading.Thread.Sleep(60000);//    try//    {//        File.Delete(filePath);//    }//    catch (Exception) { }//});
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/387172.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

界面控件Telerik UI for WPF 2024 Q2亮点 - 全新的AIPrompt组件

Telerik UI for WPF拥有超过100个控件来创建美观、高性能的桌面应用程序&#xff0c;同时还能快速构建企业级办公WPF应用程序。UI for WPF支持MVVM、触摸等&#xff0c;创建的应用程序可靠且结构良好&#xff0c;非常容易维护&#xff0c;其直观的API将无缝地集成Visual Studio…

vite tsx项目的element plus集成 - 按需引入踩坑

前面我们进行了开源组件的自研&#xff0c;很多组件可直接用现成的开源组件库&#xff0c;并不需要自己重复造轮子&#xff0c;为此我们讲如何在当前vite vitepress tsx技术整合的项目中实现element plus组件的按需引入&#xff0c;同时解决遇到的一些坑。 安装Element Plus…

Codeforces Round #956 (Div. 2) and ByteRace 2024

A.思维&#xff1a;https://codeforces.com/contest/1983/problem/A AC代码&#xff1a; #include<bits/stdc.h> using namespace std; int t; int n; int main(){cin>>t;while(t--){cin>>n;for(int i1;i<n;i) cout<<i<<" ";cout…

《浅谈如何培养树立正确的人工智能伦理观念》

目录 摘要&#xff1a; 一、引言 二、《机械公敌》的情节与主题概述 三、人工智能伦理与法律问题分析 1.伦理挑战 2.法律问题 四、培养正确的人工智能伦理观念的重要性 五、培养正确的人工智能伦理观念的途径与方法 1.加强教育与宣传 2.制定明确的伦理准则和规范 3.…

Doris全方位教程+应用实例

Impala性能稍领先于presto,但是presto在数据源支持上非常丰富&#xff0c;包括hive、图数据库、传统关系型数据库、Redis等 缺点&#xff1a;这两种对hbase支持的都不好&#xff0c;presto 不支持&#xff0c;但是对hdfs、hive兼容性很好&#xff0c;其实这也是顺理成章的&…

Swift学习入门,新手小白看过来

&#x1f604;作者简介&#xff1a; 小曾同学.com,一个致力于测试开发的博主⛽️&#xff0c;主要职责&#xff1a;测试开发、CI/CD 如果文章知识点有错误的地方&#xff0c;还请大家指正&#xff0c;让我们一起学习&#xff0c;一起进步。 &#x1f60a; 座右铭&#xff1a;不…

java-数据结构与算法-02-数据结构-06-双端队列

1. 概述 双端队列、队列、栈对比 注1&#xff1a; Java 中 LinkedList 即为典型双端队列实现&#xff0c;不过它同时实现了 Queue 接口&#xff0c;也提供了栈的 push pop 等方法 注2&#xff1a; 不同语言&#xff0c;操作双端队列的方法命名有所不同&#xff0c;参见下表 接…

day05 Router、vuex、axios

配置 router和vuex需要在创建vue项目的时候&#xff0c;开始的时候选择Manually select features&#xff0c;于是就可以在下一个创建配置讯问中选择router和vuex。 axios则需要执行命令行&#xff1a; npm install axios -S 之后再在需要发送请求的view导入即可。 router…

Chapter 20 Python包

欢迎大家订阅【Python从入门到精通】专栏&#xff0c;一起探索Python的无限可能&#xff01; 文章目录 前言一、自定义包1. 什么是Python包&#xff1f;2. 目录结构3. 导入方式4. __all__变量 二、第三方包1. 什么是第三方包&#xff1f;2. 安装第三方包 前言 在 Python 中&am…

PHP反序列化漏洞

一.PHP的序列化和反序列化 &#xff08;1&#xff09;.作用 PHP的序列化和反序列化是PHP中用于存储或传输PHP的值的一个过程。序列化是将变量转换为可存储或传输的字符串的过程&#xff0c;而反序列化则是将这些字符串转换回PHP变量的过程。这两个过程在PHP开发中非常有用&am…

vue element-ui日期控件传参

前端&#xff1a;Vue element-ui <el-form-item label"过期时间" :rules"[ { required: true, message: 请选择过期时间, trigger: blur }]"><el-date-picker v-model"form.expireTime" type"date" format"yyyy-MM-dd&…

Linux--序列化与反序列化

序列化 序列化是指将数据结构或对象状态转换成可以存储或传输的格式的过程。在序列化过程中&#xff0c;对象的状态信息被转换为可以保持或传输的格式&#xff08;如二进制、XML、JSON等&#xff09;。序列化后的数据可以被写入到文件、数据库、内存缓冲区中&#xff0c;或者通…

当年很流行,现在已经淘汰的Java技术,请不要学了!【建议收藏】

在Java技术的发展历程中&#xff0c;确实有一些曾经流行但现在已经被淘汰或不再推荐使用的技术。了解这些技术可以帮助你避免学习过时的知识&#xff0c;从而更高效地提升自己的技能。 以下是一些曾经流行但现在已经不太推荐学习的Java技术&#xff1a; 1. Servlet 2.x&#x…

谷粒商城实战笔记-71-商品服务-API-属性分组-前端组件抽取父子组件交互

文章目录 一&#xff0c;一次性创建所有的菜单二&#xff0c;开发属性分组界面1&#xff0c;左侧三级分类树形组件2&#xff0c;右侧分组列表3&#xff0c;左右两部分通信3.1 子组件发送数据3.2&#xff0c;父组件接收数据 Vue的父子组件通信父组件向子组件传递数据子组件向父组…

【odoo17】后端py方法触发右上角提示组件

概要 在前面文章中&#xff0c;有介绍过前端触发的通知服务。 【odoo】右上角的提示&#xff08;通知服务&#xff09; 此文章则介绍后端触发方法。 内容 直接上代码&#xff1a;但是前提一定是按钮触发&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; def bu…

自动化测试 pytest 中 scope 限制 fixture使用范围!

导读 fixture 是 pytest 中一个非常重要的模块&#xff0c;可以让代码更加简洁。 fixture 的 autouse 为 True 可以自动化加载 fixture。 如果不想每条用例执行前都运行初始化方法(可能多个fixture)怎么办&#xff1f;可不可以只运行一次初始化方法&#xff1f; 答&#xf…

17.延迟队列

介绍 延迟队列&#xff0c;队列内部是有序的&#xff0c;延迟队列中的元素是希望在指定时间到了以后或之前取出和处理。 死信队列中&#xff0c;消息TTL过期的情况其实就是延迟队列。 使用场景 1.订单在十分钟内未支付则自动取消。 2.新创建的店铺&#xff0c;如果十天内没…

【Ant Design Vue的更新日志】

🌈个人主页: 程序员不想敲代码啊 🏆CSDN优质创作者,CSDN实力新星,CSDN博客专家 👍点赞⭐评论⭐收藏 🤝希望本文对您有所裨益,如有不足之处,欢迎在评论区提出指正,让我们共同学习、交流进步! 以下是Ant Design Vue的更新日志 版本1.7.0(发布日期:2023年4月) …

TCP/IP协议——使用Socket套接字实现

目录 Socket 使用Socket实现TCP客户端和服务器的过程 使用Socket搭建TCP服务器 线程优化 向客户端发送消息 连接的断开 客户端主动断开 服务端主动断开 服务器完整的程序 使用Socket编写客户端程序连接TCP服务器 Socket Socket是一种网络通信协议&#xff0c;它允许…

渗透测试:筑牢网络安全的坚固防线

在当今这个互联网高度发达的时代&#xff0c;网络安全已成为维护社会稳定和经济发展的重要基石。随着互联网的普及&#xff0c;网络攻击手段日益复杂多变&#xff0c;各类安全威胁层出不穷。为了有效应对这些挑战&#xff0c;渗透测试作为一种重要的安全测试与评估方法&#xf…