继续在上篇《使用Aspose.Cell控件实现Excel高难度报表的生成(一)》随笔基础上,研究探讨基于模板的Aspose.cell报表实现,其中提到了下面两种报表的界面,如下所示:

或者这样的报表格式

首先来分析第一种报表,这个其实还是比较固定的二维表,我们只要绑定相关的信息即可,设计模板如下所示:

 
实际生成的报表如下所示:

实现的代码其实不复杂,如下所示:

private DataTable GetTable(string sql)


        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return db.ExecuteDataSet(command).Tables[0];
        }
        private void btnAllMonthReport_Click(object sender, EventArgs e)
        {
            string sql = @"Select [LastCount] as LC, [LastMoney] as LM, [CurrentInCount] as CIC, [CurrentInMoney] as CIM, 
                           [CurrentOutCount] as COC, [CurrentOutMoney] as COM, [CurrentCount] as CC, [CurrentMoney] as CM, 
                           YearMonth,ItemName 
                           from TB_ReportMonthCheckOut ";
            DataTable dtBigType = GetTable(sql + " where ReportType =3");
            dtBigType.TableName = "BigType";
            if (dtBigType.Rows.Count == 0)
                return;             DataTable dtItemType = GetTable(sql + " where ReportType =3");
            dtItemType.TableName = "ItemType";             WorkbookDesigner designer = new WorkbookDesigner();
            string path = System.IO.Path.Combine(Application.StartupPath, "Report2-1.xls");
            designer.Open(path);
            designer.SetDataSource(dtBigType);
            designer.SetDataSource(dtItemType);
            designer.SetDataSource("YearMonth", dtBigType.Rows[0]["YearMonth"].ToString());
            designer.Process();             //Save the excel file
            string fileToSave = FileDialogHelper.SaveExcel();
            if (File.Exists(fileToSave))
            {
                File.Delete(fileToSave);
            }
            designer.Save(fileToSave, FileFormatType.Excel2003);
            Process.Start(fileToSave);
        }

通过绑定两个不同的DataTable对象,然后引用他的属性即可,行会自动增加以适应实际的数据,并且对象变量&=$YearMonth也正常显示了,注意一点就是,所有使用变量的地方,都必须在一个独立的Excel单元格中,否则不能解析出来。另外上图的红色圆圈里面表示,汇总的函数,会自动根据行列的增加,自动调整引用,这真是我们需要的。

出库单的实现也差不多,实现代码如下所示:


            string TakeOutBill = Path.Combine(Application.StartupPath, "TakeOutBill.xls");
            WorkbookDesigner designer = new WorkbookDesigner();
            designer.Open(TakeOutBill);
            designer.SetDataSource("TakeOutDate", DateTime.Now.ToString("yyyy-MM-dd"));
            designer.SetDataSource("WareHouse", this.txtWareHouse.Text);
            designer.SetDataSource("Manager", this.txtCreator.Text);
            designer.SetDataSource("CostCenter", this.txtCostCenter.Text);
            designer.SetDataSource("Dept", this.txtDept.Text);             string columns = "Start|int,ItemNo,ItemName,Specification,Unit,Price|decimal,Count|int";
            DataTable dt = DataTableHelper.CreateTable(columns);
            dt.TableName = "Detail";
            DataRow row = null;
            for (int i = 0; i < this.lvwDetail.Items.Count; i++)
            {
                PurchaseDetailInfo info = this.lvwDetail.Items[i].Tag as PurchaseDetailInfo;
                if (info != null)
                {
                    row = dt.NewRow();
                    row["Start"] = (i + 1);
                    row["ItemNo"] = info.ItemNo;
                    row["ItemName"] = info.ItemName;
                    row["Specification"] = info.Specification;
                    row["Unit"] = info.Unit;
                    row["Price"] = info.Price;
                    row["Count"] = Math.Abs(info.Quantity);
                    dt.Rows.Add(row);
                }
            }
            designer.SetDataSource(dt);
            designer.Process();
            string fileToSave = FileDialogHelper.SaveExcel();
            if (File.Exists(fileToSave))
            {
                File.Delete(fileToSave);
            }
            designer.Save(fileToSave, FileFormatType.Excel2003);
            Process.Start(fileToSave); 

以上报表,其实实现思路基本都差不多,相对来时,还是比较容易的,接下来设计一个比较困难的报表,需要结合Aspose.Cell一些对象来动态创建行列,并设置单元格的变量,然后填入相应的对象构造报表,另外还需要注意单元格格式的变化,如下所示的这种报表

