【发布时间】:2018-11-18 00:48:01
【问题描述】:
我需要在 Excel 文件的第一张表和第二张表中从“B2”读取到“H10”(5 行 7 列)。我下面的代码适用于从两张纸上读取每个单元格,我怎样才能从两张纸上读取我需要的单元格? (我看到很多使用 activeworksheet 的解决方案,但没有指定它正在阅读哪个工作表,这无法解决我的问题。)
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
string str;
int rCnt;
int cCnt;
int rw = 0;
int cl = 0;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open("PATH", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("sheetName");
range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
for (rCnt = 1; rCnt <= rw; rCnt++)
{
for (cCnt = 1; cCnt <= cl; cCnt++)
{
str = ((range.Cells[rCnt, cCnt] as Excel.Range).Value2).ToString();
MessageBox.Show(str);
}
}
【问题讨论】:
标签: c# excel excel-interop