Kha*_*isi 5 c# excel autofilter
问题:我的代码总是返回一行而不是 13。
代码:
Excel.Application xlApp = null;
Excel.Workbook xlWorkBook = null;
Excel.Worksheet xlWorkSheet = null;
Excel.Range xlrange = null;
string sCurrentDir = Directory.GetCurrentDirectory();
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(sCurrentDir + @"\Res\res.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlrange = xlWorkSheet.UsedRange;
xlrange.AutoFilter(5, "4", Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);
Excel.Range filteredRange = xlrange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Excel.XlSpecialCellsValue.xlTextValues);
MessageBox.Show(filteredRange.Rows.Count.ToString());
Run Code Online (Sandbox Code Playgroud)
如您所见,我想用值“4”过滤第 5 列。它应该返回 13 行,但它总是返回 1 行。
任何帮助将不胜感激
我回答我自己的问题:
...
...
Excel.Range filteredRange = xlrange.SpecialCells(Excel.XlCellType.xlCellTypeVisible, Excel.XlSpecialCellsValue.xlTextValues);
for (int areaId = 1; areaId <= filteredRange.Areas.Count; areaId++)
{
areaRange = filteredRange.Areas[areaId];
object[,] areaValues = areaRange.Value;
for (int row = 0; row < areaValues.GetLength(0); row++)
{
// ignore the Header row
if (areaValues[row + 1, 1].ToString().ToLower() == "id") continue;
//Add The Row To A List Or Array
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14109 次 |
| 最近记录: |