是用Epplus生成Excel 图表
1. 前言
这是我最近项目刚要的需求,然后在网上找了半天的教材 但是很不幸,有关于Epplus的介绍真的太少了,然后经过了我的不断研究然后不断的采坑,知道现在看到Excel都想吐的时候,终于成功的完成了公司的要求,最后我 稍微的优化了一下代码(毕竟是个刚工作一年多的新人),现在就给大家看一看不足之处,希望给为大佬给以指点,稍后在后面我会给出项目下载。
//首页执行
DataTable dt = DBhelper.gettable();
List<RangeData> rlist=new List<RangeData>();
RangeData rd = new RangeData();
rd.Charttype = "ColumnStacked";
rd.Yaxis = "E6,G6,O6";
rd.UserName = "测试名称";
rd.Region = "测试地址";
rd.ChartName = "测试名称";
rd.ChartKeyword = "F36";
rd.Appointedtime = DateTime.Now.ToString();
rlist.Add(rd);
RangeData rd1 = new RangeData();
rd1.Charttype = "ColumnClustered";
rd1.Yaxis = "E6,G6,O6";
rd1.UserName = "测试名称1";
rd1.Region = "测试地址1";
rd1.ChartName = "测试名称1";
rd1.ChartKeyword = "F36";
rd1.Appointedtime = DateTime.Now.ToString();
rlist.Add(rd1);
RangeData rd2 = new RangeData();
rd2.Charttype = "Pie";
rd2.Yaxis = "E6,G6,O6";
rd2.UserName = "测试名称2";
rd2.Region = "测试地址2";
rd2.ChartName = "测试名称2";
rd2.ChartKeyword = "F36";
rd2.Appointedtime = DateTime.Now.ToString();
rlist.Add(rd2);
RangeData rd3 = new RangeData();
rd3.Charttype = "singleColumnClustered";
rd3.Yaxis = "E6,G6,O6";
rd3.UserName = "测试名称23";
rd3.Region = "测试地址23";
rd3.ChartName = "测试名称3";
rd3.ChartKeyword = "F36";
rd3.Appointedtime = DateTime.Now.ToString();
rlist.Add(rd3);
ExcelInfo.ExportClient("", dt, rlist);
public class ExcelInfo
{
/// <summary>
/// 输出Excel文件
/// </summary>
/// <param name="FileName"></param>
/// <param name="table"></param>
/// <param name="?"></param>
public static void ExportClient(string FileName, DataTable table, List<RangeData> Rlist)
{
//reportTitle = "Microsoft,IBM,Oracle,Google.Yahoo";
FileName = FileName+DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".xlsx";
// string TJcell = "D6,F6,H6,AJ6";
if (Directory.Exists("UpFiles"))
{
}
else
{
DirectoryInfo directoryInfo = new DirectoryInfo("UpFiles");
directoryInfo.Create();
}
FileInfo file = new FileInfo(@"D:\文档资料\自学项目\EpplusExcelChartWeb\EpplusExcelChartWeb\upfiles\" + FileName);
createExcel(file, table, Rlist); }
/// <summary>
/// 创建Excel Sheet
/// </summary>
/// <param name="file"></param>
public static void createExcel(FileInfo file, DataTable table, List<RangeData> Rlist)
{
ExcelPackage package = new ExcelPackage(new FileStream(@"D:\文档资料\自学项目\EpplusExcelChartWeb\EpplusExcelChartWeb\test1.xlsx", FileMode.Open));
ExcelWorksheet sheet = null;
sheet = package.Workbook.Worksheets[]; #region 设置Excel数据
SheetData(table, sheet);
#endregion
//设置图形
if (Rlist.Count > )
{
ExcelWorksheet sheet1 = null;
sheet1 = package.Workbook.Worksheets.Add("Data"); for (int i = ; i < Rlist.Count; i++)
{
SheetData(table, sheet1);
sheet.Cells["C3"].Value = Rlist[i].UserName;
sheet.Cells["C4"].Value = Rlist[i].Region;
sheet.Cells["O4"].Value = Rlist[i].Appointedtime;
if (Rlist[i].Charttype == "ColumnStacked")
{
//堆积柱形图
ColumnStacked(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName,Rlist[i].ChartKeyword);
}
else if (Rlist[i].Charttype == "ColumnClustered")
{
//簇状柱形图
ColumnClustered(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName, Rlist[i].ChartKeyword);
}
else if (Rlist[i].Charttype == "Pie")
{
//饼图
Pie(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName, Rlist[i].ChartKeyword);
}
else if (Rlist[i].Charttype == "singleColumnClustered")
{
singleColumnClustered(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName, Rlist[i].ChartKeyword);
}
}
}
else
{
System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('没有数据')</script>");
}
package.SaveAs(file);//保存文件
}
//工作簿数据
private static void SheetData(DataTable table, ExcelWorksheet sheet)
{
//if (cols[0] != "")
//{
// //设置列标题
// for (int col = 1; col <= cols.Length; col++)
// {
// sheet.Cells[1, col].Value = cols[col - 1];
// }
//}
//else
//{
//设置列标题
//for (int col = 1; col <= table.Columns.Count; col++)
//{
// sheet.Cells[1, col].Value = table.Columns[col - 1].ColumnName;
//}
//}
//设置数据
for (int row = ; row < table.Rows.Count; row++)
{
for (int col = ; col < table.Columns.Count; col++)
{
string range = sheet.MergedCells[row + , col + ];
string strvalue = table.Rows[row][col].ToString();
// sheet.Cells[row + 1, col].Style.Numberformat.Format = "#0\\.00%";//设置数据的格式为百分比
if (table.Rows[row]["F36"].ToString() != "")
{
sheet.Cells[row + , col + ].Style.Fill.PatternType = ExcelFillStyle.Solid;
sheet.Cells[row + , col + ].Style.Fill.BackgroundColor.SetColor(Color.Gray);
sheet.Cells[row + , col + ].Value = strvalue;
}
sheet.Cells[row + , col + ].Value = strvalue; }
}
}
/// <summary>
/// 堆积柱形图
/// </summary>
private static void ColumnStacked(DataTable table,ExcelWorksheet sheet, string TJCell,string Chartname,string ChartKeyword)
{
////图表系列
ExcelChartSerie chartSerie = null;
//图表
ExcelChart chart = null;
chart = sheet.Drawings.AddChart(Chartname, eChartType.ColumnStacked);
chart.Legend.Position = eLegendPosition.Right;
chart.Legend.Add();
chart.SetSize(, );//设置图表大小
chart.ShowHiddenData = true;
#region 规定单元格生成图表
string[] TJcellarray = TJCell.Split(',');
string XAxis = string.Empty;
string YAxis = string.Empty;
for (int row = ; row <= table.Rows.Count; row++)
{
if (table.Rows[row - ][ChartKeyword].ToString() != "")
{
for (int j = ; j < TJcellarray.Length; j++)
{
XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + ) + ",";
YAxis += "test!" + TJcellarray[j] + ",";
}
int Xlength = XAxis.Length;
int Ylength = YAxis.Length;
XAxis = XAxis.Substring(, Xlength - );
YAxis = YAxis.Substring(, Ylength - );
chartSerie = chart.Series.Add(XAxis, YAxis);
chartSerie.HeaderAddress = sheet.Cells[row + , ];//设置每条线的名称
XAxis = "";
YAxis = "";
}
}
#endregion
// chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36");
chart.SetPosition(table.Rows.Count + , , , );//设置图表位置
}
/// <summary>
/// 多条簇状柱形图
/// </summary>
private static void ColumnClustered(DataTable table, ExcelWorksheet sheet, string TJCell, string Chartname, string ChartKeyword)
{
////图表系列
ExcelChartSerie chartSerie = null;
//图表
ExcelChart chart = null;
chart = sheet.Drawings.AddChart(Chartname, eChartType.ColumnClustered);
chart.Legend.Position = eLegendPosition.Right;
chart.Legend.Add();
chart.SetSize(, );//设置图表大小
chart.ShowHiddenData = true;
#region 规定单元格生成图表
string[] TJcellarray = TJCell.Split(',');
string XAxis = string.Empty;
string YAxis = string.Empty;
for (int row = ; row <= table.Rows.Count; row++)
{
if (table.Rows[row - ][ChartKeyword].ToString() != "")
{
for (int j = ; j < TJcellarray.Length; j++)
{
XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + ) + ",";
YAxis += "test!" + TJcellarray[j] + ",";
}
int Xlength = XAxis.Length;
int Ylength = YAxis.Length;
XAxis = XAxis.Substring(, Xlength - );
YAxis = YAxis.Substring(, Ylength - );
chartSerie = chart.Series.Add(XAxis, YAxis);
chartSerie.HeaderAddress = sheet.Cells[row + , ];//设置每条线的名称
XAxis = "";
YAxis = "";
}
}
#endregion
// chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36");
chart.YAxis.MinorTickMark = eAxisTickMark.None;
chart.XAxis.MinorTickMark = eAxisTickMark.None;//修改刻度线 chart.YAxis.MinorTickMark = eAxisTickMark.None;
chart.XAxis.MinorTickMark = eAxisTickMark.None;//修改刻度线 chart.SetPosition(table.Rows.Count + , , , );//设置图表位置
} /// <summary>
///饼图
/// </summary>
private static void Pie(DataTable table, ExcelWorksheet sheet, string TJCell, string Chartname, string ChartKeyword)
{
////图表系列
ExcelChartSerie chartSerie = null;
//图表
ExcelChart chart = null;
chart = sheet.Drawings.AddChart(Chartname, eChartType.Pie);
chart.Legend.Position = eLegendPosition.Right;
chart.Legend.Add();
chart.SetSize(, );//设置图表大小
chart.ShowHiddenData = true;
#region 规定单元格生成图表
string[] TJcellarray = TJCell.Split(',');
string XAxis = string.Empty;
string YAxis = string.Empty;
for (int row = ; row <= table.Rows.Count; row++)
{
if (table.Rows[row - ][ChartKeyword].ToString() != "")
{
for (int j = ; j < TJcellarray.Length; j++)
{
XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + ) + ",";
YAxis += "test!" + TJcellarray[j] + ",";
}
int Xlength = XAxis.Length;
int Ylength = YAxis.Length;
XAxis = XAxis.Substring(, Xlength - );
YAxis = YAxis.Substring(, Ylength - );
chartSerie = chart.Series.Add(XAxis, YAxis);
chartSerie.HeaderAddress = sheet.Cells[row + , ];//设置每条线的名称
XAxis = "";
YAxis = "";
}
}
#endregion
// chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36"); chart.YAxis.MinorTickMark = eAxisTickMark.None;
chart.XAxis.MinorTickMark = eAxisTickMark.None;//修改刻度线 chart.SetPosition(table.Rows.Count + +, , , );//设置图表位置
}
/// <summary>
/// 单条簇状柱形图
/// </summary>
private static void singleColumnClustered(DataTable table, ExcelWorksheet sheet, string TJCell, string Chartname, string ChartKeyword)
{
////图表系列
ExcelChartSerie chartSerie = null;
//图表
ExcelChart chart = null;
chart = sheet.Drawings.AddChart(Chartname, eChartType.ColumnClustered);
chart.Legend.Position = eLegendPosition.Right;
chart.Legend.Add();
chart.SetSize(, );//设置图表大小
chart.ShowHiddenData = true;
#region 规定单元格生成图表
string[] TJcellarray = TJCell.Split(',');
string XAxis = string.Empty;
string YAxis = string.Empty;
for (int row = ; row <= table.Rows.Count; row++)
{
if (table.Rows[row - ][ChartKeyword].ToString() != "")
{
for (int j = ; j < TJcellarray.Length; j++)
{
XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + ) + ",";
YAxis += "test!" + TJcellarray[j] + ",";
}
int Xlength = XAxis.Length;
int Ylength = YAxis.Length;
XAxis = XAxis.Substring(, Xlength - );
YAxis = YAxis.Substring(, Ylength - );
chartSerie = chart.Series.Add(XAxis, YAxis);
chartSerie.HeaderAddress = sheet.Cells[row + , ];//设置每条线的名称
XAxis = "";
YAxis = "";
}
}
#endregion
// chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36");
chart.YAxis.MinorTickMark = eAxisTickMark.None;
chart.XAxis.MinorTickMark = eAxisTickMark.None;//修改刻度线 chart.SetPosition(table.Rows.Count + + , , , );//设置图表位置
}
/// <summary>
/// 提取字符串
/// </summary>
/// <param name="p_str"></param>
/// <returns></returns>
public static string GetEXcelstr(string p_str)
{
string strReturn = string.Empty; if (p_str == null || p_str.Trim() == "")
{
strReturn = "";
} foreach (char chrTemp in p_str)
{
if (!Char.IsNumber(chrTemp))
{ strReturn += chrTemp; }
} return strReturn; }
}
//RangeData类 public class RangeData
{
///// <param name="XStartCell">数据开始单元格</param>
//public string XStartCell { get; set; }
// /// <param name="XEndCell">数据结束单元格</param>
//public string XEndCell { get; set; }
// /// <param name="EduName">Y开始单元格</param>
//public string YStartCell { get; set; }
// /// <param name="EduName">Y结束单元格</param>
//public string YEndCell { get; set; }
//图表类型 1.ColumnStacked 堆积柱形图 2.ColumnClustered 多条簇状柱形图 3.Pie饼图4.singleColumnClustered 单条簇状图
public string Charttype { get; set; }
//指定Y轴分类标签
public string Yaxis { get; set; }
//用户名
public string UserName { get; set; }
//所在区域
public string Region { get; set; }
//指定时间
public string Appointedtime { get; set; }
//图表名称
public string ChartName { get; set; }
//制图关键字
public string ChartKeyword { get; set; } }
如果执行报错 可能是获取Excel文件地址的问题。
下载地址https://files.cnblogs.com/files/Cjb8973/EpplusExcelChartWeb.rar
是用Epplus生成Excel 图表的更多相关文章
- 使用Epplus生成Excel 图表
1. 前言 这是我最近项目刚要的需求,然后在网上找了半天的教材 但是很不幸,有关于Epplus的介绍真的太少了,然后经过了我的不断研究然后不断的采坑,知道现在看到Excel都想吐的时候,终于成功的 ...
- 在.NET中使用EPPlus生成Excel报表 .
--摘抄自:http://blog.csdn.net/zhoufoxcn/article/details/14112473 在开发.NET应用中可能会遇到需要生成带图表(Chart)的Excel报表的 ...
- JAVA生成EXCEL图表
跟据客户的要求,需要开发一套包括图形的报表,还需要导出WORD 图表需要这样: 这样: 这样: 还有这样: 接下来是实现思路: 以往用的最多的就是JFreechart,手上也有实现各种图形的资源,但是 ...
- 使用poi和jfreechart生成excel图表图片
最近项目在频繁的操作excel,里边涉及到很多和图表有关的东西.有时候需要使用java操作excel自带的图标,比较复杂的我们都是使用excel模板的形式实现. 除此之外,也有一些功能只需要生成对应的 ...
- python生成Excel图表(通过xlsxwriter)
前面介绍了pandas的简单用法,pandas的数据可以通过matlab第三方包将数据生成报表,但是我想将报表生成在Excel中,这时候就可以借助xlsxwriter第三方包来生成图标 缺点:xl ...
- EPPlus生成Excel表格(只支持2007及以上)
主要来源: https://www.cnblogs.com/rumeng/p/3785748.html http://epplus.codeplex.com/ FileInfo newFile = n ...
- 【JAVA】POI生成EXCEL图表(柱状图、折线等)
1.使用excel工具自带的图形工具创建一个图: 2.绑定数据区域: 3.数据区域绑定完成,我们要做的就是将数据写入到数据区域中: 4.标记 5.POI 引入包 <!-- https://mvn ...
- EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类
大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...
- 生成 excel 插件 Epplus
最近做 .net core 项目 发现一个新的 生成excel 的插件 . 以前值用 aspose 或者 npio. 简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能 ...
随机推荐
- JavaScript前端和Java后端的AES加密和解密
在实际开发项目中,有些数据在前后端的传输过程中需要进行加密,那就需要保证前端和后端的加解密需要统一.这里给大家简单演示AES在JavaScript前端和Java后端是如何实现加密和解密的. 直接上代码 ...
- C++类中静态变量和静态方法使用介绍
静态成员的提出是为了解决数据共享的问题.实现共享有许多方法,如:设置全局性的变量或对象是一种方法.但是,全局变量或对象是有局限性的.这一章里,我们主要讲述类的静态成员来实现数据的共享. 静态数据成员 ...
- 史上最难的一道Java面试题 (分析篇)
博客园 匠心零度 转载请注明原创出处,谢谢! 无意中了解到如下题目,觉得蛮好. 题目如下: public class TestSync2 implements Runnable { int b = 1 ...
- JAVAEE企业级应用开发浅谈第二辑:MVC和三层架构
上海尚学堂警句:一份信心,一份努力,一份成功:十分信心,十分努力,十分成功. Step1.情景概要 Hello,小伙伴们,昨天跟大家分享了JAVA EE 企业级应用开发中大家耳熟能详的概念-三层架构, ...
- 在HBulider中如何快速的生成有序(ol)和无序(ul)列表
首先你需要创建一个HTML文件,然后在body里面写入你要创建的类型(有序或者无序),然后列表的个数个人来定: 格式如下: ul > li * 5 代表我要创建一个列表为5个的无序类型 然 ...
- dotweb框架之旅 [四] - 常用对象-HttpContext
dotweb属于一个Web框架,希望通过框架行为,帮助开发人员快速构建Web应用,提升开发效率,减少不必要的代码臃肿. dotweb包含以下几个常用对象: App(dotweb) App容器,为Web ...
- 一天工作所用到的Git命令
一天工作所用到的Git命令 像大多数新手一样,我一开始是在网上搜索 Git 命令,然后把答案复制粘贴,并没有真正理解它们究竟做了什么. 后来我曾经想过:"如果有一个最常见的 Git 命令的列 ...
- php代码常见错误详解整理
错误类型: 一.未使用二进制上传 代码: Fatal error: This encoded file is corrupted. Please refer to http://www.ze ...
- linux脚本定时备份数据库表(详解)
数据库备份策略 备份的数据库 服务器:10.10.10.254.10.2.11.10 数据库:gps6 备份的表: user_info alarminfo electronic_fence jpush ...
- HTTPS和HTTP有什么区别?如何将HTTP转化成HTTPS
不知道大家有没有注意到输入网址时的HTTP部分,在打开网站进行操作时有时候会自动跳转为HTTPS格式,这是为什么?HTTP与HTTPS到底有什么区别?如何将HTTP转化成HTTPS,针对这些问题,我们 ...