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)啥的";//公司
通過(guò)SQL語(yǔ)句直接導(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ù)庫(kù)
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)文章
unity實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
C#利用iTextSharp組件給PDF文檔添加圖片/文字水印
這篇文章主要給大家介紹了關(guān)于如何C#利用iTextSharp組件給PDF文檔添加圖片/文字水印的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
在C#中使用二叉樹實(shí)時(shí)計(jì)算海量用戶積分排名的實(shí)現(xiàn)詳解
這篇文章主要介紹了在C#中使用二叉樹實(shí)時(shí)計(jì)算海量用戶積分排名的實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
深入淺析C#?11?對(duì)?ref?和?struct?的改進(jìn)
這篇文章主要介紹了C#?11?對(duì)?ref?和?struct?的改進(jìn),有了這些基礎(chǔ)設(shè)施,開發(fā)者們將能輕松使用安全的方式來(lái)編寫沒有任何堆內(nèi)存開銷的高性能代碼,需要的朋友可以參考下2022-04-04
C#實(shí)現(xiàn)凍結(jié)Excel窗口以鎖定行列或解除凍結(jié)
在處理大型Excel工作簿時(shí),有時(shí)候我們需要在工作表中凍結(jié)窗格,這樣可以在滾動(dòng)查看數(shù)據(jù)的同時(shí)保持某些行或列固定不動(dòng),下面我們就來(lái)看看如何使用C#實(shí)現(xiàn)凍結(jié)Excel窗口吧2024-04-04

