2017-11-23 17:24 by 曹金宝, 阅读, 评论, 收藏, 编辑

1.  前言

  这是我最近项目刚要的需求,然后在网上找了半天的教材  但是很不幸,有关于Epplus的介绍真的太少了,然后经过了我的不断研究然后不断的采坑,知道现在看到Excel都想吐的时候,终于成功的完成了公司的要求,最后我 稍微的优化了一下代码(毕竟是个刚工作一年多的新人),现在就给大家看一看不足之处,希望给为大佬给以指点,稍后在后面我会给出项目下载。

  1. //首页执行
  2. DataTable dt = DBhelper.gettable();
  3. List<RangeData> rlist=new List<RangeData>();
  4. RangeData rd = new RangeData();
  5. rd.Charttype = "ColumnStacked";
  6. rd.Yaxis = "E6,G6,O6";
  7. rd.UserName = "测试名称";
  8. rd.Region = "测试地址";
  9. rd.ChartName = "测试名称";
  10. rd.ChartKeyword = "F36";
  11. rd.Appointedtime = DateTime.Now.ToString();
  12. rlist.Add(rd);
  13. RangeData rd1 = new RangeData();
  14. rd1.Charttype = "ColumnClustered";
  15. rd1.Yaxis = "E6,G6,O6";
  16. rd1.UserName = "测试名称1";
  17. rd1.Region = "测试地址1";
  18. rd1.ChartName = "测试名称1";
  19. rd1.ChartKeyword = "F36";
  20. rd1.Appointedtime = DateTime.Now.ToString();
  21. rlist.Add(rd1);
  22. RangeData rd2 = new RangeData();
  23. rd2.Charttype = "Pie";
  24. rd2.Yaxis = "E6,G6,O6";
  25. rd2.UserName = "测试名称2";
  26. rd2.Region = "测试地址2";
  27. rd2.ChartName = "测试名称2";
  28. rd2.ChartKeyword = "F36";
  29. rd2.Appointedtime = DateTime.Now.ToString();
  30. rlist.Add(rd2);
  31. RangeData rd3 = new RangeData();
  32. rd3.Charttype = "singleColumnClustered";
  33. rd3.Yaxis = "E6,G6,O6";
  34. rd3.UserName = "测试名称23";
  35. rd3.Region = "测试地址23";
  36. rd3.ChartName = "测试名称3";
  37. rd3.ChartKeyword = "F36";
  38. rd3.Appointedtime = DateTime.Now.ToString();
  39. rlist.Add(rd3);
  40. ExcelInfo.ExportClient("", dt, rlist);

public class ExcelInfo
{