这个报表初看没有太多特别的地方,难点就是他的第一行列也是变化的,因此不能通过普通的方式构建二维表,然后绑定数据源的方式,要先加载模板文件,然后操作Excel对象,把第一行的各列头部补齐,然后给下一行各单元格填入对象公式,如&=BigType.DeptName 和&=BigType.TotalMoney等内容,实现的代码如下所示:


            string sql = @"Select [YearMonth], [DeptName], [ItemType], [TotalMoney]
                           from TB_ReportDeptCost ";
            DataTable dt = GetTable(sql);
            if (dt.Rows.Count == 0)
                return;             List<string> itemTypeList = new List<string>();
            List<string> partList = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                string itemType = row["ItemType"].ToString();
                if (!itemTypeList.Contains(itemType))
                {
                    itemTypeList.Add(itemType);
                }                 string part = row["DeptName"].ToString();
                if (!partList.Contains(part))
                {
                    partList.Add(part);
                }
            }             string columnString = "DeptName";
            for (int i = 0; i < itemTypeList.Count; i++)
            {
                columnString += string.Format(",TotalMoney{0}|decimal", i);
            }
            DataTable dtBigType = DataTableHelper.CreateTable(columnString);
            dtBigType.TableName = "BigType";
            foreach (string part in partList)
            {
                DataRow row = dtBigType.NewRow();
                row["DeptName"] = part;
                for (int i = 0; i < itemTypeList.Count; i++)
                {
                    string itemType = itemTypeList[i];
                    DataRow[] rowSelect = dt.Select(string.Format("DeptName='{0}' AND ItemType='{1}'", part, itemType));
                    if (rowSelect.Length > 0)
                    {
                        row["TotalMoney" + i.ToString()] = rowSelect[0]["TotalMoney"];
                    }
                }
                dtBigType.Rows.Add(row);
            }             WorkbookDesigner designer = new WorkbookDesigner();
            string path = System.IO.Path.Combine(Application.StartupPath, "Report1.xls");
            designer.Open(path);             Aspose.Cells.Worksheet w = designer.Workbook.Worksheets[0];
            //先设置标题项目:如大修件,日常备件等
            int rowIndex = 2;//第三行为标题
            Aspose.Cells.Style style = w.Cells[rowIndex + 1, 1].Style;//继承数字栏目的样式
            style.Number = 4;//对应格式是#,##0.00
            Aspose.Cells.Style boldStyle = w.Cells[rowIndex, 0].Style;//继承开始栏目的样式
            for (int i = 0; i < itemTypeList.Count; i++)
            {
                w.Cells[rowIndex, i + 1].PutValue(itemTypeList[i]);
                w.Cells[rowIndex, i + 1].Style = boldStyle;
                w.Cells[rowIndex + 1, i + 1].PutValue("&=BigType.TotalMoney" + i.ToString());
                w.Cells[rowIndex + 1, i + 1].Style = style;
            }             //添加合计行
            w.Cells[rowIndex, itemTypeList.Count + 1].PutValue("合计");
            w.Cells[rowIndex, itemTypeList.Count + 1].Style = boldStyle;
            w.Cells[rowIndex + 1, itemTypeList.Count + 1].PutValue(string.Format("&=&=SUM(B{{r}}:{0}{{r}})", GetChar(itemTypeList.Count + 1)));
            w.Cells[rowIndex + 1, itemTypeList.Count + 1].Style = style;             designer.SetDataSource(dtBigType);
            designer.SetDataSource("YearMonth", dt.Rows[0]["YearMonth"].ToString());
            designer.Process();             //Save the excel file
            string fileToSave = FileDialogHelper.SaveExcel();
            if (File.Exists(fileToSave))
            {
                File.Delete(fileToSave);
            }
            designer.Save(fileToSave, FileFormatType.Excel2003);
            Process.Start(fileToSave);  

