我正在尝试访问文件夹中存在的多个Excel文件,然后查找特定的工作表,然后搜索特定的列( Headers 位于工作表的第一行) . 该列包含数值,我需要对工作表的该列的所有值求和,然后将总和粘贴到创建新excel的表中 . 输出excel应包含具有该列的所有值的值的表我访问的擅长
PlannedEffort || Actual effort || Deployed By || Month
粗体字母字段是Excel工作表的 Headers ,我需要找到文件夹中存在的所有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 回答
尝试此操作来对列值求和: