我已经为此工作了2天,我有包含类型和数字的EXCEL文件,我需要获得特定类型的所有数字,
我试过了,但对我不起作用:
public void ReadExcel(string SelectedType)
{
string excelName = "";
OpenFileDialog dialogRead = new OpenFileDialog();
dialogRead.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
dialogRead.ShowDialog();
excelName = dialogRead.FileName;
Application excelApp = new Application();
Workbook workbook = excelApp.Workbooks.Open(excelName);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook
.Sheets["INVENTORY "];
FilterByCellValue(worksheet, "V", SelectedType);
string newFilePath = "FilteredExcelFile.xlsx";
workbook.SaveAs(newFilePath);
excelApp.Quit();
}
static void FilterByCellValue(Microsoft.Office.Interop.Excel.Worksheet worksheet, string column, string cellValue)
{
Range dataRange = worksheet.UsedRange;
Range filterColumn = (Range)dataRange.Columns[column];
filterColumn.AutoFilter(1, cellValue, XlAutoFilterOperator.xlFilterValues, Type.Missing, true);
}
我希望得到 Select 类型的所有数字,但我得到了这个异常_AutoFilter method of Range class failed
.