从多个excel中访问特定工作表的列值

时间:2013-09-26 11:47:16

标签: c# excel

我正在尝试访问文件夹中存在的多个Excel文件,然后查找特定工作表,然后搜索特定列(页眉位于工作表的第一行)。该列包含数值,我需要对工作表的该列的所有值求和,然后将总和粘贴到创建新Excel的表中。输出excel应包含具有该列的所有值的值的表我访问的擅长

PlannedEffort || 实际工作量 || 部署 ||的

粗体字母字段是Excel工作表的标题,我需要找到文件夹中存在的所有Excel的“实际工作量”列的数值之和。

以下是我的代码。在搜索列后,我一直在阅读列值。我正在使用C#语言与Microsoft.Interop dll ver 12.0和系统中存在MS excel 2007

课程计划     {

    static void Main(string[] args)
    {
        Excel.Application application = new Excel.Application();

        Excel.Workbook xlWorkBook;
        Excel.Sheets sheets;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;
        System.Array myValues;
        string findName = "Actual Effort";

        string[] path = Directory.GetFiles(@"C:\Users\Documnents\Projects\ReadExcelApp\*.xls");

       foreach (string xlPath in path)
        {
            xlWorkBook = application.Workbooks.Open(xlPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            sheets = xlWorkBook.Worksheets;
            xlWorkSheet = (Excel.Worksheet)sheets.get_Item(3);
            range = xlWorkSheet.UsedRange;

            string findColumnValue = RetrieveColumnValue(xlWorkSheet, findName);
           //string colCount = findColumnValue.(Stuck at this line:how to access the column values)

        }
    }
    public static string RetrieveColumnValue(Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet, string findName)
    {
        Excel.Range rng = xlWorkSheet.UsedRange;
        Excel.Range rngResult = null;
        rngResult = rng.Find(findName, Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext,
            Type.Missing, Type.Missing, Type.Missing);
        if (rngResult != null)
        {
            Excel.Range cRng = null;
            int rowused = rng.Rows.Count;

            string returnStr;
            for (int i = 1; i < rowused; i++)
            {
                cRng = (Excel.Range)xlWorkSheet.Cells[i, rngResult.Column];
                if (cRng != null)
                {
                    returnStr = cRng.Name.ToString();
                }

            }
            return returnStr;
        }
        else
        {
            return string.Empty;
        }
    }

}

此外,有可能一旦运行,我是否可以在具有MS Office 2010的系统中执行exe?

1 个答案:

答案 0 :(得分:0)

尝试此操作来对列值求和:

        decimal returnVal = 0;
        for (int i = 1; i < rowused; i++) //NB: off-by-one error here? Shouldn't it be from 1 to <= rows ?
        {
            cRng = (Excel.Range)xlWorkSheet.Cells[i, rngResult.Column];
            if (cRng != null)
            {
                decimal currentVal;
                if( decimal.TryParse(cRng.Value2.ToString(), out currentVal) )
                    returnVal += currentVal;
            }
        }
        return returnVal.ToString();
相关问题