目的:
1.根据模板里面的excel数据信息,动态创建line chart
2.linechart 的样式改为灰色
3.以流的形式写到客户端,不管客户端是否装excel,都可以导出到到客户端
4.使用Aspose.Cells的基本功能
5.使用mvc测试代码
导出到excel里面的效果图
excel里面的数据源sheet2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
2001 2002 2003 2004 2005 2006 2007 中原地产 10 20 30 40 50 70 80 上海中原 30 80 44 55 88 90 120 河北中原 24 45 55 66 88 90 70 南京中原 44 55 66 77 88 99 90 背景中原 11 34 36 37 33 32 21 中原地产2 10 20 30 40 50 70 80 上海中原3 30 80 44 55 88 90 120 上海中原4 24 45 55 66 88 90 70 上海中原5 44 55 66 77 88 99 90 上海中原6 11 34 36 37 33 32 21 上海中原7 10 20 30 40 50 70 80 上海中原8 30 80 44 55 88 90 120 上海中原9 24 45 55 66 88 90 70 上海中原10 44 55 66 77 88 99 90 上海中原11 11 34 36 37 33 32 21 中原地产12 10 20 30 40 50 70 80 上海中原13 30 80 44 55 88 90 120 上海中原14 24 45 55 66 88 90 70 上海中原15 44 55 66 77 88 99 90 上海中原16 11 34 36 37 33 32 21 上海中原17 10 20 30 40 50 70 80 上海中原18 30 80 44 55 88 90 120 上海中原19 24 45 55 66 88 90 70 上海中原21 44 55 66 77 88 99 90 上海中原22 11 34 36 37 33 32 21 |
入口方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public ActionResult excels() { WorkbookDesigner designer = new WorkbookDesigner(); string path = Server.MapPath( "/Templete/11111.xls" ); designer.Workbook.Open(path); Workbook workbook = designer.Workbook; //创建一个chart到页面 CreateStaticReport1(workbook); designer.Process(); //将流文件写到客户端流的形式写到客户端,名称是_report.xls designer.Save( "_report.xls" , SaveType.OpenInExcel, FileFormatType.Excel2003, System.Web.HttpContext.Current.Response); Response.Flush(); Response.Close(); designer = null ; // Response.End(); return View( "getexcel" ); } |
生成chart方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | private void CreateStaticReport1(Workbook workbook) {
//创建一个折线图
workbook.Worksheets[0].Charts.Add(ChartType.Line, 1, 1, 25, 10);
Aspose.Cells.Chart chart = workbook.Worksheets[0].Charts[0];
//折线区域竖线设置为显示颜色设置为灰色
chart.CategoryAxis.MajorGridLines.IsVisible = true ;
chart.CategoryAxis.MajorGridLines.Color = Color.Gray;
//折线区域设置横着的网格线显示
chart.MajorGridLines.IsVisible = true ;
chart.MajorGridLines.Color = Color.Gray;
//设置title样式
chart.Title.Text = "Sales By Region For Years" ;
chart.Title.TextFont.Color = Color.Gray;
chart.Title.TextFont.IsBold = true ;
chart.Title.TextFont.Size = 12;
//Set Properties of nseries
chart.NSeries.Add( "Sheet2!B2:H26" , false );
//Set NSeries Category Datasource
chart.NSeries.CategoryData = "Sheet2!B1:H1" ;
Cells cells = workbook.Worksheets[1].Cells;
//loop over the Nseriese
for ( int i = 0; i < chart.NSeries.Count; i++)
{
//设置每条折线的名称
chart.NSeries[i].Name = cells[i + 1, 0].Value.ToString();
//设置线的宽度
chart.NSeries[i].Line.Weight = WeightType.MediumLine;
//设置每个值坐标点的样式
chart.NSeries[i].MarkerStyle = ChartMarkerType.Circle;
chart.NSeries[i].MarkerSize = 5;
chart.NSeries[i].MarkerBackgroundColor = Color.White;
chart.NSeries[i].MarkerForegroundColor = Color.Gray;
//每个折线向显示出值
chart.NSeries[i].DataLabels.IsValueShown = true ;
chart.NSeries[i].DataLabels.TextFont.Color = Color.Gray;
}
//设置x轴上数据的样式为灰色
chart.CategoryAxis.TickLabels.Font.Color = Color.Gray;
chart.CategoryAxis.TickLabelPosition = TickLabelPositionType.NextToAxis;
//设置y轴的样式
chart.ValueAxis.TickLabelPosition = TickLabelPositionType.Low;
chart.ValueAxis.TickLabels.Font.Color = Color.Gray;
// chart.ValueAxis.TickLabels.TextDirection = TextDirectionType.LeftToRight;
//设置Legend位置以及样式
chart.Legend.Position = LegendPositionType.Bottom;
chart.Legend.TextFont.Color = Color.Gray;
chart.Legend.Border.Color = Color.Gray; } |
目的:设置chart的y坐标轴显示值
用aspose.cell生成的chart生成的Y轴是默认生成的,自己要定义y轴坐标值
1.把数据源写到excel里面,list里面
2.y轴坐标自己定义
第一种:默认设置:chart里面会自己定义y轴坐标
第二种:y周坐标以对数显示 chart.ValueAxis.IsLogarithmic = true; 以10 100 1000格式显示
第三种:只设置间隔值 chart.ValueAxis.MajorUnit =20000;,会自动获取y轴的最大值与最小值
第四种:设置y轴的最大值与最小值,设置间隔值
//设置y坐标轴的厚度
chart.ValueAxis.AxisLine.Weight = WeightType.WideLine;
chart.ValueAxis.Title.Text = "y轴坐标";
chart.ValueAxis.MajorUnit =20000;//设置y轴的显示值间隔
chart.ValueAxis.MaxValue = 200000;//设置y轴开始最大值
chart.ValueAxis.MinValue = 0;//设置y轴的最小值
3.设置右边坐标轴是不是显示
//设置右边坐标轴显示
chart.SecondValueAxis.IsVisible = true;
//设置y坐标轴间隔值字大小
chart.SecondValueAxis.TickLabels.Font.Size = 12;
chart.SecondValueAxis.Title.Text = "y轴坐标2";
导出效果:
//设置y坐标轴的厚度
chart.ValueAxis.AxisLine.Weight = WeightType.WideLine;
chart.ValueAxis.Title.Text = "y轴坐标";
chart.ValueAxis.MajorUnit =20000;//设置y轴的显示值间隔
chart.ValueAxis.MaxValue = 200000;//设置y轴开始最大值
chart.ValueAxis.MinValue = 0;//设置y轴的最小值
第二种:y周坐标以对数显示 chart.ValueAxis.IsLogarithmic = true; 以10 100 1000格式显示
//设置y坐标轴的厚度
chart.ValueAxis.AxisLine.Weight = WeightType.WideLine;
chart.ValueAxis.Title.Text = "y轴坐标";
chart.ValueAxis.MajorUnit =20000;//设置y轴的显示值间隔
chart.ValueAxis.MaxValue = 80000;//设置y轴开始最大值
chart.ValueAxis.MinValue = 0;//设置y轴的最小值
入口函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public ActionResult excels()
{
WorkbookDesigner designer = new WorkbookDesigner();
string path = Server.MapPath( "/Templete/11111.xls" );
designer.Workbook.Open(path);
Workbook workbook = designer.Workbook;
CreateStaticData(workbook);
CreateStaticReport(workbook);
designer.Process();
//将流文件写到客户端流的形式写到客户端,名称是_report.xls
designer.Save( "_report.xls" , SaveType.OpenInExcel, FileFormatType.Excel2003, System.Web.HttpContext.Current.Response);
Response.Flush();
Response.Close();
designer = null ;
// Response.End();
return View( "getexcel" );
} |
设置数据源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | private void CreateStaticData(Workbook workbook)
{
//Initialize Cells object 我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8
不良信息举报平台
互联网安全管理备案
Copyright 2023 www.wodecom.cn All Rights Reserved |