如何使用 Microsoft.Office.Interop 从 Excel 中自动选择数据

问题描述

我正在使用

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;

我在 PowerPoint 中有图表,例如

enter image description here

和优秀一样

enter image description here

添加超过 7 条数据时,我需要将选择数据设为动态,仅使用 7 条,因为只有图表选择 7 行,但是当 excel 数据多于或少于 7 条时需要此动态选择。 当添加超过 7 个数据时,excel 中已存在但仅选择 7 行

foreach (Microsoft.Office.Interop.PowerPoint.Shape textShape in slide.Shapes)
{

  if (textShape.HasChart == MsoTriState.msoTrue || textShape.HasSmartArt == MsoTriState.msoTrue)
    {

      ChartData chartData = textShape.Chart.ChartData;
    //textShape.Chart.Legend.te = JsonData.twitter_account_analysis.content.content_type[0].name;
    chartData.Activate();

    Workbook workbook = chartData.Workbook;


    Microsoft.Office.Interop.PowerPoint.ChartArea chartArea = textShape.Chart.ChartArea;
    workbook.Application.Visible = false;
    //workbook.Application.Calculate();
    Worksheet dataSheet = workbook.Worksheets[1];
    //dataSheet.Tableupdate();

    //System.Threading.Thread.Sleep(50);
    int firstcolNumber = 2;
    int rowNumber = 2;

    //  Clearing prevIoUs data
    //dataSheet.UsedRange.Columns[1,Type.Missing].Clear();
    //dataSheet.UsedRange.Columns[2,Type.Missing].Clear();

    //Dount chart

    for (int i = 0; i <99; i++)
    {
      if (i < JsonData.twitter_metrics.tweets_over_time.Length)
        {
          dataSheet.Cells[firstcolNumber + i,rowNumber].Clear();
        dataSheet.Cells[firstcolNumber + i,1].Clear();
        dataSheet.Cells[firstcolNumber + i,rowNumber] = JsonData.twitter_metrics.tweets_over_time[i].stats_count.ToString();
        dataSheet.Cells[firstcolNumber + i,1] = JsonData.twitter_metrics.tweets_over_time[i].id;

        }
      else
        {
          dataSheet.Cells[firstcolNumber + i,1].Clear();
        }

        }
        //dataSheet.get_Range("A1",$"B{JsonData.twitter_metrics.tweets_over_time.Length}").Select();
                                                    //Marshal.FinalReleaseComObject(dataSheet);
                                                    var yAxis = (Microsoft.Office.Interop.PowerPoint.Axis)textShape.Chart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlValue,Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);
                                                    var xAxis = (Microsoft.Office.Interop.PowerPoint.Axis)textShape.Chart.Axes(Microsoft.Office.Interop.PowerPoint.XlAxisType.xlCategory,Microsoft.Office.Interop.PowerPoint.XlAxisGroup.xlPrimary);


                                                }

                                                Marshal.ReleaseComObject(textShape);
                                            }
                                            Marshal.ReleaseComObject(slide);
                                            
                                            //System.Threading.Thread.Sleep(50);
                                            break;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)