我正在尝试提取Excel单元格值。我能够成功地获取行值。如何将每个单元格值拉出行?
using Microsoft.Office.Interop.Excel;
string pathToExcelFile = @"C:\Users\MyName\Desktop\Log.xls";
Application xlApp = new Application();
Workbook xlWorkbook = xlApp.Workbooks.Open(pathToExcelFile, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
_Worksheet xlWorksheet = (_Worksheet)xlWorkbook.Sheets[1];
Range xlRange = xlWorksheet.UsedRange;
var rowValue = ((Range)xlRange.Cells[2, 1]).Value2.ToString();
答案 0 :(得分:2)
试试这个:
foreach (Range c in xlRange.Cells)
{
Console.WriteLine("Address: " + c.Address + " - Value: " + c.Value);
}
我的测试文件输出:
完整代码:
string testingExcel = @"C:\TestingExcel.xlsx";
Application xlApp = new Application();
Workbook xlWorkbook = xlApp.Workbooks.Open(testingExcel, Type.Missing, true);
_Worksheet xlWorksheet = (_Worksheet)xlWorkbook.Sheets[1];
Range xlRange = xlWorksheet.UsedRange;
foreach (Range c in xlRange.Rows.Cells)
{
Console.WriteLine("Address: " + c.Address + " - Value: " + c.Value);
}
xlWorkbook.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkbook);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp);
多行编辑输入: