简体   繁体   English

C# Excel:获取行数和列数的正确方法

[英]C# Excel : Correct way to get Rows and Columns count

I have problem with C# Interop Excel get valid range with big file我在使用 C# Interop Excel 时遇到问题,获取大文件的有效范围

https://www.dropbox.com/s/betci638b1faw8g/Demo%20Training%20Data.xlsx?dl=0 https://www.dropbox.com/s/betci638b1faw8g/Demo%20Training%20Data.xlsx?dl=0

This is my file but real size is 1000 but I got 49998这是我的文件,但实际大小是 1000,但我得到了 49998

I used standard code我使用标准代码

Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(txtbTrainPath.Text);
Excel.Worksheet xlWorksheet = xlWorkbook.Worksheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;              
xlApp.Visible = true;
xlWorksheet.Columns[5].Delete();
xlWorksheet.Columns[3].Delete();              
rowCount = xlRange.Rows.Count;
colCount = xlRange.Columns.Count;

Only for this file is not work correctly, other files works well.只有这个文件不能正常工作,其他文件才能正常工作。 Please help me to find what is the problem.请帮我找出问题所在。 How to resize worksheet for only valid size.如何仅调整工作表的大小以获得有效大小。

I would use this approach to get the Rows and Columns count which will return the result of the cells which are not empty.我会使用这种方法来获取行和列计数,这将返回非空单元格的结果。

// Find the last real row
lastUsedRow = worksheet.Cells.Find("*",System.Reflection.Missing.Value, 
                               System.Reflection.Missing.Value, System.Reflection.Missing.Value, 
                               Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlPrevious, 
                               false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Row;

// Find the last real column
lastUsedColumn = worksheet.Cells.Find("*", System.Reflection.Missing.Value, 
                               System.Reflection.Missing.Value,System.Reflection.Missing.Value, 
                               Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious, 
                               false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Column;

Here is the complete code for your reference:以下是完整代码供您参考:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp     = null;
Excel.Workbook wb           = null;
Excel.Worksheet worksheet   = null;
int lastUsedRow             = 0;
int lastUsedColumn          = 0;
string srcFile              = @"Path to your XLSX file";

xlApp = new Excel.ApplicationClass();
xlApp.Visible = false;
wb = xlApp.Workbooks.Open(srcFile,
                               0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
                               true, false, 0, true, false, false);

worksheet = (Excel.Worksheet)wb.Worksheets[1];
Excel.Range range

// Find the last real row
lastUsedRow = worksheet.Cells.Find("*",System.Reflection.Missing.Value, 
                               System.Reflection.Missing.Value, System.Reflection.Missing.Value, 
                               Excel.XlSearchOrder.xlByRows,Excel.XlSearchDirection.xlPrevious, 
                               false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Row;

// Find the last real column
lastUsedColumn = worksheet.Cells.Find("*", System.Reflection.Missing.Value, 
                               System.Reflection.Missing.Value,System.Reflection.Missing.Value, 
                               Excel.XlSearchOrder.xlByColumns,Excel.XlSearchDirection.xlPrevious, 
                               false,System.Reflection.Missing.Value,System.Reflection.Missing.Value).Column;

xlApp.Workbooks.Close();
xlApp.Quit();

Marshal.ReleaseComObject(worksheet);
Marshal.ReleaseComObject(wb);
Marshal.ReleaseComObject(xlApp);

After downloading the excel file and taking a look at it, I found as I expected, formatting on row 49998 column C. The font was different, the number formatting was different and the font color was red.下载了excel文件看了一下,果然如我所料,在第49998行C列格式化,字体不同,数字格式不同,字体颜色为红色。 I simply deleted all the cells on row 49998 and this simply decreased the number of used range rows to 49997. You could play this guess which cells have formatting by deleting the last row then check again.我只是删除了第 49998 行上的所有单元格,这只是将使用的范围行数减少到 49997。您可以通过删除最后一行来猜测哪些单元格具有格式,然后再次检查。 I have a feeling ALL cells above row 49998 have some kind of different formatting.我有一种感觉,第 49998 行上方的所有单元格都有某种不同的格式。

To fix this one sheet to get the proper used range.修复这张纸以获得正确的使用范围。 You have two choices.你有两个选择。 1) Select the WHOLE row starting at row 1001, then scroll down to row 49998. With the SHIFT key pressed, click on the WHOLE row 49998. With rows 1001 to 49998 selected, right click on the selection and select "Delete" and possibly, if it asks, shift the cells up. 1) 选择从第 1001 行开始的整个行,然后向下滚动到第 49998 行。按住 SHIFT 键,单击第 49998 行。选择第 1001 到 49998 行,右键单击选择并选择“删除”,可能,如果它询问,请将单元格向上移动。 Or 2) copy the cells with data only and paste the cells into an new worksheet, delete the old worksheet and rename the new worksheet to the previous worksheet name.或 2) 仅复制包含数据的单元格并将单元格粘贴到新工作表中,删除旧工作表并将新工作表重命名为以前的工作表名称。 I hope this makes sense.我希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM