本文整理汇总了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;
}