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

C#代碼實(shí)現(xiàn)根據(jù)Excel數(shù)據(jù)創(chuàng)建PowerPoint圖表

 更新時間:2026年06月26日 11:31:43   作者:2501_93070778  
創(chuàng)建圖表是提升 PowerPoint 演示文稿表現(xiàn)力的有效方式,本文將介紹如何在 C# 中讀取 Excel 數(shù)據(jù),在 PowerPoint 幻燈片中創(chuàng)建圖表,以及如何將 Excel 圖表以圖片形式插入到 PowerPoint 中,有需要的小伙伴可以了解下

創(chuàng)建圖表是提升 PowerPoint 演示文稿表現(xiàn)力的有效方式,它能夠?qū)?fù)雜的數(shù)據(jù)以直觀的形式呈現(xiàn),幫助觀眾快速理解關(guān)鍵信息。通過讀取 Excel 數(shù)據(jù)生成圖表,可以減少手動錄入數(shù)據(jù)的工作量,并提高數(shù)據(jù)的準(zhǔn)確性。如果希望在 PowerPoint 中直接使用 Excel 文件里的圖表,也可以將圖表以圖片形式插入到幻燈片中,從而完整保留原有的樣式和格式。

本文將介紹如何在 C# 中讀取 Excel 數(shù)據(jù),在 PowerPoint 幻燈片中創(chuàng)建圖表,以及如何將 Excel 圖表以圖片形式插入到 PowerPoint 中。

安裝依賴

開始之前,需要在 .NET 項(xiàng)目中添加所需的程序集引用。你可以下載對應(yīng)的 DLL 文件并手動引用,也可以通過 NuGet 安裝相關(guān)組件。

PM> Install-Package Spire.Office

使用 C# 根據(jù) Excel 數(shù)據(jù)創(chuàng)建 PowerPoint 圖表

在 .NET 中,可以先讀取 Excel 工作表中的數(shù)據(jù),再將這些數(shù)據(jù)作為數(shù)據(jù)源,在 PowerPoint 幻燈片中生成圖表。具體步驟如下:

1.創(chuàng)建 Presentation 對象。

2.創(chuàng)建 Workbook 對象,并使用 Workbook.LoadFromFile() 方法加載 Excel 文件。

3.獲取演示文稿中的第一張幻燈片,并使用 ISlide.Shapes.AppendChart() 方法添加圖表。

4.使用 IChart.ChartData.Clear() 方法清除圖表中的默認(rèn)示例數(shù)據(jù)。

5.獲取 Excel 工作簿中的第一個工作表。

6.遍歷工作表中的行和列:

  • 讀取單元格數(shù)據(jù)。
  • 將讀取的數(shù)據(jù)寫入圖表數(shù)據(jù)源。

7.設(shè)置圖表標(biāo)題。

8.設(shè)置圖表系列標(biāo)簽和分類標(biāo)簽。

9.設(shè)置各數(shù)據(jù)系列的值。

10.設(shè)置分類軸和值軸的數(shù)字格式。

11.設(shè)置圖表樣式。

12.保存 PowerPoint 演示文稿。

完整示例代碼如下:

using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Xls;
using System.Drawing;
using FileFormat = Spire.Presentation.FileFormat;
using IChart = Spire.Presentation.Charts.IChart;
namespace PresentationChartExcelData
{
    class Program
    {
        public static void Main(string[] args)
        {
            // 創(chuàng)建 Presentation 類的實(shí)例
            Presentation presentation = new Presentation();
            // 設(shè)置幻燈片大小
            presentation.SlideSize.Type = SlideSizeType.Screen16x9;
            // 創(chuàng)建 Workbook 類的實(shí)例并加載 Excel 文件
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            // 獲取工作簿中的第一個工作表
            Worksheet sheet = workbook.Worksheets[0];
            // 在演示文稿中創(chuàng)建圖表
            RectangleF rect = new RectangleF(
                50,
                100,
                presentation.SlideSize.Size.Width - 100,
                presentation.SlideSize.Size.Height - 150);
            ISlide slide = presentation.Slides[0];
            IChart chart = slide.Shapes.AppendChart(ChartType.ColumnClustered, rect);
            // 清除圖表中的默認(rèn)示例數(shù)據(jù)
            chart.ChartData.Clear(0, 0, 5, 5);
            // 遍歷工作表中的所有行
            for (int i = 0; i < sheet.AllocatedRange.RowCount; i++)
            {
                // 遍歷工作表中的所有列
                for (int j = 0; j < sheet.AllocatedRange.ColumnCount; j++)
                {
                    // 將 Excel 單元格數(shù)據(jù)寫入圖表數(shù)據(jù)
                    chart.ChartData[i, j].Value = sheet.AllocatedRange[i + 1, j + 1].Value2;
                    // 同時復(fù)制數(shù)字格式
                    chart.ChartData[i, j].NumberFormat = sheet.AllocatedRange[i + 1, j + 1].NumberFormat;
                }
            }
            // 設(shè)置圖表標(biāo)題
            chart.ChartTitle.TextProperties.Text = sheet.Name;
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 25;
            chart.HasTitle = true;
            // 設(shè)置系列標(biāo)簽和分類標(biāo)簽
            chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + sheet.AllocatedRange.RowCount];
            // 設(shè)置系列數(shù)據(jù)
            chart.Series[0].Values = chart.ChartData["B2", "B" + sheet.AllocatedRange.RowCount];
            chart.Series[1].Values = chart.ChartData["C2", "C" + sheet.AllocatedRange.RowCount];
            // 設(shè)置坐標(biāo)軸數(shù)字格式
            chart.PrimaryCategoryAxis.NumberFormat = sheet.AllocatedRange["A2"].NumberFormat;
            chart.PrimaryValueAxis.NumberFormat = sheet.AllocatedRange["B2"].NumberFormat;
            // 設(shè)置圖表樣式
            chart.ChartStyle = ChartStyle.Style2;
            // 設(shè)置系列重疊和間隙寬度
            chart.OverLap = 50;
            chart.GapWidth = 200;
            // 保存演示文稿
            presentation.SaveToFile("output/PresentationChartExcelData.pptx", FileFormat.Pptx2019);
            // 釋放資源
            presentation.Dispose();
            workbook.Dispose();
        }
    }
}

使用 C# 將 Excel 圖表作為圖片插入 PowerPoint

如果希望將 Excel 工作表中的現(xiàn)有圖表插入到 PowerPoint 幻燈片中,并完整保留其原有的樣式和格式,可以先將圖表導(dǎo)出為圖片,再將圖片插入到幻燈片中。

具體步驟如下:

  1. 創(chuàng)建 Presentation 對象。
  2. 創(chuàng)建 Workbook 對象,并使用 Workbook.LoadFromFile() 方法加載 Excel 文件。
  3. 使用 Workbook.SaveChartAsImage() 方法將工作表中的圖表保存為圖片。
  4. 使用 Presentation.Images.Append() 方法將圖片添加到演示文稿資源中。
  5. 使用 Presentation.Slides[].AppendEmbedImage() 方法將圖片插入到指定幻燈片。
  6. 使用 Presentation.SaveToFile() 方法保存 PowerPoint 演示文稿。

完整示例代碼如下:

using Spire.Presentation;
using Spire.Presentation.Drawing;
using Spire.Xls;
using System.Drawing;
using FileFormat = Spire.Presentation.FileFormat;
namespace PresentationChartExcelChart
{
    class Program
    {
        public static void Main(string[] args)
        {
            // 創(chuàng)建 Presentation 類的實(shí)例
            Presentation presentation = new Presentation();
            // 設(shè)置幻燈片大小
            presentation.SlideSize.Type = SlideSizeType.Screen16x9;
            // 創(chuàng)建 Workbook 類的實(shí)例并加載 Excel 文件
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            // 將第一個工作表中的第一個圖表保存為圖片
            Image image = workbook.SaveChartAsImage(workbook.Worksheets[0], 0);
            // 將圖片添加到演示文稿資源中
            IImageData imageData = presentation.Images.Append(image);
            // 將圖片插入到第一張幻燈片
            RectangleF rect = new RectangleF(
                50,
                120,
                presentation.SlideSize.Size.Width - 100,
                presentation.SlideSize.Size.Height - 170);
            presentation.Slides[0].Shapes.AppendEmbedImage(
                ShapeType.Rectangle,
                imageData,
                rect);
            // 保存演示文稿
            presentation.SaveToFile(
                "output/PresentationChartExcelChart.pptx",
                FileFormat.Pptx2019);
            // 釋放資源
            presentation.Dispose();
            workbook.Dispose();
        }
    }
}

總結(jié)

本文介紹了如何在 C# 中利用 Excel 數(shù)據(jù)生成 PowerPoint 圖表,以及如何將 Excel 圖表作為圖片插入 PowerPoint 演示文稿。第一種方法通過讀取工作表數(shù)據(jù)動態(tài)創(chuàng)建圖表,適用于需要根據(jù)最新數(shù)據(jù)自動生成演示文稿的場景;第二種方法則將 Excel 中已有的圖表導(dǎo)出為圖片并插入幻燈片,能夠完整保留圖表的樣式和格式,適合直接復(fù)用現(xiàn)有圖表。開發(fā)者可根據(jù)實(shí)際需求選擇合適的方式,實(shí)現(xiàn) Excel 數(shù)據(jù)與 PowerPoint 演示文稿之間的高效集成。

到此這篇關(guān)于C#代碼實(shí)現(xiàn)根據(jù)Excel數(shù)據(jù)創(chuàng)建PowerPoint圖表的文章就介紹到這了,更多相關(guān)C#創(chuàng)建PowerPoint圖表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

松潘县| 错那县| 滨州市| 平泉县| 金寨县| 赤峰市| 交口县| 和林格尔县| 花莲县| 阜新| 隆尧县| 宁河县| 海淀区| 海原县| 金川县| 新乡县| 瑞昌市| 泰宁县| 南安市| 白沙| 贵阳市| 临颍县| 吉林市| 高雄市| 徐汇区| 商洛市| 罗甸县| 根河市| 乡城县| 德惠市| 新源县| 丹凤县| 江西省| 开鲁县| 嘉禾县| 唐海县| 仙桃市| 西乌| 东乌珠穆沁旗| 合阳县| 吐鲁番市|