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.xdnf.cn/news/1489231.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【SpringBoot】2 项目搭建

创建项目 1&#xff09;确实本地 jdk 版本 打开命令行窗口&#xff1a;快捷键 Windows R&#xff0c;输入 CMD&#xff0c;敲回车 执行命令&#xff1a;java -version 2&#xff09;在项目 clone 的位置创建 Spring Boot 项目&#xff0c;使用 Maven 进行依赖管理&#xff…

LoRA:低秩自适应

LoRA:低秩自适应 本章节是对轻松上手微调大语言模型——QLORA篇中提到的LoRA的原理解释。 背后动机 现今模型的参数量变得越来越大&#xff0c;对预训练模型进行全微调变得越来越不可行。为了解决这个问题有了LoRA&#xff08;Low-Rank Adaption&#xff09;的诞生。将可训练…

【更新2022】各省农业科技活动经费(RD)测算 1999-2022 无缺失

各省农业科技活动经费&#xff08;R&D&#xff09;测算数据在农业经济学、政策研究和农村发展规划等领域的论文研究中具有重要应用价值。首先&#xff0c;这些数据可以用于分析不同省份在农业科技投入上的差异及其对农业生产力和产出的影响&#xff0c;帮助揭示不同地区农业…

算法——二分查找(day10)

目录 69. x 的平方根 题目解析&#xff1a; 算法解析&#xff1a; 代码&#xff1a; 35. 搜索插入位置 题目解析&#xff1a; 算法解析&#xff1a; 代码&#xff1a; 69. x 的平方根 69. x 的平方根 - 力扣&#xff08;LeetCode&#xff09; 题目解析&#xff1a; 老…

Tenable Nessus 10.7.5 (macOS, Linux, Windows) 发布 - #1 漏洞评估解决方案

Tenable Nessus 10.7.5 (macOS, Linux, Windows) 发布 - #1 漏洞评估解决方案 发布 Nessus 试用版自动化安装程序&#xff0c;支持 macOS Sonoma、RHEL 9 和 Ubuntu 24.04 请访问原文链接&#xff1a;https://sysin.org/blog/nessus-10/&#xff0c;查看最新版。原创作品&…

人工智能类——计算机科学与技术

计算机科学与技术是一个非常大的门类。目前计算机科学与技术类招生的专业主要有计算机科学与技术、软件工程、网络工程、信息安全、物联网工程等&#xff0c;后面的几个专业是计算机科学与技术的重要分支&#xff0c;而这个门类的其他分支并没有单列出来一个本科专业&#xff0…

步入新时代,使用区块链服务API打造创新应用

随着区块链技术的兴起&#xff0c;我们正步入一个全新的数据时代——一个由透明性、安全性和去中心化定义的时代。Blockchain公司的区块链API&#xff0c;作为连接现实世界与区块链世界的桥梁&#xff0c;为全球开发者和企业提供了一种前所未有的方式&#xff0c;以访问、交互并…

JS 改造数组对象,将其不确定的key作为value,并合并相同key的value值

