gpt4 book ai didi

c# - 使用 Microsoft.Office.Interop.Excel 读取所有行和列

转载 作者:行者123 更新时间:2023-11-30 22:03:43 25 4
gpt4 key购买 nike

我有 4 列 4 行的 Excel 工作表。当我使用 Microsoft.Office.Interop.Excel.WorkSheet 读取列和行 Count 时,行数和列数分别为 1048576 行和 16384 列。计数必须行为 4,列为 4。我错过了什么?

            ApplicationClass excelApp = null;
Workbook myWorkBook = null;
Worksheet mySheet = null;
Range dataRange = null;
excelApp = new ApplicationClass();
myWorkBook = excelApp.Workbooks.Open(@"C:\Users\Dev2Admin\Desktop\Employees.xlsx");
mySheet = (Worksheet)myWorkBook.Sheets["Sheet1"];


for (int row = 1; row < mySheet.Rows.Count; row++) // Count is 1048576 instead of 4
{
for (int col = 1; col < mySheet.Columns.Count; col++) // Count is 16384 instead of 4
{
dataRange = (Range)mySheet.Cells[row, col];
Console.Write(String.Format(dataRange.Value2.ToString() + " "));
}
Console.WriteLine();
}

最佳答案

您省略了 UsedRange 属性,在您的工作表上正确使用的列或行计数如下所示:

  int totalColumns = mySheet.UsedRange.Columns.Count;
int totalRows = mySheet.UsedRange.Rows.Count;

关于c# - 使用 Microsoft.Office.Interop.Excel 读取所有行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25833425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com