C# 如何在Excel 动态生成PivotTable
时间:2022-05-06
本文章向大家介绍C# 如何在Excel 动态生成PivotTable,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Excel 中的透视表对于数据分析来说,非常的方便,而且很多业务人员对于Excel的操作也是非常熟悉的,因此用Excel作为分析数据的界面,不失为一种很好的选择。那么如何用C#从数据库中抓取数据,并在Excel 动态生成PivotTable呢?下面结合实例来说明。
一般来说,数据库的设计都遵循规范化的原则,从而减少数据的冗余,但是对于数据分析来说,数据冗余能够提高数据加载的速度,因此为了演示透视表,这里现在数据库中建立一个视图,将需要分析的数据整合到一个视图中。如下图所示:
数据源准备好后,我们先来建立一个web应用程序,然后用NuGet加载Epplus程序包,如下图所示:
在index.aspx前台页面中,编写如下脚本:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="ExcelPivot.Web.index" %>
2
3 <!DOCTYPE html>
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head runat="server">
6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7 <title>Excel PivotTable</title>
8 <link rel="stylesheet" type="text/css" href="css/style.css" />
9 </head>
10 <body>
11 <form id="form1" runat="server">
12 <div id="container">
13
14 <div id="contents">
15
16 <div id="post">
17 <header>
18 <h1> Excel PivotTable </h1>
19 </header>
20 <div id="metro-array" style="display: inline-block;">
21 <div style="width: 230px; height: 230px; float: left; ">
22
23 <a class="metro-tile" style="cursor: pointer; width: 230px; height: 110px; display: block; color: #fff; margin-bottom: 10px;">
24
25 <input type="button" runat="server" id="Button1" name="btn1" value="回款情况分析" onserverclick="btn1_ServerClick"
26 style=" color:white; font-size:16px;float:left; border:0; width:230px; height:110px; cursor:pointer;"/>
27
28 </a>
29
30 <a class="metro-tile" style="cursor: pointer; width: 230px; height: 110px; display: block; color: #fff;">
31 <input type="button" runat="server" id="Button2" name="btn1" value="sampe1" onserverclick="btn1_ServerClick"
32 style=" color:white; font-size:16px;float:left; border:0; width:230px; height:110px; cursor:pointer;"/>
33 </a>
34 </div>
35
36 <div style="width: 230px; height: 230px; float: left; margin-left: 10px">
37
38 <a class="metro-tile" style="cursor: pointer; width: 230px; height: 230px; display: block; color: #fff">
39 <input type="button" runat="server" id="btn1" name="btn1" value="sampe1" onserverclick="btn1_ServerClick"
40 style=" color:white; font-size:16px;float:left; border:0; width:230px; height:230px; cursor:pointer;"/>
41 </a>
42
43 </div>
44
45 <div style="width: 230px; height: 230px; float: left; margin-left: 10px">
46
47 <a class="metro-tile" style="cursor: pointer; width: 230px; height: 110px; display: block; color: #fff; margin-bottom: 10px;">
48 <input type="button" runat="server" id="Button3" name="btn1" value="sampe1" onserverclick="btn1_ServerClick"
49 style=" color:white; font-size:16px;float:left; border:0; width:230px; height:110px; cursor:pointer;"/>
50 </a>
51
52 <a class="metro-tile" style="cursor: pointer; width: 110px; height: 110px; margin-right: 10px; display: block; float: left; color: #fff;">
53 <input type="button" runat="server" id="Button4" name="btn1" value="sampe1" onserverclick="btn1_ServerClick"
54 style=" color:white; font-size:16px;float:left; border:0; width:110px; height:110px; cursor:pointer;"/>
55 </a>
56
57 <a class="metro-tile" style="cursor: pointer; width: 110px; height: 110px; display: block; float: right; color: #fff;">
58 <input type="button" runat="server" id="Button5" name="btn1" value="sampe1" onserverclick="btn1_ServerClick"
59 style=" color:white; font-size:16px;float:left; border:0; width:110px; height:110px; cursor:pointer;"/>
60 </a>
61 </div>
62
63 </div>
64 </div>
65
66 </div>
67 </div>
68 </form>
69 </body>
70 <script src="js/tileJs.js" type="text/javascript"></script>
71 </html>
其中 TileJs是一个开源的构建类似win8 Metro风格的javascript库。
编写后台脚本:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using OfficeOpenXml;
8 using OfficeOpenXml.Table;
9 using OfficeOpenXml.ConditionalFormatting;
10 using OfficeOpenXml.Style;
11 using OfficeOpenXml.Utils;
12 using OfficeOpenXml.Table.PivotTable;
13 using System.IO;
14 using System.Data.SqlClient;
15 using System.Data;
16 namespace ExcelPivot.Web
17 {
18 public partial class index : System.Web.UI.Page
19 {
20 protected void Page_Load(object sender, EventArgs e)
21 {
22
23 }
24 private DataTable getDataSource()
25 {
26 //createDataTable();
27 //return ProductInfo;
28
29 SqlConnection conn = new SqlConnection();
30 conn.ConnectionString = "Data Source=.;Initial Catalog=olap;Persist Security Info=True;User ID=sa;Password=sa";
31 conn.Open();
32
33 SqlDataAdapter ada = new SqlDataAdapter("select * from v_pm_olap_test", conn);
34 DataSet ds = new DataSet();
35 ada.Fill(ds);
36
37 return ds.Tables[0];
38
39
40
41 }
42
43 protected void btn1_ServerClick(object sender, EventArgs e)
44 {
45 try
46 {
47 DataTable table = getDataSource();
48 string path = "_demo_" + System.Guid.NewGuid().ToString().Replace("-", "_") + ".xls";
49 //string path = "_demo.xls";
50 FileInfo fileInfo = new FileInfo(path);
51 var excel = new ExcelPackage(fileInfo);
52
53 var wsPivot = excel.Workbook.Worksheets.Add("Pivot");
54 var wsData = excel.Workbook.Worksheets.Add("Data");
55 wsData.Cells["A1"].LoadFromDataTable(table, true, OfficeOpenXml.Table.TableStyles.Medium6);
56 if (table.Rows.Count != 0)
57 {
58 foreach (DataColumn col in table.Columns)
59 {
60
61 if (col.DataType == typeof(System.DateTime))
62 {
63 var colNumber = col.Ordinal + 1;
64 var range = wsData.Cells[2, colNumber, table.Rows.Count + 1, colNumber];
65 range.Style.Numberformat.Format = "yyyy-MM-dd";
66 }
67 else
68 {
69
70 }
71 }
72 }
73
74 var dataRange = wsData.Cells[wsData.Dimension.Address.ToString()];
75 dataRange.AutoFitColumns();
76 var pivotTable = wsPivot.PivotTables.Add(wsPivot.Cells["A1"], dataRange, "Pivot");
77 pivotTable.MultipleFieldFilters = true;
78 pivotTable.RowGrandTotals = true;
79 pivotTable.ColumGrandTotals = true;
80 pivotTable.Compact = true;
81 pivotTable.CompactData = true;
82 pivotTable.GridDropZones = false;
83 pivotTable.Outline = false;
84 pivotTable.OutlineData = false;
85 pivotTable.ShowError = true;
86 pivotTable.ErrorCaption = "[error]";
87 pivotTable.ShowHeaders = true;
88 pivotTable.UseAutoFormatting = true;
89 pivotTable.ApplyWidthHeightFormats = true;
90 pivotTable.ShowDrill = true;
91 pivotTable.FirstDataCol = 3;
92 //pivotTable.RowHeaderCaption = "行";
93
94 //row field
95 var field004 = pivotTable.Fields["销售客户经理"];
96 pivotTable.RowFields.Add(field004);
97
98 var field001 = pivotTable.Fields["项目简称"];
99 pivotTable.RowFields.Add(field001);
100 //field001.ShowAll = false;
101
102 //column field
103 var field002 = pivotTable.Fields["年"];
104 pivotTable.ColumnFields.Add(field002);
105 field002.Sort = OfficeOpenXml.Table.PivotTable.eSortType.Ascending;
106 var field005 = pivotTable.Fields["月"];
107 pivotTable.ColumnFields.Add(field005);
108 field005.Sort = OfficeOpenXml.Table.PivotTable.eSortType.Ascending;
109
110 //data field
111 var field003 = pivotTable.Fields["回款金额"];
112 field003.Sort = OfficeOpenXml.Table.PivotTable.eSortType.Descending;
113 pivotTable.DataFields.Add(field003);
114
115 pivotTable.RowGrandTotals = false;
116 pivotTable.ColumGrandTotals = false;
117
118 //save file
119 excel.Save();
120 //open excel file
121 string file = @"C:Windowsexplorer.exe";
122 System.Diagnostics.Process.Start(file, path);
123
124 }
125 catch (Exception ex)
126 {
127 Response.Write(ex.Message);
128 }
129 }
130 }
131 }
编译运行,如下图所示:
单击 [回款情况分析],稍等片刻,会打开Excel,并自动生成透视表,如下图所示:
原文链接:http://www.cnblogs.com/isaboy/p/csharp_excel_pivot_table.html
- 小程序缓存 删不删你都该知道的事儿
- 32位 or 64位:Apache CloudStack系统VM架构选择
- Apache CloudStack系统VM架构选择
- ASP.NET MVC Controller激活系统详解:IoC的应用[下篇]
- 使用Docker的Alluxio群集设置
- 通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[中]:管道如何处理请求
- ASP.NET MVC Model元数据及其定制: 初识Model元数据
- 如何在企业中融入机器学习
- How ASP.NET MVC Works?
- 如果没有Visual Studio 2015,我们如何创建.NET Core项目 ?
- “前.NET Core时代”如何实现跨平台代码重用 ——源文件重用
- 简析Linux主要应用领域及范围
- ASP.NET MVC Controller激活系统详解:默认实现
- 机器之心年度盘点:2017年人工智能领域度备受关注的科研成果
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Linux上通过SSH挂载远程文件系统方法详解
- Linux上创建、列出、删除Docker容器方法总结
- Centos 7.4服务器时间同步配置方法【基于NTP服务】
- PowerBI 通用万能日历模板,想干嘛就干嘛
- Linux持续集成自动化安装Maven的方法
- 在CentOS上安装搭建PHP+Apache+Mysql的服务器环境
- Centos7.4服务器安装apache及安装过程出现的问题解决方法
- ubuntu系统修改时区和时间的方法
- CentOS7 LNMP+phpmyadmin环境搭建 第一篇虚拟机及centos7安装
- flutter 输入框组件TextField的实现代码
- Mac通过不同终端SSH连接远程服务器的讲解
- Linux关于透明大页的使用与禁用介绍
- linux中使用grep命令详解
- linux下使用cmake编译安装mysql的详细教程
- Linux CentOS 7.0中java安装与配置环境变量的步骤详解