原創(chuàng)|其它|編輯:郝浩|2012-08-31 15:56:22.000|閱讀 4379 次
概述:本篇中將簡(jiǎn)單記錄下Aspose.Cells這個(gè)強(qiáng)大的Excel操作組件。這個(gè)組件的強(qiáng)大之處,就不多說,對(duì)于我們的報(bào)表總是會(huì)有導(dǎo)出Excel的處理,如果你使用微軟Excel的com組件,那么對(duì)于簡(jiǎn)單的操作還行,但是多余復(fù)雜的模板,那將是一個(gè)令人頭疼的事。在Aspose.Cells之下,將是一個(gè)簡(jiǎn)單的事情。他可以導(dǎo)入導(dǎo)出excel操作,在本節(jié)將要說的就是他的際遇excel模板的導(dǎo)出強(qiáng)大功能。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
本篇中將簡(jiǎn)單記錄下Aspose.Cells這個(gè)強(qiáng)大的Excel操作組件。這個(gè)組件的強(qiáng)大之處,就不多說,對(duì)于我們的報(bào)表總是會(huì)有導(dǎo)出Excel的處理,如果你使用微軟Excel的com組件,那么對(duì)于簡(jiǎn)單的操作還行,但是多余復(fù)雜的模板,那將是一個(gè)令人頭疼的事。在Aspose.Cells之下,將是一個(gè)簡(jiǎn)單的事情。他可以導(dǎo)入導(dǎo)出excel操作,在本節(jié)將要說的就是他的際遇excel模板的導(dǎo)出強(qiáng)大功能。
多的不說,我們先來利用Northwind做兩個(gè)小demo。先說說Aspose.Cells的模板語法:
1. &=DataSource.Field,&=[DataSource].[Field]是對(duì)DataTable和幾何類型的引用,將會(huì)從當(dāng)前行開始豎直向下生成多行數(shù)據(jù)。
2. &=$data:是對(duì)變量或數(shù)組的引用。數(shù)組存在skip,horizontal等屬性,具體參見官方網(wǎng)站
3. &=&=動(dòng)態(tài)公式計(jì)算;{r}當(dāng)前行,{c}當(dāng)前列,{-n},{n}當(dāng)前行或列的偏移量前n或后n。
4. &==是動(dòng)態(tài)計(jì)算,如excel,if等語句。(if(logic_test,true_value,false_value))
還有其他更為復(fù)雜的匯總計(jì)算的表達(dá)式,這里也不在這節(jié)多講,有興趣的朋友,可以去官網(wǎng)看看。我們先來個(gè)簡(jiǎn)單的例子,光說,總是不行的,還是要代碼實(shí)踐才能說明一切:
Excel模板1:
代碼:在我們的代碼中添加數(shù)據(jù)源:
如下:
1 var sql = @"select * from Customers
2 where Customers.City ='London'";
3 var dt=GetDataTable(sql);
4 dt.TableName = "Customers";
5 WorkbookDesigner designer = new WorkbookDesigner();
6 designer.Open(MapPath("~/1.xls"));
7 //數(shù)據(jù)源
8 designer.SetDataSource(dt);
9 //報(bào)表單位
10 designer.SetDataSource("ReportUtils", "xxxxx有限公司客戶信息");
11 designer.SetDataSource("ReportAdd", "London");
12 //截止日期
13 designer.SetDataSource("ReportDate", DateTime.Now.ToString("yyyy年MM月dd日"));
14
15 designer.Process();
16
17 designer.Save(string.Format("report.xls"), SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
18 Response.Flush();
19 Response.Close();
20 designer = null;
21 Response.End();
代碼很簡(jiǎn)單,就是添加了一個(gè)datatable,和幾個(gè)變量的數(shù)據(jù)源,我們所生成excel為:
這就完成了我們的一個(gè)簡(jiǎn)單的多表頭數(shù)據(jù)導(dǎo)出報(bào)表。
Demo2中我們將來嘗試下他的統(tǒng)計(jì)公式和函數(shù)計(jì)算(利用&=&=計(jì)算):
Excel模板2:Northwind的Order Details表
代碼:和上面不同的就只有幾句:
1 var order = GetDataTable(@"select * from [Order Details]
2 where [Order Details].OrderID=10248");
3 order.TableName = "Order";
4
5 designer.SetDataSource(order);
Excel效果:
Excel模板下載 全部代碼:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 var s=Aspose.Cells.CellsHelper.GetVersion();
4 var sql = @"select * from Customers
5 where Customers.City ='London'";
6 var dt=GetDataTable(sql);
7 dt.TableName = "Customers";
8 var order = GetDataTable(@"select * from [Order Details]
9 where [Order Details].OrderID=10248");
10 order.TableName = "Order";
11 WorkbookDesigner designer = new WorkbookDesigner();
12 designer.Open(MapPath("~/1.xls"));
13 //數(shù)據(jù)源
14 designer.SetDataSource(dt);
15 designer.SetDataSource(order);
16 //報(bào)表單位
17 designer.SetDataSource("ReportUtils", "xxxxx有限公司客戶信息");
18 designer.SetDataSource("ReportAdd", "London");
19 //截止日期
20 designer.SetDataSource("ReportDate", DateTime.Now.ToString("yyyy年MM月dd日"));
21
22 designer.Process();
23
24 designer.Save(string.Format("report.xls"), SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
25 Response.Flush();
26 Response.Close();
27 designer = null;
28 Response.End();
29 }
本站文章除注明轉(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)載