当前位置: 首页>>代码示例>>C#>>正文


C# Workbook.Replace方法代码示例

本文整理汇总了C#中Workbook.Replace方法的典型用法代码示例。如果您正苦于以下问题:C# Workbook.Replace方法的具体用法?C# Workbook.Replace怎么用?C# Workbook.Replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Workbook的用法示例。


在下文中一共展示了Workbook.Replace方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateSummaryByYear

        public Workbook CreateSummaryByYear()
        {
            try
            {
                DBInit();
            }
            catch
            {
            }
            finally
            {
                if (this.oleDbConnection1 != null)
                    this.oleDbConnection1.Close();
            }

            //Open the template file
            string designerFile = MapPath("~/Designer/Northwind.xls");
            Workbook workbook = new Workbook(designerFile);

            try
            {
                //Specify SQL and execute the query to fill the datatable
                this.oleDbSelectCommand1.CommandText = @"SELECT COUNT(Orders.OrderID) AS Orders, SUM([Order Subtotals].Subtotal) AS Sales,
                    FORMAT(Orders.ShippedDate, 'yyyy/Q') AS Quarter FROM Orders INNER JOIN [Order Subtotals]
                    ON Orders.OrderID = [Order Subtotals].OrderID WHERE (Orders.ShippedDate IS NOT NULL)
                    GROUP BY FORMAT(Orders.ShippedDate, 'yyyy/Q')";
                this.oleDbDataAdapter1.Fill(this.dataTable1);
            }
            catch
            {
            }
            finally
            {
                if (this.oleDbConnection1 != null)
                    this.oleDbConnection1.Close();
            }

            //Get the worksheet
            Worksheet sheet = workbook.Worksheets["Sheet14"];
            //Name the sheet
            sheet.Name = "Summary By Year";
            //Get the cells collection
            Cells cells = sheet.Cells;
            //Create an array of datatables with specific fields
            DataTable[] yearSummary = new DataTable[3];
            for (int i = 0; i < 3; i++)
            {
                yearSummary[i] = new DataTable();
                yearSummary[i].Columns.Add("YearOrQuarter", typeof(int));
                yearSummary[i].Columns.Add("Orders", typeof(int));
                yearSummary[i].Columns.Add("Sales", typeof(decimal));
            }
            //Adding records to the datatables
            for (int i = 0; i < this.dataTable1.Rows.Count; i++)
            {
                string strQuarter = (string)this.dataTable1.Rows[i]["Quarter"];
                int year = int.Parse(strQuarter.Substring(0, 4)) - 1994;
                DataRow row = yearSummary[year].NewRow();
                row["YearOrQuarter"] = int.Parse(strQuarter.Substring(strQuarter.Length - 1));
                row["Sales"] = this.dataTable1.Rows[i]["Sales"];
                row["Orders"] = this.dataTable1.Rows[i]["Orders"];
                yearSummary[year].Rows.Add(row);
            }
            //Replace some values in the workbook
            for (int i = 0; i < 3; i++)
            {
                workbook.Replace("&summary" + (i + 1).ToString(), yearSummary[i]);
            }
            //Remove the unnecessary worksheets in the workbook
            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                sheet = workbook.Worksheets[i];
                if (sheet.Name != "Summary By Year")
                {
                    workbook.Worksheets.RemoveAt(i);
                    i--;
                }
            }
            //Get the generated workbook
            return workbook;
        }
开发者ID:babar-raza,项目名称:Aspose_Cells_NET,代码行数:81,代码来源:SummaryByYear.cs


注:本文中的Workbook.Replace方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。