原創(chuàng)|對(duì)比評(píng)測(cè)|編輯:郝浩|2013-07-11 15:57:56.000|閱讀 745 次
概述:今天教大家如何用DevExpress電子表格(Spreadsheet)制作發(fā)貨清單
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
上一篇文章跟大家介紹了DevExpress電子表格單元格和公式的基本設(shè)置,今天教大家如何用電子表格(Spreadsheet)制作發(fā)貨清單。
假設(shè)我們的數(shù)據(jù)模型是這樣的:
public class Customer { public string Name { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string City { get; set; } public string State { get; set; } public string Zip { get; set; } public string Phone { get; set; } public List<Product> Products { get; set; } } public class Product { public string Name { get; set; } public int Quantity { get; set; } public double Discount { get; set; } public double Price { get; set; } }
而我們要?jiǎng)?chuàng)建的電子表格清單是這樣的:
首先,大家應(yīng)該還記得如何新建一個(gè)工作簿并設(shè)置默認(rèn)值:
Workbook book = new Workbook(); // Setup document defaults... book.Styles.DefaultStyle.Font.Name = "Segoe UI"; book.Styles.DefaultStyle.Font.Size = 14;
設(shè)置模擬的顧客信息:
Customer customer = new Customer() { Name = "Alcorn Mickey", Address1 = "Mickeys World of Fun", Address2 = "436 1st Ave.", City = "Cleveland", State = "OH", Zip = "37288", Phone = "(203)290-8902" }; customer.Products = new List<Product> { new Product() { Name = "Chai", Price = 100, Quantity = 3, Discount = 0.1 }, new Product() { Name = "Chang", Price = 120, Quantity = 6, Discount = 0.15 }, };
添加賬單信息:
// Add Billing info... book.Worksheets[0].Cells["B2"].Value = "BILL TO:"; book.Worksheets[0].Cells["B2"].Font.Bold = true; book.Worksheets[0].Cells["B3"].Value = customer.Name; book.Worksheets[0].Cells["B4"].Value = customer.Address1; book.Worksheets[0].Cells["B5"].Value = customer.Address2; book.Worksheets[0].Cells["B6"].Value = String.Format("{0} {1}, {2}", customer.City, customer.State, customer.Zip); book.Worksheets[0].Cells["B7"].Value = String.Format("Phone: {0}", customer.Phone);
添加標(biāo)題:
// Add Header... book.Worksheets[0].Cells["B9"].Value = "Product"; book.Worksheets[0].Cells["C9"].Value = "Quantity"; book.Worksheets[0].Cells["D9"].Value = "Price"; book.Worksheets[0].Cells["E9"].Value = "Discount"; book.Worksheets[0].Cells["F9"].Value = "Due";
格式化整個(gè)標(biāo)題Range [B9:F9]:
// Format header... book.Worksheets[0].Range["B9:F9"].ColumnWidthInCharacters = 16; Formatting header = book.Worksheets[0].Range["B9:F9"].BeginUpdateFormatting(); header.Alignment.Horizontal = HorizontalAlignment.Right; header.Borders.BottomBorder.Color = Color.Black; header.Borders.BottomBorder.LineStyle = BorderLineStyle.Medium; header.Font.Bold = true; book.Worksheets[0].Range["B9:F9"].EndUpdateFormatting(header); // Product is left aligned... book.Worksheets[0].Cells["B9"].Alignment.Horizontal = HorizontalAlignment.Left;
為每個(gè)產(chǎn)品設(shè)置屬性:
const int start = 10; // Add line items... int row = start; foreach(Product product in customer.Products) { book.Worksheets[0].Cells["B" + row].Value = product.Name; book.Worksheets[0].Cells["C" + row].Value = product.Quantity; book.Worksheets[0].Cells["D" + row].Value = (double)product.Price; book.Worksheets[0].Cells["D" + row].NumberFormat = "$#,##0.00"; book.Worksheets[0].Cells["E" + row].Value = (double)product.Discount; book.Worksheets[0].Cells["E" + row].NumberFormat = "0%"; book.Worksheets[0].Cells["F" + row].Formula = String.Format("=C{0}*D{0}*(1-E{0})", row); book.Worksheets[0].Cells["F" + row].NumberFormat = "$#,##0.00"; row++; }
匯總:
// Total... book.Worksheets[0].Cells["E" + (row + 1)].Value = "Total:"; book.Worksheets[0].Cells["E" + (row + 1)].Alignment.Horizontal = HorizontalAlignment.Right; book.Worksheets[0].Cells["E" + (row + 1)].Font.Bold = true; book.Worksheets[0].Cells["F" + (row + 1)].Formula = String.Format("=SUM(F{0}:F{1})", start, row - 1); book.Worksheets[0].Cells["F" + (row + 1)].NumberFormat = "$#,##0.00";
這樣一個(gè)簡(jiǎn)單的發(fā)貨清單就完成了,現(xiàn)在我們將這個(gè)電子表格文檔保存為一個(gè)PDF文檔:
book.ExportToPdf(@"Invoice.pdf");
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件