  1.      /// <summary>
  2. /// 输出Excel文件
  3. /// </summary>
  4. /// <param name="FileName"></param>
  5. /// <param name="table"></param>
  6. /// <param name="?"></param>
  7. public static void ExportClient(string FileName, DataTable table, List<RangeData> Rlist)
  8. {
  9. //reportTitle = "Microsoft,IBM,Oracle,Google.Yahoo";
  10. FileName = FileName+DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".xlsx";
  11. // string TJcell = "D6,F6,H6,AJ6";
  12. if (Directory.Exists("UpFiles"))
  13. {
  14. }
  15. else
  16. {
  17. DirectoryInfo directoryInfo = new DirectoryInfo("UpFiles");
  18. directoryInfo.Create();
  19. }
  20. FileInfo file = new FileInfo(@"D:\文档资料\自学项目\EpplusExcelChartWeb\EpplusExcelChartWeb\upfiles\" + FileName);
  21. createExcel(file, table, Rlist);
  22. }
  23. /// <summary>
  24. /// 创建Excel Sheet
  25. /// </summary>
  26. /// <param name="file"></param>
  27. public static void createExcel(FileInfo file, DataTable table, List<RangeData> Rlist)
  28. {
  29. ExcelPackage package = new ExcelPackage(new FileStream(@"D:\文档资料\自学项目\EpplusExcelChartWeb\EpplusExcelChartWeb\test1.xlsx", FileMode.Open));
  30. ExcelWorksheet sheet = null;
  31. sheet = package.Workbook.Worksheets[1];
  32. #region 设置Excel数据
  33. SheetData(table, sheet);
  34. #endregion
  35. //设置图形
  36. if (Rlist.Count > 0)
  37. {
  38. ExcelWorksheet sheet1 = null;
  39. sheet1 = package.Workbook.Worksheets.Add("Data");
  40. for (int i = 0; i < Rlist.Count; i++)
  41. {
  42. SheetData(table, sheet1);
  43. sheet.Cells["C3"].Value = Rlist[i].UserName;
  44. sheet.Cells["C4"].Value = Rlist[i].Region;
  45. sheet.Cells["O4"].Value = Rlist[i].Appointedtime;
  46. if (Rlist[i].Charttype == "ColumnStacked")
  47. {
  48. //堆积柱形图
  49. ColumnStacked(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName,Rlist[i].ChartKeyword);
  50. }
  51. else if (Rlist[i].Charttype == "ColumnClustered")
  52. {
  53. //簇状柱形图
  54. ColumnClustered(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName, Rlist[i].ChartKeyword);
  55. }
  56. else if (Rlist[i].Charttype == "Pie")
  57. {
  58. //饼图
  59. Pie(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName, Rlist[i].ChartKeyword);
  60. }
  61. else if (Rlist[i].Charttype == "singleColumnClustered")
  62. {
  63. singleColumnClustered(table, sheet, Rlist[i].Yaxis, Rlist[i].ChartName, Rlist[i].ChartKeyword);
  64. }
  65. }
  66. }
  67. else
  68. {
  69. System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('没有数据')</script>");
  70. }
  71. package.SaveAs(file);//保存文件
  72. }
  73. //工作簿数据
  74. private static void SheetData(DataTable table, ExcelWorksheet sheet)
  75. {
  76. //if (cols[0] != "")
  77. //{
  78. // //设置列标题
  79. // for (int col = 1; col <= cols.Length; col++)
  80. // {
  81. // sheet.Cells[1, col].Value = cols[col - 1];
  82. // }
  83. //}
  84. //else
  85. //{
  86. //设置列标题
  87. //for (int col = 1; col <= table.Columns.Count; col++)
  88. //{
  89. // sheet.Cells[1, col].Value = table.Columns[col - 1].ColumnName;
  90. //}
  91. //}
  92. //设置数据
  93. for (int row = 0; row < table.Rows.Count; row++)
  94. {
  95. for (int col = 0; col < table.Columns.Count; col++)
  96. {
  97. string range = sheet.MergedCells[row + 7, col + 2];
  98. string strvalue = table.Rows[row][col].ToString();
  99. // sheet.Cells[row + 1, col].Style.Numberformat.Format = "#0\\.00%";//设置数据的格式为百分比
  100. if (table.Rows[row]["F36"].ToString() != "")
  101. {
  102. sheet.Cells[row + 7, col + 2].Style.Fill.PatternType = ExcelFillStyle.Solid;
  103. sheet.Cells[row + 7, col + 2].Style.Fill.BackgroundColor.SetColor(Color.Gray);
  104. sheet.Cells[row + 7, col + 2].Value = strvalue;
  105. }
  106. sheet.Cells[row + 7, col + 2].Value = strvalue;
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// 堆积柱形图
  112. /// </summary>
  113. private static void ColumnStacked(DataTable table,ExcelWorksheet sheet, string TJCell,string Chartname,string ChartKeyword)
  114. {
  115. ////图表系列
  116. ExcelChartSerie chartSerie = null;
  117. //图表
  118. ExcelChart chart = null;
  119. chart = sheet.Drawings.AddChart(Chartname, eChartType.ColumnStacked);
  120. chart.Legend.Position = eLegendPosition.Right;
  121. chart.Legend.Add();
  122. chart.SetSize(500, 400);//设置图表大小
  123. chart.ShowHiddenData = true;
  124. #region 规定单元格生成图表
  125. string[] TJcellarray = TJCell.Split(',');
  126. string XAxis = string.Empty;
  127. string YAxis = string.Empty;
  128. for (int row = 1; row <= table.Rows.Count; row++)
  129. {
  130. if (table.Rows[row - 1][ChartKeyword].ToString() != "")
  131. {
  132. for (int j = 0; j < TJcellarray.Length; j++)
  133. {
  134. XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + 6) + ",";
  135. YAxis += "test!" + TJcellarray[j] + ",";
  136. }
  137. int Xlength = XAxis.Length;
  138. int Ylength = YAxis.Length;
  139. XAxis = XAxis.Substring(0, Xlength - 1);
  140. YAxis = YAxis.Substring(0, Ylength - 1);
  141. chartSerie = chart.Series.Add(XAxis, YAxis);
  142. chartSerie.HeaderAddress = sheet.Cells[row + 6, 2];//设置每条线的名称
  143. XAxis = "";
  144. YAxis = "";
  145. }
  146. }
  147. #endregion
  148. // chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36");
  149. chart.SetPosition(table.Rows.Count + 7, 10, 1, 20);//设置图表位置
  150. }
  151. /// <summary>
  152. /// 多条簇状柱形图
  153. /// </summary>
  154. private static void ColumnClustered(DataTable table, ExcelWorksheet sheet, string TJCell, string Chartname, string ChartKeyword)
  155. {
  156. ////图表系列
  157. ExcelChartSerie chartSerie = null;
  158. //图表
  159. ExcelChart chart = null;
  160. chart = sheet.Drawings.AddChart(Chartname, eChartType.ColumnClustered);
  161. chart.Legend.Position = eLegendPosition.Right;
  162. chart.Legend.Add();
  163. chart.SetSize(500, 400);//设置图表大小
  164. chart.ShowHiddenData = true;
  165. #region 规定单元格生成图表
  166. string[] TJcellarray = TJCell.Split(',');
  167. string XAxis = string.Empty;
  168. string YAxis = string.Empty;
  169. for (int row = 1; row <= table.Rows.Count; row++)
  170. {
  171. if (table.Rows[row - 1][ChartKeyword].ToString() != "")
  172. {
  173. for (int j = 0; j < TJcellarray.Length; j++)
  174. {
  175. XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + 6) + ",";
  176. YAxis += "test!" + TJcellarray[j] + ",";
  177. }
  178. int Xlength = XAxis.Length;
  179. int Ylength = YAxis.Length;
  180. XAxis = XAxis.Substring(0, Xlength - 1);
  181. YAxis = YAxis.Substring(0, Ylength - 1);
  182. chartSerie = chart.Series.Add(XAxis, YAxis);
  183. chartSerie.HeaderAddress = sheet.Cells[row + 6, 2];//设置每条线的名称
  184. XAxis = "";
  185. YAxis = "";
  186. }
  187. }
  188. #endregion
  189. // chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36");
  190. chart.SetPosition(table.Rows.Count + 7, 10, 10, 20);//设置图表位置
  191. }
  192. /// <summary>
  193. ///饼图
  194. /// </summary>
  195. private static void Pie(DataTable table, ExcelWorksheet sheet, string TJCell, string Chartname, string ChartKeyword)
  196. {
  197. ////图表系列
  198. ExcelChartSerie chartSerie = null;
  199. //图表
  200. ExcelChart chart = null;
  201. chart = sheet.Drawings.AddChart(Chartname, eChartType.Pie);
  202. chart.Legend.Position = eLegendPosition.Right;
  203. chart.Legend.Add();
  204. chart.SetSize(500, 400);//设置图表大小
  205. chart.ShowHiddenData = true;
  206. #region 规定单元格生成图表
  207. string[] TJcellarray = TJCell.Split(',');
  208. string XAxis = string.Empty;
  209. string YAxis = string.Empty;
  210. for (int row = 1; row <= table.Rows.Count; row++)
  211. {
  212. if (table.Rows[row - 1][ChartKeyword].ToString() != "")
  213. {
  214. for (int j = 0; j < TJcellarray.Length; j++)
  215. {
  216. XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + 6) + ",";
  217. YAxis += "test!" + TJcellarray[j] + ",";
  218. }
  219. int Xlength = XAxis.Length;
  220. int Ylength = YAxis.Length;
  221. XAxis = XAxis.Substring(0, Xlength - 1);
  222. YAxis = YAxis.Substring(0, Ylength - 1);
  223. chartSerie = chart.Series.Add(XAxis, YAxis);
  224. chartSerie.HeaderAddress = sheet.Cells[row + 6, 2];//设置每条线的名称
  225. XAxis = "";
  226. YAxis = "";
  227. }
  228. }
  229. #endregion
  230. // chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36");
  231. chart.SetPosition(table.Rows.Count + 7+25, 10, 1, 20);//设置图表位置
  232. }
  233. /// <summary>
  234. /// 单条簇状柱形图
  235. /// </summary>
  236. private static void singleColumnClustered(DataTable table, ExcelWorksheet sheet, string TJCell, string Chartname, string ChartKeyword)
  237. {
  238. ////图表系列
  239. ExcelChartSerie chartSerie = null;
  240. //图表
  241. ExcelChart chart = null;
  242. chart = sheet.Drawings.AddChart(Chartname, eChartType.ColumnClustered);
  243. chart.Legend.Position = eLegendPosition.Right;
  244. chart.Legend.Add();
  245. chart.SetSize(500, 400);//设置图表大小
  246. chart.ShowHiddenData = true;
  247. #region 规定单元格生成图表
  248. string[] TJcellarray = TJCell.Split(',');
  249. string XAxis = string.Empty;
  250. string YAxis = string.Empty;
  251. for (int row = 1; row <= table.Rows.Count; row++)
  252. {
  253. if (table.Rows[row - 1][ChartKeyword].ToString() != "")
  254. {
  255. for (int j = 0; j < TJcellarray.Length; j++)
  256. {
  257. XAxis += "Data!" + GetEXcelstr(TJcellarray[j]) + (row + 6) + ",";
  258. YAxis += "test!" + TJcellarray[j] + ",";
  259. }
  260. int Xlength = XAxis.Length;
  261. int Ylength = YAxis.Length;
  262. XAxis = XAxis.Substring(0, Xlength - 1);
  263. YAxis = YAxis.Substring(0, Ylength - 1);
  264. chartSerie = chart.Series.Add(XAxis, YAxis);
  265. chartSerie.HeaderAddress = sheet.Cells[row + 6, 2];//设置每条线的名称
  266. XAxis = "";
  267. YAxis = "";
  268. }
  269. }
  270. #endregion
  271. // chartSerie = ChartData(table, cell, sheet, chartSerie, chart, TJcell,"F36");
  272. chart.SetPosition(table.Rows.Count + 7 + 25, 10, 10, 20);//设置图表位置
  273. }
  274. /// <summary>
  275. /// 提取字符串
  276. /// </summary>
  277. /// <param name="p_str"></param>
  278. /// <returns></returns>
  279. public static string GetEXcelstr(string p_str)
  280. {
  281. string strReturn = string.Empty;
  282. if (p_str == null || p_str.Trim() == "")
  283. {
  284. strReturn = "";
  285. }
  286. foreach (char chrTemp in p_str)
  287. {
  288. if (!Char.IsNumber(chrTemp))
  289. {
  290. strReturn += chrTemp;
  291. }
  292. }
  293. return strReturn;
  294. }
    }
  1. //RangeData类
  2.  
  3. public class RangeData
  4. {
  5. ///// <param name="XStartCell">数据开始单元格</param>
  6. //public string XStartCell { get; set; }
  7. // /// <param name="XEndCell">数据结束单元格</param>
  8. //public string XEndCell { get; set; }
  9. // /// <param name="EduName">Y开始单元格</param>
  10. //public string YStartCell { get; set; }
  11. // /// <param name="EduName">Y结束单元格</param>
  12. //public string YEndCell { get; set; }
  13. //图表类型 1.ColumnStacked 堆积柱形图 2.ColumnClustered 多条簇状柱形图 3.Pie饼图4.singleColumnClustered 单条簇状图
  14. public string Charttype { get; set; }
  15. //指定Y轴分类标签
  16. public string Yaxis { get; set; }
  17. //用户名
  18. public string UserName { get; set; }
  19. //所在区域
  20. public string Region { get; set; }
  21. //指定时间
  22. public string Appointedtime { get; set; }
  23. //图表名称
  24. public string ChartName { get; set; }
  25. //制图关键字
  26. public string ChartKeyword { get; set; }
  27. }

 如果执行报错 可能是获取Excel文件地址的问题。

 

下载地址https://files.cnblogs.com/files/Cjb8973/EpplusExcelChartWeb.rar

版权声明:本文为Cjb8973原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:http://www.cnblogs.com/Cjb8973/p/7885914.html