sourcegrid统计报表画法以及EXCEL导出内容代码完全版
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using SourceGrid;
using Aspose.Cells;
using System.Drawing.Drawing2D; namespace WinTest
{
public partial class sourceGridTest : Form
{
public sourceGridTest()
{
InitializeComponent();
try
{
// CreateGrid();
}
catch (Exception)
{ return;
}
}
private Dictionary<string, Dictionary<string, List<string>>> dicContents = new Dictionary<string, Dictionary<string, List<string>>>();
private bool firstColumnLoad = true;//第一列初次加载
private bool towColumnLoad = true;//第二列初次加载
private bool threeColumnLoad = true;//第三列初次加载
private bool isNull = false;//是否子类别为空 /// <summary>
/// 添加标题内容
/// </summary>
public void AddDictionary()
{
string key = string.Empty; string strKey = string.Empty;
key = "借阅_1";
Dictionary<string, List<string>> dicBorrow = new Dictionary<string, List<string>>(); strKey = "内阅_Y";
dicBorrow.Add(strKey, new List<string>());
dicBorrow[strKey].Add("借次_JYRC");
dicBorrow[strKey].Add("纸阅(本)_ZZJY");
strKey = "外阅_N";
dicBorrow.Add(strKey, new List<string>());
dicBorrow[strKey].Add("借次_JYRC");
dicBorrow[strKey].Add("纸阅(本)_ZZJY");
dicContents.Add(key, dicBorrow); key = "查印_3";
Dictionary<string, List<string>> dicSearchAndPrint = new Dictionary<string, List<string>>();
strKey = "对询_Y";
dicSearchAndPrint.Add(strKey, new List<string>());
dicSearchAndPrint[strKey].Add("查次_CDRC");
dicSearchAndPrint[strKey].Add("查目_CKTM");
strKey = "对1询_N";
dicSearchAndPrint.Add(strKey, new List<string>());
dicSearchAndPrint[strKey].Add("查次_CDRC");
dicSearchAndPrint[strKey].Add("查目_CKTM");
strKey = "电询_T";
dicSearchAndPrint.Add(strKey, new List<string>());
dicSearchAndPrint[strKey].Add("查次_CDRC");
dicSearchAndPrint[strKey].Add("查目_CKTM");
dicContents.Add(key, dicSearchAndPrint); key = "在阅_2";
Dictionary<string, List<string>> dicSearchOnline = new Dictionary<string, List<string>>();
strKey = "审数_1,4";
dicSearchOnline.Add(strKey, new List<string>());
dicContents.Add(key, dicSearchOnline); key = "土记";
Dictionary<string, List<string>> dicLandUse = new Dictionary<string, List<string>>();
strKey = "法控_7";
dicLandUse.Add(strKey, new List<string>());
dicLandUse[strKey].Add("查次_CDRC");
dicLandUse[strKey].Add("查目_CKTM");
strKey = "原询_4";
dicLandUse.Add(strKey, new List<string>());
dicLandUse[strKey].Add("查次_CDRC");
dicLandUse[strKey].Add("查目_CKTM");
strKey = "登询_5";
dicLandUse.Add(strKey, new List<string>());
dicLandUse[strKey].Add("查次_CDRC");
dicLandUse[strKey].Add("查目_CKTM");
strKey = "他询_6";
dicLandUse.Add(strKey, new List<string>());
dicLandUse[strKey].Add("次_CDRC");
dicLandUse[strKey].Add("目_CKTM");
dicContents.Add(key, dicLandUse); key = "打计_3";
Dictionary<string, List<string>> dicPrint = new Dictionary<string, List<string>>();
strKey = "对印_IP";
dicPrint.Add(strKey, new List<string>());
strKey = "对印_OP";
dicPrint.Add(strKey, new List<string>());
dicContents.Add(key, dicPrint); key = "统计_3";
Dictionary<string, List<string>> dicImport = new Dictionary<string, List<string>>();
strKey = "对出_IEP";
dicImport.Add(strKey, new List<string>());
strKey = "对出_OEP";
dicImport.Add(strKey, new List<string>());
dicContents.Add(key, dicImport);
} /// <summary>
/// 创建表格
/// 如需扩展只需加入新的集合,增加行数/列数即可
/// </summary>
public void CreateGrid()
{
threeColumnLoad = true;//第三列初次加载
towColumnLoad = true;//第二列初次加载
firstColumnLoad = true;//第一列初次加载
dicContents.Clear();
//设置grid
SourceGrid.Grid grid = grid1;
grid.Rows.Clear();
grid.Columns.Clear();
grid.BorderStyle = BorderStyle.FixedSingle;
grid.FixedRows = ;
grid.FixedColumns = ; grid.Height = ;
grid.Width = ; grid.ColumnsCount = ;
grid.Rows.Insert();
//行数
int rowsCount = ;
//列数
int columnsCount = ;
#region 添加表格标题
AddDictionary(); #endregion //内容列
SourceGrid.Cells.ColumnHeader head = null;
head = new SourceGrid.Cells.ColumnHeader("月份");
grid[, ] = head;
grid[, ].ColumnSpan = ;
grid[, ].Editor = null;
head.AutomaticSortEnabled = false;//禁止排序 //计算中间每列宽度
int ColumnsWidthCount = ;
for (int i = ; i <= columnsCount; i++)
{
head = new SourceGrid.Cells.ColumnHeader(i - + "月");
grid[, i] = head;
grid[, i].View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
grid.Columns[i].Width = ;
ColumnsWidthCount += ;
head.AutomaticSortEnabled = false;//禁止排序
} head = new SourceGrid.Cells.ColumnHeader("横向合计");
head.AutomaticSortEnabled = false;//禁止排序
grid[,columnsCount] = head;
//最后一列的宽度填充
grid.Columns[columnsCount].Width = grid1.Width - ColumnsWidthCount - * ; //内容行,以后扩展可以增加行数
for (int i = ; i <= rowsCount; i++)
{
grid.Rows.Insert(i);
} //第一列计数器
int rowIndex = ;
int num = ;
//第二列计数器
int rowIndex_Tow=;
int num_Tow = ;
//第三列计数器
int rowIndex_Three = ; #region 绘制横向标题栏
foreach (string iKey in dicContents.Keys)
{
rowIndex = ;
//计算每个大类要占用多少行
foreach (string jKey in dicContents[iKey].Keys)
{
rowIndex_Tow = ;
//如果数据标题只有两列
if (dicContents[iKey][jKey].Count == )
{
rowIndex = dicContents[iKey].Count;
rowIndex_Tow = ;
rowIndex_Three++;
isNull = true;
}
else
{
//数据标题有三列
foreach (string kKey in dicContents[iKey][jKey])
{
rowIndex++;
rowIndex_Tow++;
rowIndex_Three++; if (threeColumnLoad == true)
{
//第二列第一次加载的列计算方式
grid[, ] = new SourceGrid.Cells.Cell(kKey, typeof(string));
grid[, ].ColumnSpan = ;
grid[, ].Editor = null;//禁止修改
threeColumnLoad = false;
}
else
{
//第二列非第一次加载以后的列计算方式
grid[rowIndex_Three, ] = new SourceGrid.Cells.Cell(kKey, typeof(string));
grid[rowIndex_Three, ].ColumnSpan = ;
grid[rowIndex_Three, ].Editor = null;//禁止修改
} }
isNull = false;
}
LoadGridRowsTitle(grid, rowIndex_Tow, ref num_Tow, ref towColumnLoad, isNull, jKey, true, , );
}
LoadGridRowsTitle(grid, rowIndex, ref num, ref firstColumnLoad, isNull, iKey, false, , );
}
#endregion #region 填充数据 //for (int i = 1; i < num; i++)
//{
// for (int j = 6; j <= columnsCount; j++)
// {
// grid[i, j] = new SourceGrid.Cells.Cell(grid[i, 0].Value.ToString() + "、" + grid[i, 3].Value.ToString() + "、" + grid[i, 5].Value.ToString(), typeof(string));
// grid[i, j].Editor = null;
// }
//} #endregion #region 页脚
//利用档案总计
grid[num, ] = new SourceGrid.Cells.Cell("总计", typeof(string)); ;
grid[num, ].ColumnSpan = ;
grid[num, ].Editor = null;
head.AutomaticSortEnabled = false;//禁止排序
//利用档案总计
grid[num, ] = new SourceGrid.Cells.Cell("KKCOUNT", typeof(string)); ;
grid[num, ].ColumnSpan = ;
grid[num, ].Editor = null;
head.AutomaticSortEnabled = false;//禁止排序 //利用档案人次总计
grid[num + , ] = new SourceGrid.Cells.Cell("利用总计", typeof(string)); ;
grid[num + , ].ColumnSpan = ;
grid[num + , ].Editor = null;
head.AutomaticSortEnabled = false;//禁止排序 //利用档案总计
grid[num + , ] = new SourceGrid.Cells.Cell("KKCOUNT1", typeof(string)); ;
grid[num + , ].ColumnSpan = ;
grid[num + , ].Editor = null;
head.AutomaticSortEnabled = false;//禁止排序
#endregion
} /// <summary>
/// sourceGrid处理合并列的类
/// </summary>
/// <param name="grid">控件</param>
/// <param name="rowIndex">合并行数</param>
/// <param name="num">下一个标题的位置</param>
/// <param name="firstLoad">是否第一次加载</param>
/// <param name="isNull">是否有第三级别</param>
/// <param name="Key">标题名</param>
/// <param name="isSecondColum">是否为无第三级别的类</param>
/// <param name="x">初始横坐标</param>
/// <param name="y">单元格纵坐标</param>
private static void LoadGridRowsTitle(SourceGrid.Grid grid, int rowIndex, ref int num, ref bool firstLoad, bool isNull, string Key, bool isSecondColum, int x, int y)
{
if (firstLoad == true)
{
//第二列第一次加载的列计算方式
grid[x, y] = new SourceGrid.Cells.Cell(Key, typeof(string));
grid[x, y].ColumnSpan = ;
grid[x, y].RowSpan = rowIndex;
grid[x, y].Editor = null;//禁止修改
grid[x, y].View.Border = new DevAge.Drawing.RectangleBorder(new DevAge.Drawing.BorderLine(Color.Black, , DashStyle.Solid, ), new DevAge.Drawing.BorderLine(Color.Black, , DashStyle.Solid, ));//边框
//居中
grid[x, y].View.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
firstLoad = false;
num = + rowIndex;
}
else
{
//try
//{ //第二列非第一次加载以后的列计算方式
grid[num, y] = new SourceGrid.Cells.Cell(Key, typeof(string));
grid[num, y].ColumnSpan = ;
grid[num, y].RowSpan = rowIndex;
grid[num, y].Editor = null;//禁止修改
if (isSecondColum)
{
if (isNull)
{
grid[num, y].ColumnSpan = ;
}
}
//}
//catch (Exception)
//{ // return;
//} num = num + rowIndex;
}
} private void button1_Click(object sender, EventArgs e)
{
//第一列计数器
int rowIndex = ;
int num = ;
//int index = 0;
//第二列计数器
int rowIndex_Tow = ;
int num_Tow = ;
//第三列计数器
int rowIndex_Three = ; firstColumnLoad = true;//第一列初次加载
towColumnLoad = true;//第二列初次加载
threeColumnLoad = true;//第三列初次加载
isNull = false;//是否子类别为空
int columnsCount = ;//列数 using (SaveFileDialog dlg = new SaveFileDialog())
{
dlg.Filter = "Xls文件(*.xls)|*.xls";
dlg.Title = "保存导出结果";
if (dlg.ShowDialog(this) == DialogResult.OK)
{
Workbook workbook = new Workbook(); //工作簿
Worksheet sheet = workbook.Worksheets[]; //工作表
Cells cells = sheet.Cells;//单元格
#region Excel样式设置
//大标题样式
Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
styleTitle.Font.Name = "宋体";//文字字体
styleTitle.Font.Size = ;//文字大小
styleTitle.Font.IsBold = true;//粗体
styleTitle.IsTextWrapped = true;//单元格内容自动换行 //子标题样式
Style styleSubtitle = workbook.Styles[workbook.Styles.Add()];//新增样式
styleSubtitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
styleSubtitle.Font.Name = "宋体";//文字字体
styleSubtitle.Font.Size = ;//文字大小
styleSubtitle.Font.IsBold = true;//粗体
styleSubtitle.IsTextWrapped = true;//单元格内容自动换行
//styleTitle.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
//styleTitle.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
//styleTitle.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
//styleTitle.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
cells.Merge(, , , grid1.Columns.Count);//合并单元格
cells[, ].PutValue("利用统计(2016年度)");//填写内容
cells[, ].SetStyle(styleTitle);
cells.Rows[].Height = ; #endregion //内容列
cells.Merge(, , , );
cells[, ].PutValue("月份");
cells[, ].SetStyle(styleSubtitle);
for (int i = ; i <= columnsCount; i++)
{
cells[, i].PutValue(i - + "月");
cells[, i].SetStyle(styleSubtitle);
}
cells[, columnsCount].PutValue("横向合计");
cells[, columnsCount].SetStyle(styleSubtitle); #region 绘制横向标题栏
foreach (string iKey in dicContents.Keys)
{
rowIndex = ;
//计算每个大类要占用多少行
foreach (string jKey in dicContents[iKey].Keys)
{
rowIndex_Tow = ;
//如果数据标题只有两列
if (dicContents[iKey][jKey].Count == )
{
rowIndex = dicContents[iKey].Count;
rowIndex_Tow = ;
rowIndex_Three++;
isNull = true;
}
else
{
//数据标题有三列
foreach (string kKey in dicContents[iKey][jKey])
{
rowIndex++;
rowIndex_Tow++;
rowIndex_Three++;
if (threeColumnLoad == true)
{
//第二列第一次加载的列计算方式
cells.Merge(, , , );
cells[, ].PutValue(kKey);//填写内容
cells[, ].SetStyle(styleSubtitle);
threeColumnLoad = false;
}
else
{
try
{
//第二列非第一次加载以后的列计算方式
cells.Merge(rowIndex_Three, , , );
cells[rowIndex_Three, ].PutValue(kKey);//填写内容
cells[rowIndex_Three, ].SetStyle(styleSubtitle);
}
catch (Exception)
{
break;
}
}
}
isNull = false;
}
LoadExcelRowsTitle(rowIndex_Tow, ref num_Tow, ref towColumnLoad, cells, styleSubtitle, isNull, jKey, true, , );
}
LoadExcelRowsTitle(rowIndex, ref num, ref firstColumnLoad, cells, styleSubtitle,isNull,iKey,false,,);
}
#endregion #region 填充主要数据 for (int i = ; i < grid1.Rows.Count - ; i++)
{
for (int j = ; j <= grid1.Columns.Count - ; j++)
{
cells[i + , j].PutValue("kkk");
var contentStyle = cells[i + , j].GetStyle();
contentStyle.HorizontalAlignment = TextAlignmentType.Center;
cells[i + , j].SetStyle(contentStyle);
}
}
#endregion #region 页脚
//利用档案总计
cells[num, ].PutValue("总计");
cells.Merge(num, , , );
cells[num, ].SetStyle(styleSubtitle);
cells[num, ].PutValue(grid1[num- , grid1.Columns.Count - ].Value); var footerStyle = cells[num, ].GetStyle();
footerStyle.HorizontalAlignment = TextAlignmentType.Center;
cells[num, ].SetStyle(footerStyle);
cells.Merge(num, , , ); //利用档案人次总计
cells[num+, ].PutValue("利用总计");
cells.Merge(num + , , , );
cells[num+, ].SetStyle(styleSubtitle);
cells[num+, ].PutValue(grid1[num, grid1.Columns.Count - ].Value);
footerStyle.HorizontalAlignment = TextAlignmentType.Center;
cells[num+, ].SetStyle(footerStyle);
cells.Merge(num + , , , );
#endregion workbook.Save(dlg.FileName); }
}
} /// <summary>
/// Excel处理合并列的类
/// </summary>
/// <param name="grid">控件</param>
/// <param name="rowIndex">合并行数</param>
/// <param name="num">下一个标题的位置</param>
/// <param name="firstLoad">是否第一次加载</param>
/// <param name="isNull">是否有第三级别</param>
/// <param name="Key">标题名</param>
/// <param name="isSecondColum">是否为无第三级别的类</param>
/// <param name="x">初始横坐标</param>
/// <param name="y">单元格纵坐标</param>
private static void LoadExcelRowsTitle(int rowIndex, ref int num, ref bool firstLoad, Cells cells, Style styleTitle, bool isNull, string Key, bool isSecondColum, int x, int y)
{
if (firstLoad == true)
{
//第一列第一次加载的列计算方式
cells.Merge(x, y, rowIndex, );//合并列
cells[x, y].PutValue(Key);//填写内容
cells[x, y].SetStyle(styleTitle);//填写内容
firstLoad = false;
num = + rowIndex;
}
else
{
//第一列第一次加载的列计算方式
cells.Merge(num, y, rowIndex, );//合并列
cells[num, y].PutValue(Key);//填写内容
cells[num, y].SetStyle(styleTitle);//填写内容
if (isSecondColum)
{
if (isNull)
{
cells.Merge(num, y, , );
}
}
num = num + rowIndex;
}
} private void button2_Click(object sender, EventArgs e)
{
CreateGrid();
} }
}
grid样式设置:
例如文字居左:
grid[i, j].View.TextAlignment = DevAge.Drawing.ContentAlignment.TopLeft;
此版本适用于C/S端
sourcegrid统计报表画法以及EXCEL导出内容代码完全版的更多相关文章
- 伪Excel导出新版代码
/// <summary> /// 导出Excel CreateExcel(ds, "统计表.xls",new int[]{6,8}); /// </summar ...
- 读取excel文件内容代码
最近工作需要批量添加映射excel文件字段的代码 于是通过读取excel2007实现了批量生成代码,记录下代码 需要引入poi的jar包 import java.awt.List; import j ...
- 说出来你可能不信,我用excel就能做一张高端的统计报表
统计报表是指各级企事业.行政单位按规定的表格形式.内容.时间要求报送程序,自上而下统一布置,提供统计资料的一种统计调查方式.统计报表具有来源可靠.回收率高.方式灵活等特点,是各个基层企业或事业单位填报 ...
- js导入excel&导出excel
Excel导入 html代码 <button style={{ color: '#1890ff', fontSize: '14px', cursor: 'pointer' }} onClick= ...
- 考勤输入导入OA平台与考勤统计报表导出功能源代码
注:以某某公司为例,每日签到时间为8点整 每日签退时间为17点30分 规则:公司签到签退时间在OA平台中可以视实际情况调整,当天有请假并通过工作流审批通过为有效,当天因公外出并通过工作流审批通过为 ...
- 二十六、【开源框架】EFW框架Winform前端开发之Grid++Report报表、条形码、Excel导出、图表控件
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...
- python制作简单excel统计报表2之操作excel的模块openpyxl简单用法
python制作简单excel统计报表2之操作excel的模块openpyxl简单用法 # coding=utf-8 from openpyxl import Workbook, load_workb ...
- python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图
python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...
- abp框架Excel导出——基于vue
abp框架Excel导出--基于vue 目录 abp框架Excel导出--基于vue 1.技术栈 1.1 前端采用vue,官方提供 1.2 后台是abp--aspnetboilerplate 2. E ...
随机推荐
- Kafka入门之生产者消费者测试
目录: kafka启动脚本以及关闭脚本 1. 同一个生产者同一个Topic,两个相同的消费者相同的Group 2. 同一个生产者同一个Topic,两个消费者不同Group 3. 两个生产者同一个Top ...
- B - School Marks
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Little ...
- ecplise常见的一些问题
修改注释的字体,默认的字体太小了.
- Git之通过ssh clone代码
1.git平台:码云 2.服务器系统:Linux 1.在Linux中创建ssh公钥,将创建的公钥添加到码云的ssh公钥管理 2.一般来说我们配置完站点之后,都会生成一个站点对应的文件夹,进入文件夹,然 ...
- 五分钟了解Mecanim角色动画系统
http://www.narkii.com/club/thread-305414-1.html Unity 4.0推出的Mecanim动画系统已经有一段时间,不过据了解很多的朋友仍然在使用原来的角色动 ...
- 进击python第4篇:初探模块
模块,用一砣代码实现了某个功能的代码集合,任何python程序都可以作为模块导入,n个 .py 文件组成的代码集合就称为模块. but 为什么要引入模块概念?主要原因是代码重用(code reuse) ...
- 2014-9-9 NOIP模拟赛
东方幻想乡系列模拟赛Stage 1命题 Nettle审题 Barty ccy1991911 FlanS39 Wagner T2 高精除高精,从来没写过,不知道怎么写,我就用大数减小数ans次,果断超时 ...
- 【渗透测试】如何利用burpsuite测试无回显漏洞
前面的文章讲了在windows和linux上的不同的无文件渗透测试的方法,那么这篇文章给大家讲解如何在漏洞没有回显的情况下,利用burpsuite自带插件进行测试的方式. 首先我们稍微提一下有哪些无回 ...
- Validation(2)
站在巨人的肩膀上 spring注解式参数校验 2016年06月15日 15:42:47 God_Ming 阅读数:57021 标签: springhibernatevalidator 更多 个人分类: ...
- Mybatis源码解析(二)
根据上篇的代码跟踪mybatis已经ready好 SqlSessionFactory了,下面就是我们怎么去通过这个factory去获取sqlSession会话了,继续扒源码: mybatis-spri ...