当前位置: 首页 > news >正文

c# Aspose.Cells 导出Excel表格 使用随记

/// <summary>/// 导出Excel表格/// </summary>/// <param name="list">数据集合</param>/// <param name="header">数据表头</param>/// <param name="sumColumn">求和列</param>/// <param name=" title"> 标题</param>/// <returns></returns>public static void ExportExcel(DataTable dt, string[] header, string sumColumn=null, string title = null){//Workbook wb = new Workbook(FileFormatType.Xlsx);Workbook wb = new Workbook();try{Worksheet sheet = wb.Worksheets[0];sheet.Name = "MO上行查询结果";if (dt.Rows.Count <= 0){System.Web.HttpContext.Current.Response.Write("<script>alert('没有检测到需要导出数据!');</script>");return;}// 为单元格添加样式Aspose.Cells.Style style = wb.CreateStyle();//style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;  //设置居中style.Font.Size = 20;//文字大小style.Font.IsBold = true;//粗体style.HorizontalAlignment = TextAlignmentType.Center;//文字居中// 为合计单元格样式Aspose.Cells.Style sumstyle =  wb.CreateStyle();sumstyle.Font.Size = 12;//文字大小sumstyle.Font.IsBold = true;//粗体sumstyle.Font.Color = Color.Red;   //颜色//style.HorizontalAlignment = TextAlignmentType.Center;//文字居中//添加标题int rowIndex = 0;sheet.Cells.SetRowHeight(rowIndex, 40); //设置行高if (title!=null){sheet.Cells[rowIndex, 0].PutValue(title);sheet.Cells[rowIndex, 0].SetStyle(style);sheet.Cells.Merge(0, 0, 1, header.Length); //合并行 Merge(1, 1,1, 5); 第一行 第一列 到 第一行 第 5列  //Merge切记后两位参数不能是 0 rowIndex++;}//添加表头style.Font.Size = 12;//文字大小sheet.Cells.SetRowHeight(rowIndex, 20); //设置行高for (int c = 0; c < header.Length; c++){sheet.Cells[rowIndex, c].PutValue(header[c]);sheet.Cells[rowIndex, c].SetStyle(style);sheet.Cells.SetColumnWidth(c, 20);//设置宽度//设置求和列公试if (sumColumn != null && sumColumn.IndexOf(header[c]) > -1){string sn = sheet.Cells[rowIndex + 1, c].Name;string en = sheet.Cells[rowIndex + dt.Rows.Count, c].Name;sheet.Cells[rowIndex + dt.Rows.Count + 1, c].SetStyle(sumstyle);sheet.Cells[rowIndex + dt.Rows.Count + 1, c].Formula = "=SUM(" + sn + ":" + en + ")"; //设置单元格公式     }}rowIndex++;//添加内容for (int r = 0; r < dt.Rows.Count; r++)//遍历DataTable行{for (int c = 0; c < header.Length; c++) //列只遍历到和表头数量相等的列,其余的列不导出{//自动把数字变量转出后 单元格变为数字类型。try{Convert.ToDecimal(dt.Rows[r][c]);sheet.Cells[r + rowIndex, c].PutValue(Convert.ToDecimal(dt.Rows[r][c])); //要导出数字列是数字类型,必须把添加变量转数字类型。}catch { sheet.Cells[r + rowIndex, c].PutValue(dt.Rows[r][c].ToString()); }}}//输出到Excel  直接输出到浏览器wb.Save(HttpContext.Current.Response, DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx",        ContentDisposition.Attachment, new OoxmlSaveOptions(SaveFormat.Xlsx));//保存成97或2003写法//workbook.Save(HttpContext.Current.Response, “output.xls”, ContentDisposition.Attachment, New XlsSaveOptions(SaveFormat.Excel97To2003))HttpContext.Current.Response.End();  //一定要结束HttpContext.Current.Response// 释放Excel对象Marshal.ReleaseComObject(sheet);Marshal.ReleaseComObject(wb);}catch (Exception e){System.Web.HttpContext.Current.Response.Write("<script>alert('导出异常:" + e.Message + "!');</script>");}}#endregion

Aspose.Cells .net 18.4
https://download.csdn.net/download/zhangyang20091/88614299?spm=1001.2014.3001.5503

http://www.lryc.cn/news/256677.html

相关文章:

  • SAP UI5 walkthrough step3 Controls
  • 【分布式微服务专题】从单体到分布式(一、SpringCloud项目初步升级)
  • python安装与工具PyCharm
  • Baumer工业相机堡盟工业相机如何通过BGAPISDK将相机图像高速保存到电脑内存(C#)
  • iOS 防截屏方法(一)
  • 【SpringBoot篇】5种类型参数传递json数据传参的操作
  • Mac系统升级node.js版本和npm版本
  • 风力发电对讲 IP语音对讲终端IP安防一键呼叫对讲 医院对讲终端SV-6005网络音频终端
  • 智能优化算法应用:基于蜉蝣算法3D无线传感器网络(WSN)覆盖优化 - 附代码
  • pymysql报错: unable to rollback、Already closed
  • [Linux] Web基础知识与http协议
  • 2020年第九届数学建模国际赛小美赛B题血氧饱和度的变异性解题全过程文档及程序
  • 【Flink on k8s】- 11 - 使用 Flink kubernetes operator 运行 Flink 作业
  • 【Linux】系统初识之冯诺依曼体系结构与操作系统
  • 【PyTorch】模型训练过程优化分析
  • GO -- 设计模式
  • angular状态管理方案(ngrx)
  • EPICS modbus 模块数字量读写练习
  • 万界星空科技低代码平台:搭建MES系统的优势
  • 【ArcGIS微课1000例】0078:创建点、线、面数据的最小几何边界
  • 五花八门客户问题(BUG) - 数据库索引损坏
  • mysql select count 非常慢
  • Tomcat管理功能使用
  • kyuubi整合flink yarn session mode
  • err_connect_length_mismatch错误
  • dva的学习总结
  • Docker部署.NET6项目
  • Pandas 打开有密码的Excel
  • CCF 202104-2:邻域均值--C++
  • 基于JAVA+SpringBoot+Vue的前后端分离的医院信息智能化HIS系统