使用Aspose.Cell控件实现Excel高难度报表的生成(二)的更多相关文章

  1. 使用Aspose.Cell控件实现Excel高难度报表的生成(三)

    在之前几篇文章中,介绍了关于Apsose.cell这个强大的Excel操作控件的使用,相关文章如下: 使用Aspose.Cell控件实现Excel高难度报表的生成(一) 使用Aspose.Cell控件 ...

  2. 使用Aspose.Cell控件实现Excel高难度报表的生成

    1.使用Aspose.Cell控件实现Excel高难度报表的生成(一) http://www.cnblogs.com/wuhuacong/archive/2011/02/23/1962147.html ...

  3. 使用Aspose.Cell控件实现Excel高难度报表的生成(一)

    时光飞逝,生活.工作.业余研究总是在不停忙碌着,转眼快到月底,该月的博客文章任务未完,停顿回忆一下,总结一些经验以及好的东西出来,大家一起分享一下.本文章主要介绍报表的生成,基于Aspose.Cell ...

  4. (转)使用Aspose.Cell控件实现Excel高难度报表的生成(一)

    本文章主要介绍报表的生成,基于Aspose.Cell控件的报表生成.谈到报表,估计大家都有所领悟以及个人的理解,总的来说,一般的报表生成,基本上是基于以下几种方式:一种是基于微软Excel内置的引擎来 ...

  5. 利用Aspose.Cell控件导入Excel非强类型的数据

    导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...

  6. 利用Aspose.Word控件和Aspose.Cell控件,实现Word文档和Excel文档的模板化导出

    我们知道,一般都导出的Word文档或者Excel文档,基本上分为两类,一类是动态生成全部文档的内容方式,一种是基于固定模板化的内容输出,后者在很多场合用的比较多,这也是企业报表规范化的一个体现. 我的 ...

  7. 使用Aspose.Cell控件实现多个Excel文件的合并

    之前有写过多篇关于使用Apose.Cell控件制作自定义模板报表和通用的导出Excel表格数据的操作,对这个控件的功能还是比较满意,而且也比较便利.忽然有一天,一个朋友说:你已经有生成基于自定义模板报 ...

  8. 用Aspose.Cells控件读取Excel

    Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...

  9. 基于C#语言MVC框架Aspose.Cells控件导出Excel表数据

    控件bin文件下载地址:https://download.csdn.net/download/u012949335/10610726 @{ ViewBag.Title = "xx" ...

随机推荐

  1. 基于开源Dubbo分布式RPC服务框架的部署整合

    一.前言 Dubbo 作为SOA服务化治理方案的核心框架,用于提高业务逻辑的复用.整合.集中管理,具有极高的可靠性(HA)和伸缩性,被应用于阿里巴巴各成员站点,同时在包括JD.当当在内的众多互联网项目 ...

  2. 看看 JDK 8 给我们带来什么(转)

    世界正在缓慢而稳步的改变.这次改变给我们带来了一个新模样的JDK7,java社区也在一直期盼着在JDK8,也许是JDK9中出现一些其他的改进.JDK8的改进目标是填补JDK7实现中的一些空白——部分计 ...

  3. jQuery Mobile高手必备的十大技巧和代码片段

    与任何新技术一样,常常难就难在如何开始入手. 有鉴于此,我们整理出了与jQuery Mobile库有关的我认为最便利的一些技巧.方法和代码片段. 由于本文不是旨在全面介绍使用jQuery Mobile ...

  4. sql对应C#的类型

  5. http页面转发和重定向的区别

    一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下:request.getRequestDispatcher("new.jsp").forward(request ...

  6. 使用Powershell取出属于某些指定组的用户并导出为csv

    知识这东西就像雪球,越滚越大,今天看到了这篇自己1年多前写的博文,简直弱爆了.于是更新一下程序: 2016-6-15更新,短短几行代码,就拿到了组和组成员,其中还用到了递归,以处理组成员是组的情况: ...

  7. Target runtime Apache Tomcat v6.0 is not defined

    在工程目录下的.settings文件夹里,打开org.eclipse.wst.common.project.facet.core.xml文件,其内容是: <?xml version=" ...

  8. [ PHP+jQuery ] ajax 多级联动菜单的应用:电商网站的用户地址选择功能 ( 二 ) - 仿亚马逊下拉面板

    /** jQuery version: 1.8.3 Author: 小dee Date: 2014.11.8 */ 接上一篇博客. 实现带缓存的仿亚马逊下拉面板 效果图: 图1 初始 图2 点击省份 ...

  9. ADO 事务

    Ado.Net事务处理.在ADO.NET 中,可以使用Connection 和Transaction 对象来控制事务.若要执行事务,请执行下列操作:• 调用Connection 对象的BeginTra ...

  10. Off-heap Memory in Apache Flink and the curious JIT compiler

    https://flink.apache.org/news/2015/09/16/off-heap-memory.html   Running data-intensive code in the J ...