轉(zhuǎn)帖|其它|編輯:郝浩|2011-02-24 12:04:52.000|閱讀 1117 次
概述:繼續(xù)在上篇《使用Aspose.Cell控件實(shí)現(xiàn)Excel高難度報(bào)表的生成(一)》隨筆基礎(chǔ)上,研究探討基于模板的Aspose.cell報(bào)表實(shí)現(xiàn)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
繼續(xù)在上篇《使用Aspose.Cell控件實(shí)現(xiàn)Excel高難度報(bào)表的生成(一)》隨筆基礎(chǔ)上,研究探討基于模板的Aspose.cell報(bào)表實(shí)現(xiàn),其中提到了下面兩種報(bào)表的界面,如下所示:
或者這樣的報(bào)表格式
首先來(lái)分析第一種報(bào)表,這個(gè)其實(shí)還是比較固定的二維表,我們只要綁定相關(guān)的信息即可,設(shè)計(jì)模板如下所示:
實(shí)際生成的報(bào)表如下所示:
實(shí)現(xiàn)的代碼其實(shí)不復(fù)雜,如下所示:
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);
}
通過(guò)綁定兩個(gè)不同的DataTable對(duì)象,然后引用他的屬性即可,行會(huì)自動(dòng)增加以適應(yīng)實(shí)際的數(shù)據(jù),并且對(duì)象變量&=$YearMonth也正常顯示了,注意一點(diǎn)就是,所有使用變量的地方,都必須在一個(gè)獨(dú)立的Excel單元格中,否則不能解析出來(lái)。另外上圖的紅色圓圈里面表示,匯總的函數(shù),會(huì)自動(dòng)根據(jù)行列的增加,自動(dòng)調(diào)整引用,這真是我們需要的。
出庫(kù)單的實(shí)現(xiàn)也差不多,實(shí)現(xiàn)代碼如下所示:
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);
以上報(bào)表,其實(shí)實(shí)現(xiàn)思路基本都差不多,相對(duì)來(lái)時(shí),還是比較容易的,接下來(lái)設(shè)計(jì)一個(gè)比較困難的報(bào)表,需要結(jié)合Aspose.Cell一些對(duì)象來(lái)動(dòng)態(tài)創(chuàng)建行列,并設(shè)置單元格的變量,然后填入相應(yīng)的對(duì)象構(gòu)造報(bào)表,另外還需要注意單元格格式的變化,如下所示的這種報(bào)表
這個(gè)報(bào)表初看沒(méi)有太多特別的地方,難點(diǎn)就是他的第一行列也是變化的,因此不能通過(guò)普通的方式構(gòu)建二維表,然后綁定數(shù)據(jù)源的方式,要先加載模板文件,然后操作Excel對(duì)象,把第一行的各列頭部補(bǔ)齊,然后給下一行各單元格填入對(duì)象公式,如&=BigType.DeptName 和&=BigType.TotalMoney等內(nèi)容,實(shí)現(xiàn)的代碼如下所示:
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];
//先設(shè)置標(biāo)題項(xiàng)目:如大修件,日常備件等
int rowIndex = 2;//第三行為標(biāo)題
Aspose.Cells.Style style = w.Cells[rowIndex + 1, 1].Style;//繼承數(shù)字欄目的樣式
style.Number = 4;//對(duì)應(yīng)格式是#,##0.00
Aspose.Cells.Style boldStyle = w.Cells[rowIndex, 0].Style;//繼承開(kāi)始欄目的樣式
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;
}
//添加合計(jì)行
w.Cells[rowIndex, itemTypeList.Count + 1].PutValue( "合計(jì)");
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);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載