彩票走势图

DevExpress 2013.1功能亮点体验:电子表格文档API(三)

原创|对比评测|编辑:郝浩|2013-07-11 15:57:56.000|阅读 696 次

概述:今天教大家如何用DevExpress电子表格(Spreadsheet)制作发货清单

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

相关链接:


上一篇文章跟大家介绍了DevExpress电子表格单元格和公式的基本设置,今天教大家如何用电子表格(Spreadsheet)制作发货清单

假设我们的数据模型是这样的:

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; }
}

而我们要创建的电子表格清单是这样的:

DevExpress,电子表格

首先,大家应该还记得如何新建一个工作簿并设置默认值:

Workbook book = new Workbook();
 
// Setup document defaults...
book.Styles.DefaultStyle.Font.Name = "Segoe UI";
book.Styles.DefaultStyle.Font.Size = 14;

设置模拟的顾客信息:

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);

添加标题:

// 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";

格式化整个标题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;

为每个产品设置属性:

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";

这样一个简单的发货清单就完成了,现在我们将这个电子表格文档保存为一个PDF文档:

book.ExportToPdf(@"Invoice.pdf");

电子表格,PDF文档,发货清单

>>DevExpress最新版下载

慧都学院2017全新DevExpress线下研修班火热报名中!



标签:DevExpress电子表格

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn

文章转载自:慧都控件

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP