首页 > 解决方案 > 如何使用 Microsoft.Office.Interop 自动从 Excel 中选择数据

问题描述

我在用

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

我在 PowerPoint 中有图表,例如 在此处输入图像描述

并且擅长

在此处输入图像描述

当添加超过 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, rowNumber].Clear();
        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;

标签: c#office-interopcom-interopexcel-interop

解决方案


推荐阅读