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端

最新文章

  1. Innodb 表空间传输迁移数据
  2. 我的ORM之十二 -- 支持的数据库及差别
  3. php的memcache和memcached扩展区别
  4. hdu 1166 线段树单点更新
  5. 1.struts2开发流程
  6. Servlet页面跳转实现方法的区别
  7. SQL SERVER 強制指定使用索引 -转载 只为学习
  8. 找到程序真正的入口(使用IDE追踪)
  9. Writing your first Django app, part 1(转)
  10. Linux Shell脚本攻略学习总结:二
  11. SpringCloud Eureka 报错 无法启动问题
  12. Python的list用法笔记
  13. win11.2.0.4lsnrctl status hang
  14. 【CF1009F】Dominant Indices(长链剖分)
  15. 洋葱第4场C和D题解……
  16. css选择器querySelector
  17. 如何使用C语言的面向对象
  18. 在Windows10中运行debug程序
  19. Java中对比两个对象中属性值[反射、注解]
  20. “undefined JNI_GetCreatedJavaVM”和“File format not recognized”错误原因分析

热门文章

  1. 对比&lt;input type=&quot;text&quot; id=&quot;&quot;&gt;和&lt;asp:TextBox runat=&quot;server&quot; ID=&quot;&quot;&gt;
  2. LeetCode: 521 Longest Uncommon Subsequence I(easy)
  3. 2014-5-16 NOIP模拟赛
  4. 洛谷P4869 albus就是要第一个出场(线性基)
  5. [Xcode 实际操作]八、网络与多线程-(2)使用UIApplication对象打开网页
  6. ZOJ1221 Risk
  7. BZOJ1218(线段树+扫描线)
  8. 牛客练习赛42D(性质、数学)
  9. 最长上升子序列LIS(云笔记图片版)
  10. 基于阿里云SLB/ESS/EIP/ECS/VPC的同城高可用方案演练