const data [{"苹果": 3839,"小米": 1423,"华为": 4965,"oppo": 3334,"vivo": 2820,"一加": 4751},{"苹果": 3560,"小米": 2099,"华为": 3192,"oppo": 4210,"vivo…

互联网产品经理转型升级:如何成功转身为AI产品经理

前言 随着人工智能技术的快速发展&#xff0c;AI产品经理成为了职场热门职位。许多互联网产品经理纷纷寻求转型升级&#xff0c;希望投身AI领域。 一、认清形势&#xff0c;明确目标 了解AI行业发展趋势&#xff1a;关注国家政策、市场动态和技术进步&#xff0c;把握AI行业…

【漏洞复现】金万维-云联应用系统接入平台 GNRemote.dll 前台RCE

文章目录 0x00 漏洞描述0x01 测绘工具0x02 漏洞复现0x03 Nuclei检测脚本0x04 修复建议0x05 免责声明 0x00 漏洞描述 云联&#xff08;AppCloud&#xff09;是北京金万维科技有限公司的企业级私有云产品。 金万维-云联应用系统接入平台 GNRemote.dl接口存在远程命令执行漏洞&am…

电商数据精细化运营解决方案(18页PPT)

方案介绍&#xff1a; 电商数据精细化运营解决方案通过全面、深入的数据分析与应用&#xff0c;助力电商企业实现精细化管理和精准化营销&#xff0c;从而在激烈的市场竞争中脱颖而出。 部分方案内容&#xff1a;

CeoMax总裁主题最新3.8.1破解免授权版/WordPress付费资源素材下载主题

CeoMax总裁主题最新3.8.1破解免授权版&#xff0c;一套WordPress付费资源素材下载的主题&#xff0c;感觉这是做资源站唯一一个可以和ripro媲美甚至超越的模板&#xff0c;UI很美&#xff0c;功能也很强大&#xff0c;有想学习的可下载搭建学习一下&#xff0c;仅供学习研究借鉴…

几个小创新模型,Transformer与SVM、LSTM、BiLSTM、Adaboost的结合,MATLAB分类全家桶再更新!...

截止到本期MATLAB机器学习分类全家桶&#xff0c;一共发了5篇&#xff0c;参考文章如下&#xff1a; 1.机器学习分类全家桶&#xff0c;模式识别&#xff0c;故障诊断的看这一篇绝对够了&#xff01;MATLAB代码 2. 再更新&#xff0c;机器学习分类全家桶&#xff0c;模式识别&a…

2024年SCI-莲花效应优化算法Lotus effect optimization algorithm-附Matlab免费代码

引言 本期介绍了一种基于自然行为的元启发式优化算法名为莲花效应优化算法Lotus effect optimization algorithm&#xff0c;LEA的元启发式算法。它将蜻蜓算法中的高效算子(比如蜻蜓在花授粉过程中的运动)与叶子上的水的自清洁特性(莲花效应)相结合&#xff0c;进行局部搜索操…

SSM学习9:SpringBoot简介、创建项目、配置文件、多环节配置

简介 SpringBoot式用来简化Spring应用的初始搭建以及开发过程的一个框架 项目搭建 File -> New -> Project 选中pom.xml文件&#xff0c;设置为maven项目 项目启动成功 可以访问BasicController中的路径 配置文件 在resources目录下 application.properties 默…

Docker快速搭建WordPress博客系统网站

WordPress 是一款广泛使用的开源内容管理系统(CMS),用于创建和管理网站和博客。 主要功能: 易于使用的界面:WordPress 提供了一个直观的后台管理界面,使用户能够轻松创建、编辑和管理网站内容。 主题和模板:WordPress 提供了各种主题和模板,可根据网站需求进行选择和自…

Java毕业设计 基于SSM和Vue的跑腿系统小程序

Java毕业设计 基于SSM和Vue的跑腿系统小程序 这篇博文将介绍一个基于SSM框架和Vue开发的跑腿系统微信小程序&#xff0c;适合用于Java毕业设计。 功能介绍 跑腿员 登录 注册 忘记密码 首页 图片轮播 校友动态 校友动态详情 任务 在线接单 任务订单 我的 我的收藏 联系客…

前端canvas——赛贝尔曲线

曲线之美&#xff0c;不在于曲线本身&#xff0c;而在于用的人。 所以就有了这期赛贝尔曲线。 新规矩&#xff0c;先上个GIT。 效果图 开局一张图&#xff0c;代码全靠编。 代码 画骨 先想着怎么画一个心形吧&#xff0c;等你想好了&#xff0c;就知道怎么画了。 首先就还…

找工作准备刷题Day8 二叉树 (卡尔41期训练营 7.22)

第一题&#xff1a;Leetcode235. 二叉搜索树的最近公共祖先 题目描述 题解1——递归法 class Solution { public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (root nullptr)return nullptr;if (root->val > p->val &&…

【Python】 ValueError: too many values to unpack 解决方案

【Python】 ValueError: too many values to unpack 解决方案 在Python编程中&#xff0c;ValueError: too many values to unpack是一个常见的错误&#xff0c;通常出现在使用解包操作时。本文将深入探讨这个错误的原因、解决思路、解决方法&#xff0c;并通过具体案例帮助大…