c# EPPlus秘籍之Excel實(shí)現(xiàn)圖表導(dǎo)出
引言
之前受某大神推薦使用EPPlus作為excel之控件,簡(jiǎn)直如獲一寶,大數(shù)據(jù)導(dǎo)出將不再麻煩,服務(wù)器也不必再安裝煩人的office控件.簡(jiǎn)單易用更重要不用破解什么的。
大數(shù)據(jù)導(dǎo)出測(cè)試
今天測(cè)試了2點(diǎn),一個(gè)是大數(shù)據(jù)導(dǎo)出測(cè)試5W簡(jiǎn)直沒壓力。
代碼如下
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"); }
于是再測(cè)試一下有什么特殊功能,發(fā)現(xiàn)有一個(gè)圖表導(dǎo)出功能也很不錯(cuò),分享代碼如下:
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("test"); worksheet.Cells.Style.WrapText = true; worksheet.View.ShowGridLines = false;//去掉sheet的網(wǎng)格線 worksheet.Cells[1, 1].Value = "名稱"; worksheet.Cells[1, 2].Value = "價(jià)格"; 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]); //設(shè)置圖表的x軸和y軸 serie.HeaderAddress = worksheet.Cells[1, 3];//設(shè)置圖表的圖例 chart.SetPosition(150, 10);//設(shè)置位置 chart.SetSize(500, 300);//設(shè)置大小 chart.Title.Text = "銷量走勢(shì)";//設(shè)置圖表的標(biāo)題 chart.Title.Font.Color = Color.FromArgb(89, 89, 89);//設(shè)置標(biāo)題的顏色 chart.Title.Font.Size = 15;//標(biāo)題的大小 chart.Title.Font.Bold = true;//標(biāo)題的粗體 chart.Style = eChartStyle.Style15;//設(shè)置圖表的樣式 chart.Legend.Border.LineStyle = eLineStyle.Solid; chart.Legend.Border.Fill.Color = Color.FromArgb(217, 217, 217);//設(shè)置圖例的樣式
效果
如下:
將圖片導(dǎo)入到excel
還有可以將圖片導(dǎo)入到excel中去,代碼如下:
ExcelPicture picture = worksheet.Drawings.AddPicture("logo", Image.FromFile(@"c:\QQ截圖20190403205944.jpg"));//插入圖片 picture.SetPosition(100, 100);//設(shè)置圖片的位置 picture.SetSize(100, 100);//設(shè)置圖片的大小
多個(gè)excel的權(quán)限
在最后弄多個(gè)excel的權(quán)限,加入后修改是需要口令的。
worksheet.Protection.IsProtected = true;//設(shè)置是否進(jìn)行鎖定 worksheet.Protection.SetPassword("ABC");//設(shè)置密碼 worksheet.Protection.AllowAutoFilter = false;//下面是一些鎖定時(shí)權(quán)限的設(shè)置 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的描述
還有一個(gè)秘籍,加入excel的描述:
epk.Workbook.Properties.Title = "excel的標(biāo)題";//設(shè)置excel的標(biāo)題 epk.Workbook.Properties.Author = "譚廣健";//作者 epk.Workbook.Properties.Comments = "這是備注";//備注 epk.Workbook.Properties.Company = "公司版權(quán)啥的";//公司
通過SQL語句直接導(dǎo)出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 讀數(shù)據(jù)庫 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實(shí)現(xiàn)圖表導(dǎo)出的詳細(xì)內(nèi)容,更多關(guān)于c# EPPlus圖表導(dǎo)出Excel的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射實(shí)例分析
這篇文章主要介紹了C#根據(jù)反射和特性實(shí)現(xiàn)ORM映射的方法,實(shí)例分析了反射的原理、特性與ORM的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04C#在foreach遍歷刪除集合中元素的三種實(shí)現(xiàn)方法
這篇文章主要給大家總結(jié)介紹了關(guān)于C#在foreach遍歷刪除集合中元素的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C#中decimal保留2位有效小數(shù)的實(shí)現(xiàn)方法,針對(duì)decimal變量保留2位有效小數(shù)有多種方法,可以使用Math.Round方法以及ToString先轉(zhuǎn)換為字符串等操作來實(shí)現(xiàn)。具體實(shí)現(xiàn)方法感興趣的朋友跟隨小編一起看看吧2019-10-10詳解WPF如何使用WriteableBitmap提升Image性能
這篇文章主要為大家詳細(xì)介紹了WPF如何使用WriteableBitmap提升Image性能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01Unity實(shí)現(xiàn)物體弧線運(yùn)動(dòng)到規(guī)定的坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體以弧線的形式運(yùn)動(dòng)到規(guī)定的坐標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C#/VB.NET實(shí)現(xiàn)在Word中插入或刪除腳注
腳注,是可以附在文章頁面的最底端的,對(duì)某些東西加以說明,印在書頁下端的注文。這篇文章將為您展示如何通過C#/VB.NET代碼,以編程方式在Word中插入或刪除腳注,需要的可以參考一下2023-03-03C#調(diào)用百度翻譯實(shí)現(xiàn)翻譯HALCON的示例
HALCON示例程序的描述部分一直是英文的,看起來很不方便。本文就使用百度翻譯實(shí)現(xiàn)翻譯HALCON,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06