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导出内容代码完全版的更多相关文章

  1. 伪Excel导出新版代码

    /// <summary> /// 导出Excel CreateExcel(ds, "统计表.xls",new int[]{6,8}); /// </summar ...

  2. 读取excel文件内容代码

    最近工作需要批量添加映射excel文件字段的代码  于是通过读取excel2007实现了批量生成代码,记录下代码 需要引入poi的jar包 import java.awt.List; import j ...

  3. 说出来你可能不信,我用excel就能做一张高端的统计报表

    统计报表是指各级企事业.行政单位按规定的表格形式.内容.时间要求报送程序,自上而下统一布置,提供统计资料的一种统计调查方式.统计报表具有来源可靠.回收率高.方式灵活等特点,是各个基层企业或事业单位填报 ...

  4. js导入excel&导出excel

    Excel导入 html代码 <button style={{ color: '#1890ff', fontSize: '14px', cursor: 'pointer' }} onClick= ...

  5. 考勤输入导入OA平台与考勤统计报表导出功能源代码

    注:以某某公司为例,每日签到时间为8点整   每日签退时间为17点30分 规则:公司签到签退时间在OA平台中可以视实际情况调整,当天有请假并通过工作流审批通过为有效,当天因公外出并通过工作流审批通过为 ...

  6. 二十六、【开源框架】EFW框架Winform前端开发之Grid++Report报表、条形码、Excel导出、图表控件

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...

  7. python制作简单excel统计报表2之操作excel的模块openpyxl简单用法

    python制作简单excel统计报表2之操作excel的模块openpyxl简单用法 # coding=utf-8 from openpyxl import Workbook, load_workb ...

  8. python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图

    python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...

  9. abp框架Excel导出——基于vue

    abp框架Excel导出--基于vue 目录 abp框架Excel导出--基于vue 1.技术栈 1.1 前端采用vue,官方提供 1.2 后台是abp--aspnetboilerplate 2. E ...

随机推荐

  1. CF-832B

    B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. Java简单的数组用法尝试,和C语言很不一样

    public class Main { static int ARRY_LONGTH=100; static int[] getRandomArr(int n){ int[] randomArr; r ...

  3. Filezilla配置FTP中的坑以及出坑办法

    做本科生助教,老板让配置一个FTP传资料交作业,找了一台Windows服务器捣鼓,开始按网上教程自己配置特别麻烦,何西西说用Filezilla比较方便,就去Filezilla官网下载了Filezill ...

  4. 535. Encode and Decode TinyURL(rand and srand)

    Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...

  5. UGUI 锚点坑

    ----------------------------------------------------------------- 关键点:4个实心蓝点距离雪花4瓣的距离永远不变 锚点Anchors: ...

  6. Matplotlib 在绘画bar时, 鼠标响应点击 bar 的消息

    官方教程: http://urania.udea.edu.co/sitios/astronomia-2.0/pages/descargas.rs/files/descargasdt5vi/Cursos ...

  7. hdu1847(sg函数&yy)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 题意:中文题诶- 思路:直接sg函数打表即可,观察打表的结果发现是有规律的,sg函数的值只为0, ...

  8. Node.js 关于module的一些认知

    module是一个对象,在Node环境中运行js脚本,module会自动添加,并且系统会将函数封装到另一个函数中 例如: var module = { id: '.', exports: {} }; ...

  9. PJzhang:python基础入门的7个疗程-one

    猫宁!!! 参考链接:易灵微课-21天轻松掌握零基础python入门必修课-售价29元人民币 https://www.liaoxuefeng.com/wiki/1016959663602400 安全从 ...

  10. Mycat(1)

    https://www.jianshu.com/p/26513f428ecf https://blog.csdn.net/fly910905/article/details/87101059 http ...