最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用Gembox.SpreadSheet向Excel寫入數(shù)據(jù)及圖表的實(shí)例

 更新時間:2017年12月06日 15:10:50   作者:cnc  
下面小編就為大家分享一篇C#使用Gembox.SpreadSheet向Excel寫入數(shù)據(jù)及圖表的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

開發(fā)工具:VS2017

語言:C#

DotNet版本:.Net FrameWork 4.0及以上

使用的DLL工具名稱:GemBox.Spreadsheet.dll (版本:37.3.30.1185)

一、GemBox.Spreadsheet工具:

該DLL是由GemBox公司開發(fā)的基于Excel功能的開發(fā)工具,該DLL很輕量,且使用起來很方便,在這里推薦下來來使用。

下載地址:

http://xiazai.jb51.net/201712/yuanma/GemBox_Spreadsheet.zip

本文就是使用該工具進(jìn)行Excel的寫入操作。

二、創(chuàng)建Excel

為了能使用該DLL,必須在調(diào)用前寫入以下代碼:

SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

創(chuàng)建Excel文件如下:

ExcelFile excel = new ExcelFile();

這里僅僅只是創(chuàng)建一個excel,代表的是excel整個文件,而保存該文件的代碼如下:

excel.Save("文件路徑");

三、給Excel添加一些屬性

我們可以給excel添加一些諸如文檔標(biāo)題、作者、公司及備注等內(nèi)容,實(shí)現(xiàn)這些內(nèi)容的代碼如下:

excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Title, TITLE));
excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Author, "CNXY"));
excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Company, "CNXY"));
excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Comments, "By CNXY.Website: http://www.cnc6.cn"));

四、給excel默認(rèn)字體

這是給整個Excel設(shè)置統(tǒng)一的字體,具體代碼如下:

excel.DefaultFontName = "Times New Roman";

五、添加一個Sheet表格

要知道,Excel是由Sheet表格構(gòu)成的,因此添加Sheet表格的代碼如下:

ExcelWorksheet sheet = excel.Worksheets.Add("表格名稱");

以上,已經(jīng)在excel上添加了一個名為“表格名稱”的數(shù)據(jù)表格。

六、給Sheet添加密碼保護(hù)

有時候,為了保護(hù)自己的Excel不被篡改,需要設(shè)置一下Sheet的密碼,具體代碼如下:

sheet.ProtectionSettings.SetPassword("cnxy");
sheet.Protected = true;

七、讓網(wǎng)格線不可見

默認(rèn)情況下,Sheet的網(wǎng)格線是可見的,有時候,我們可以設(shè)置網(wǎng)格線不可見,具體代碼如下:

sheet.ViewOptions.ShowGridLines = false;

八、寫入單元格

訪問單元格的方式有三種,三種分別如下:

sheet.Cells["A1"]
sheet.Cells[0,0]
sheet.Rows[0].Cells[0]

以上三種方法都可以訪問單元格,但如下寫入單元格呢,其實(shí)方法很簡單,如下:

sheet.Cells["A1"].Value= 內(nèi)容

以上沒有加雙引號的原因是:內(nèi)容不一定是字符串,有可能是數(shù)字、日期等。

九、單元格樣式設(shè)置

單元格設(shè)置需要使用CellStyle對象,其代碼如下:

CellStyle style = new CellStyle();
//設(shè)置水平對齊模式
style.HorizontalAlignment = HorizontalAlignmentStyle.Center;
//設(shè)置垂直對齊模式
style.VerticalAlignment = VerticalAlignmentStyle.Center;
//設(shè)置字體
style.Font.Size = 22 * PT; //PT=20
style.Font.Weight = ExcelFont.BoldWeight;
style.Font.Color = Color.Blue;
sheet.Cells["A1"].Style = style;

填充方式如下:

sheet.Cells[24,1].Style.FillPattern.PatternStyle = FillPatternStyle.Solid; 
sheet.Rows[24].Cells[1].Style.FillPattern.PatternForegroundColor = Color.Gainsboro;

設(shè)置邊框如下:

style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);

十、合并單元格

合并單元格需使用CellRange對象,我們可以從sheet.Cells.GetSubrange或GetSubrangeAbsolute獲得,代碼如下:

CellRange range = sheet.Cells.GetSubrange("B2", "J3");
range.Value = "Chart";
range.Merged = true;
sheet.Cells.GetSubrangeAbsolute(24, 1, 24, 9).Merged = true;

十一、創(chuàng)建Chart圖表對象

使用的是LineChart對象,代碼如下:

LineChart chart =(LineChart)sheet.Charts.Add(ChartType.Line,"B4","J22");

以上意思是從B4到J22創(chuàng)建一個LineChart對象。

設(shè)置圖表標(biāo)題不可見,代碼如下:

chart.Title.IsVisible = false;

設(shè)置X軸與Y軸的標(biāo)題可見,代碼如下:

chart.Axes.Horizontal.Title.Text = "Time";
chart.Axes.Vertical.Title.Text = "Voltage";

十二、給Y軸設(shè)置屬性

主要使用了chart.Axes.VerticalValue返回的ValueAxis對象,代碼如下:

ValueAxis axisY = chart.Axes.VerticalValue;
//Y軸最大刻度與最小刻度
axisY.Minimum = -100;
axisY.Maximum = 100;
//Y軸主要與次要單位大小
axisY.MajorUnit = 20;
axisY.MinorUnit = 10;
//Y軸主要與次要網(wǎng)格是否可見
axisY.MajorGridlines.IsVisible = true;
axisY.MinorGridlines.IsVisible = true;
//Y軸刻度線類型
axisY.MajorTickMarkType = TickMarkType.Cross; 
axisY.MinorTickMarkType = TickMarkType.Inside;

十三、附上完整的源代碼

using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Charts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
namespace SpreadSheetChartDemo
{
 class Program
 {
 const int PT = 20;
 const int LENGTH = 200;
 const string TIMESNEWROMAN = "Times New Roman";
 const string TITLE = "Spread Sheet Chart Demo";
 static void Main(string[] args)
 {
 SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
 ExcelFile excel = new ExcelFile();
 //Excel默認(rèn)字體
 excel.DefaultFontName = TIMESNEWROMAN;
 //Excel文檔屬性設(shè)置
 excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Title, TITLE));
 excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Author, "CNXY"));
 excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Company, "CNXY"));
 excel.DocumentProperties.BuiltIn.Add(new KeyValuePair<BuiltInDocumentProperties, string>(BuiltInDocumentProperties.Comments, "By CNXY.Website: http://www.cnc6.cn"));
 //新建一個Sheet表格
 ExcelWorksheet sheet = excel.Worksheets.Add(TITLE);
 //設(shè)置表格保護(hù)
 sheet.ProtectionSettings.SetPassword("cnxy");
 sheet.Protected = true;
 //設(shè)置網(wǎng)格線不可見
 sheet.ViewOptions.ShowGridLines = false;
 //定義一個B2-G3的單元格范圍
 CellRange range = sheet.Cells.GetSubrange("B2", "J3");
 range.Value = "Chart";
 range.Merged = true;
 //定義一個單元格樣式
 CellStyle style = new CellStyle();
 //設(shè)置邊框
 style.Borders.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
 //設(shè)置水平對齊模式
 style.HorizontalAlignment = HorizontalAlignmentStyle.Center;
 //設(shè)置垂直對齊模式
 style.VerticalAlignment = VerticalAlignmentStyle.Center;
 //設(shè)置字體
 style.Font.Size = 22 * PT;
 style.Font.Weight = ExcelFont.BoldWeight;
 style.Font.Color = Color.Blue;
 range.Style = style;
 //增加Chart
 LineChart chart = (LineChart)sheet.Charts.Add(ChartType.Line,"B4","J22");
 chart.Title.IsVisible = false;
 chart.Axes.Horizontal.Title.Text = "Time";
 chart.Axes.Vertical.Title.Text = "Voltage";
 ValueAxis axisY = chart.Axes.VerticalValue;
 //Y軸最大刻度與最小刻度
 axisY.Minimum = -100;
 axisY.Maximum = 100;
 //Y軸主要與次要單位大小
 axisY.MajorUnit = 20;
 axisY.MinorUnit = 10;
 //Y軸主要與次要網(wǎng)格是否可見
 axisY.MajorGridlines.IsVisible = true;
 axisY.MinorGridlines.IsVisible = true;
 //Y軸刻度線類型
 axisY.MajorTickMarkType = TickMarkType.Cross; 
 axisY.MinorTickMarkType = TickMarkType.Inside;
 Random random = new Random();
 double[] data = new double[LENGTH];
 for (int i=0;i< LENGTH; i++)
 {
 if( random.Next(0,100) > 50)
 data[i] = random.NextDouble() * 100;
 else
 data[i] = -random.NextDouble() * 100;
 }
 chart.Series.Add("Random", data);
 //尾部信息
 range = sheet.Cells.GetSubrange("B23", "J24");
 range.Value = $"Write Time:{DateTime.Now:yyyy-MM-dd HH:mm:ss} By CNXY";
 range.Merged = true;
 //B25(三種單元格模式)
 sheet.Cells["B25"].Value = "http://www.cnc6.cn";
 sheet.Cells[24,1].Style.FillPattern.PatternStyle = FillPatternStyle.Solid;
 sheet.Rows[24].Cells[1].Style.FillPattern.PatternForegroundColor = Color.Gainsboro;
 //B25,J25
 sheet.Cells.GetSubrangeAbsolute(24, 1, 24, 9).Merged = true;
 string filePath = $@"{Environment.CurrentDirectory}\SheetChart.xlsx";
 try
 {
 excel.Save(filePath);
 Process.Start(filePath);
 Console.WriteLine("Write successfully");
 }
 catch(Exception ex)
 {
 Console.WriteLine(ex);
 }
 Console.Write("Press any key to continue.");
 Console.ReadKey();
 }
 }
}

十四、生成的Excel

演示的Excel下載地址:

http://xiazai.jb51.net/201712/yuanma/SheetChart.zip

十五、生成的exe

下載地址如下:

http://xiazai.jb51.net/201712/yuanma/SpreadSheetChartDemo.zip

運(yùn)行結(jié)果如下:

以上這篇C#使用Gembox.SpreadSheet向Excel寫入數(shù)據(jù)及圖表的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#簡單讀取、改變文件的創(chuàng)建、修改及訪問時間的方法

    C#簡單讀取、改變文件的創(chuàng)建、修改及訪問時間的方法

    這篇文章主要介紹了C#簡單讀取、改變文件的創(chuàng)建、修改及訪問時間的方法,涉及C#文件類SetCreationTime、SetLastWriteTime及SetLastAccessTime的相關(guān)使用技巧,需要的朋友可以參考下
    2015-07-07
  • C#中單例模式的三種寫法示例

    C#中單例模式的三種寫法示例

    這篇文章主要介紹了C#中單例模式的三種寫法示例,本文分別給出代碼實(shí)例,需要的朋友可以參考下
    2015-06-06
  • C#連接mysql數(shù)據(jù)庫完整實(shí)例

    C#連接mysql數(shù)據(jù)庫完整實(shí)例

    這篇文章主要介紹了C#連接mysql數(shù)據(jù)庫的方法,以一個完整實(shí)例形式分析了C#操作mysql數(shù)據(jù)庫連接的基本技巧,非常具有實(shí)用價值,需要的朋友可以參考下
    2015-05-05
  • C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值

    C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值

    這篇文章給大家介紹了利用C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值,首先需要定義一個一維數(shù)組,然后修改數(shù)組的長度,從而在其中增加一個元素,需要的朋友可以參考下
    2024-02-02
  • C# FileStream復(fù)制大文件功能

    C# FileStream復(fù)制大文件功能

    這篇文章主要為大家詳細(xì)介紹了C# FileStream復(fù)制大文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 通過C#和RTSPClient實(shí)現(xiàn)簡易音視頻解碼功能

    通過C#和RTSPClient實(shí)現(xiàn)簡易音視頻解碼功能

    在多媒體應(yīng)用中,實(shí)時傳輸協(xié)議(RTSP)用于流媒體服務(wù),特別是音視頻?監(jiān)控系統(tǒng),通過?C#?和?RTSPClient?庫,可以輕松實(shí)現(xiàn)簡易的音視頻解碼和播放功能,本文將詳細(xì)介紹如何使用?C#?和?RTSPClient?構(gòu)建一個簡易但高效的音視頻解碼器,需要的朋友可以參考下
    2024-12-12
  • Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀

    Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)粒子光效導(dǎo)出成png序列幀,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • C# DataTable常見用法匯總

    C# DataTable常見用法匯總

    這篇文章主要介紹了C# DataTable常見用法,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下
    2020-08-08
  • 理解C#生成驗(yàn)證碼的過程

    理解C#生成驗(yàn)證碼的過程

    這篇文章主要介紹了C#生成驗(yàn)證碼的過程,通過實(shí)例分析C#驗(yàn)證碼的生成原理,感興趣的小伙伴們可以參考一下
    2016-03-03
  • C#中的yield關(guān)鍵字的使用方法介紹

    C#中的yield關(guān)鍵字的使用方法介紹

    yield這個關(guān)鍵字是和迭代器掛鉤的,而且是與return一起以yield return的形式合用的,用來返回迭代器中的條目。
    2013-04-04

最新評論

台江县| 景德镇市| 曲靖市| 林芝县| 铜鼓县| 静宁县| 安吉县| 舞钢市| 明光市| 陵川县| 呼伦贝尔市| 徐闻县| 常熟市| 龙胜| 隆昌县| 河南省| 蕲春县| 建瓯市| 曲阳县| 盈江县| 辽阳市| 阿合奇县| 镇宁| 大关县| 新干县| 新绛县| 原阳县| 萨迦县| 瑞金市| 怀柔区| 宝兴县| 中宁县| 灯塔市| 余姚市| 多伦县| 阜南县| 鹤壁市| 麻江县| 桑日县| 遵化市| 鹿泉市|