C#------Aspose.cells使用方法

本文介绍了一种利用Aspose.Cells库在C#中创建Excel文件并绘制柱状图表的方法。通过示例代码展示了如何设置数据单元格、添加两个系列的柱状图、自定义图表样式以及保存为指定路径。

转载:

http://www.cnblogs.com/muer/p/yaxle.html

代码:

public ActionResult ImportData(HttpPostedFileBase file)
{
  Workbook workbook = new Workbook();
  Worksheet sheet = workbook.Worksheets[0];

  Cells cells = sheet.Cells;
  cells[0, 1].PutValue("Income");
  cells[0, 2].PutValue("Expense");
  cells[1, 0].PutValue("Company A");
  cells[2, 0].PutValue("Company B");
  cells[3, 0].PutValue("Company C");
  cells[1, 1].PutValue(10000);
  cells[2, 1].PutValue(20000);
  cells[3, 1].PutValue(30000);
  cells[1, 2].PutValue(8000);
  cells[2, 2].PutValue(15000);
  cells[3, 2].PutValue(28000);

  int chartIndex = sheet.Charts.Add(ChartType.Column, 6, 6, 21, 15);

  Aspose.Cells.Charts.Chart chart = sheet.Charts[chartIndex];

  //第一条柱子
  chart.NSeries.Add("B2:B4", true);
  chart.NSeries.CategoryData = "A2:A4";
  Series aSeries1 = chart.NSeries[0];
  aSeries1.Name = "=B1";
  chart.NSeries.IsColorVaried = true;
  //第二条柱子
  chart.NSeries.Add("C2:C4", true);
  chart.NSeries.CategoryData = "A2:A4";
  Series aSeries2 = chart.NSeries[1];
  aSeries2.Name = "=C1";
  //修改柱子颜色
  aSeries2.Area.ForegroundColor = System.Drawing.Color.Green;

  //修改柱形图背景色
  chart.PlotArea.Area.ForegroundColor = System.Drawing.Color.FromArgb(1, 152, 186, 180);
  //修改区域背景色
  chart.ChartArea.Area.ForegroundColor = System.Drawing.Color.White;
  //显示数据表
  chart.ShowDataTable = true;
  //显示分类条
  chart.ShowLegend = true;
  chart.Title.Text = "Income Analysis";
  workbook.Save(path);
  return null;
}

 

转载于:https://www.cnblogs.com/tianhengblogs/p/7295999.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值