C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > c# EPPlus图表导出Excel

c# EPPlus秘籍之Excel实现图表导出

作者:谭广健

这篇文章主要为大家介绍了c# EPPlus秘籍之Excel实现图表导出示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

引言

之前受某大神推荐使用EPPlus作为excel之控件,简直如获一宝,大数据导出将不再麻烦,服务器也不必再安装烦人的office控件.简单易用更重要不用破解什么的。

大数据导出测试

今天测试了2点,一个是大数据导出测试5W简直没压力。

代码如下

      FileInfo newFile = new FileInfo(@"c:\test1.xlsx");
            if (newFile.Exists)
            {
                newFile.Delete();
                newFile = new FileInfo(@"c:\test.xlsx");
            }
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test");
                 for (int i = 1; i <= 50000; i++)
                 {
                     for (int j = 1; j <= 20; j++)
                     {
                         worksheet.Cells[i, j].Value = i + "|" + j;
                     }
                 }
                 package.Save();
                 MessageBox.Show("ok");
             }
             

于是再测试一下有什么特殊功能,发现有一个图表导出功能也很不错,分享代码如下: 

                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test");
                worksheet.Cells.Style.WrapText = true;
                worksheet.View.ShowGridLines = false;//去掉sheet的网格线
                worksheet.Cells[1, 1].Value = "名称";
                worksheet.Cells[1, 2].Value = "价格";
                worksheet.Cells[1, 3].Value = "销量";
                worksheet.Cells[2, 1].Value = "大米";
                worksheet.Cells[2, 2].Value = 56;
                worksheet.Cells[2, 3].Value = 100;
                worksheet.Cells[3, 1].Value = "玉米";
                worksheet.Cells[3, 2].Value = 45;
                worksheet.Cells[3, 3].Value = 150;
                worksheet.Cells[4, 1].Value = "小米";
                worksheet.Cells[4, 2].Value = 38;
                worksheet.Cells[4, 3].Value = 130;
                worksheet.Cells[5, 1].Value = "糯米";
                worksheet.Cells[5, 2].Value = 22;
                worksheet.Cells[5, 3].Value = 200;
                using (ExcelRange range = worksheet.Cells[1, 1, 5, 3])
                {
                    range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                    range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                }
                using (ExcelRange range = worksheet.Cells[1, 1, 1, 3])
                {
                    range.Style.Font.Bold = true;
                    range.Style.Font.Color.SetColor(Color.White);
                    range.Style.Font.Name = "微软雅黑";
                    range.Style.Font.Size = 12;
                    range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128));
                }
                worksheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[1, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[1, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[2, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[2, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[2, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[3, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[3, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[3, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[4, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[4, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[4, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[5, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[5, 2].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                worksheet.Cells[5, 3].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));
                ExcelChart chart = worksheet.Drawings.AddChart("chart", eChartType.ColumnClustered);
                //eChartType中可以选择图表类型
                ExcelChartSerie serie = chart.Series.Add(worksheet.Cells[2, 3, 5, 3], worksheet.Cells[2, 1, 5, 1]);
                //设置图表的x轴和y轴
                serie.HeaderAddress = worksheet.Cells[1, 3];//设置图表的图例
                chart.SetPosition(150, 10);//设置位置
                chart.SetSize(500, 300);//设置大小
                chart.Title.Text = "销量走势";//设置图表的标题
                chart.Title.Font.Color = Color.FromArgb(89, 89, 89);//设置标题的颜色
                chart.Title.Font.Size = 15;//标题的大小
                chart.Title.Font.Bold = true;//标题的粗体
                chart.Style = eChartStyle.Style15;//设置图表的样式
                chart.Legend.Border.LineStyle = eLineStyle.Solid;
                chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217);//设置图例的样式

效果

如下:

将图片导入到excel

还有可以将图片导入到excel中去,代码如下:

ExcelPicture picture = worksheet.Drawings.AddPicture("logo", Image.FromFile(@"c:\QQ截图20190403205944.jpg"));//插入图片
picture.SetPosition(100, 100);//设置图片的位置
picture.SetSize(100, 100);//设置图片的大小

多个excel的权限

在最后弄多个excel的权限,加入后修改是需要口令的。

worksheet.Protection.IsProtected = true;//设置是否进行锁定
worksheet.Protection.SetPassword("ABC");//设置密码
worksheet.Protection.AllowAutoFilter = false;//下面是一些锁定时权限的设置
worksheet.Protection.AllowDeleteColumns = false;
worksheet.Protection.AllowDeleteRows = false;
worksheet.Protection.AllowEditScenarios = false;
worksheet.Protection.AllowEditObject = false;
worksheet.Protection.AllowFormatCells = false;
worksheet.Protection.AllowFormatColumns = false;
worksheet.Protection.AllowFormatRows = false;
worksheet.Protection.AllowInsertColumns = false;
worksheet.Protection.AllowInsertHyperlinks = false;
worksheet.Protection.AllowInsertRows = false;
worksheet.Protection.AllowPivotTables = false;
worksheet.Protection.AllowSelectLockedCells = false;
worksheet.Protection.AllowSelectUnlockedCells = false;
worksheet.Protection.AllowSort = false;

加入excel的描述

还有一个秘籍,加入excel的描述:

epk.Workbook.Properties.Title = "excel的标题";//设置excel的标题 epk.Workbook.Properties.Author = "谭广健";//作者 epk.Workbook.Properties.Comments = "这是备注";//备注 epk.Workbook.Properties.Company = "公司版权啥的";//公司

通过SQL语句直接导出Excel

 var db_data = ExecuteNonQuery("select * from SYS_LIST_SETTING", CommandType.Text);
     DataSet ds = new DataSet();
     ds.Tables.Add(db_data);
     DataSetToExcel(ds, @"c:\test1.xlsx");
     
 #region 读数据库
        public static DataTable ExecuteNonQuery(String cmdText, CommandType ct)
        {
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand(cmdText, conn))
                    {
                        cmd.CommandType = ct;
                        using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            dt.Load(sdr);
                        }
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    System.Diagnostics.Debug.WriteLine(dt.Rows.Count);
                }
            }
            return dt;
        }
 #endregion
 #region DataTable to Excel2007 
     private static void DataSetToExcel(DataSet dataSet, string filePath)
        {
            using (ExcelPackage pck = new ExcelPackage())
            {
                foreach (DataTable dataTable in dataSet.Tables)
                {
                    ExcelWorksheet workSheet = pck.Workbook.Worksheets.Add(dataTable.TableName);
                    workSheet.Cells["A1"].LoadFromDataTable(dataTable, true);
                }

                pck.SaveAs(new FileInfo(filePath));
            }
        }
 #endregion

以上就是c# EPPlus秘籍之Excel实现图表导出的详细内容,更多关于c# EPPlus图表导出Excel的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文