计算WPF中excel列的动态单元格/行的总和

时间:2012-04-29 12:41:57

标签: wpf excel

请问,是否有任何方法可以添加(总结)给定列的单元格(行)的内容,其中可能的行(单元格)的数量是动态的,即行数不是修复,在Excel工作表中?我一直在尝试没有任何有意义的结果,我尝试使用偏移但继续从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;



    }  

2 个答案:

答案 0 :(得分:1)

我不相信xlWorkSheet.Cells.Offset[i, 0].Value2有效。不要使用Offset,而只需坚持使用Cells(i, 1).Value2。如果这对您有用,请告诉我。

答案 1 :(得分: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();

//“&&”是非常必要的,以避免添加单元格的值(可能是一个字符串 - 如列的标题)加倍,以避免出错。