1

可能な行(セル)の数が動的である、つまり行の数が固定されていない特定の列のセル(行)の内容を合計(合計)する方法はありますか?エクセルワークシート?意味のある結果が得られずに試してきました。offsetを使用してみましたが、HRESULTからCOM_Exceptionが発生し続けています。

以下は、私の目的を達成するために使用しようとしているデモコードの一部です。

(コードスニペットを含む)提案をいただければ幸いです。

 private void button1_Click(object sender, RoutedEventArgs e)

       {


        excel.Application xlApp;

        excel.Workbook xlWorkBook;

        excel.Worksheet xlWorkSheet;


        object misValue = System.Reflection.Missing.Value;



        xlApp = new excel.Application();

        var MyExcel = new excel.Application();

        xlWorkBook = xlApp.Workbooks.Add(misValue);



        xlWorkSheet = (excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        excel.Range chartRange;
        chartRange = xlWorkSheet.get_Range("$A:$A", Type.Missing);

        xlWorkSheet.Cells[1, 1] = "Temperature(deg)";
        xlWorkSheet.Cells[1, 2] = "Humidity(m-3)";
        xlWorkSheet.Cells[1, 3] = "Wind Speed(m/s)";
        xlWorkSheet.Cells[2, 1] = 33;
        xlWorkSheet.Cells[2, 2] = 45;
        xlWorkSheet.Cells[2, 3] = 34;
        xlWorkSheet.Cells[3, 1] = 23;
        xlWorkSheet.Cells[3, 2] = 26;
        xlWorkSheet.Cells[3, 3] = 43;
        xlWorkSheet.Cells[4, 1] = 45;
        xlWorkSheet.Cells[4, 2] = 24;
        xlWorkSheet.Cells[4, 3] = 34;
        xlWorkSheet.Cells[5, 1] = 40;
        xlWorkSheet.Cells[5, 2] = 32;
        xlWorkSheet.Cells[5, 3] = 42;



        decimal fdt = 0;
        int i = 2;

        excel.Range targetRange = (excel.Range)xlWorkSheet.Cells[2, 1];

        while (xlWorkSheet.Cells.Offset[i, 0].Value2 != null)
        {
            i++;
            fdt += xlWorkSheet.Cells.Offset[i, 0].Value2;
        }

        MessageBox.Show(fdt.ToString());

  excel.ChartObjects xlCharts = (excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
  excel.ChartObject myChart = (excel.ChartObject)xlCharts.Add(400, 50, 400, 300);
  excel.Chart chartPage = myChart.Chart;      
  chartPage.SetSourceData(chartRange, misValue);
  chartPage.ChartType = excel.XlChartType.xl3DColumnClustered;

  try
  {
      myChart.Chart.HasTitle = true;
      excel.Range objRange = (excel.Range)xlWorkSheet.Cells["$A:$A", Type.Missing];

      String strData = objRange.get_Value(misValue).ToString();

      myChart.Chart.ChartTitle.Text = strData;


  }
  catch (Exception ex)
  {
      MessageBox.Show(ex.ToString());
  }

             xlWorkBook.SaveAs("DemoChart_1.xls", excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,

    excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);


             xlApp.Visible = true;



    }  
4

2 に答える 2

1

xlWorkSheet.Cells.Offset[i, 0].Value2私はそれが有効だとは思わない。を使用する代わりに、を使用OffsetしてCells(i, 1).Value2ください。それがあなたのために働くかどうか私に知らせてください。

于 2012-04-30T04:46:28.513 に答える
1

気にしないでください、私はそれを自分で行うことができました。そして、私のようにこの種の問題で立ち往生するかもしれない人のために、ここに行きます:

            int numRows = 0;
            double sum = 0;
            double Average = 0;
            excel.Range count = xlWorkSheet.UsedRange.Columns["A", misValue] as excel.Range;
            foreach (excel.Range cell in count.Cells)
            {
                if (cell.Value2 != null && cell.Value2 is double?)

                 {
                    sum += cell.Value2;
                    numRows += 1;
                }

            }


            Average = sum / numRows;
           txtAverage.Text = Average.ToString();

//エラーが発生しないように、セルの値(列のヘッダーなどの文字列など)をdoubleに追加しないようにするには、「&&」が非常に必要です。

于 2012-05-04T06:15:02.053 に答える