我正在尝试将数据从一个工作簿过滤到另一个工作簿,但是我得到了一个未被证实的异常。
我找不到很多使用c#在excel中进行adavnced过滤的例子,我不知道我的错误究竟在哪里。
这是我的代码:
public void excel()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBookImport;
Excel.Workbook xlWorkBookOriginal;
Excel.Worksheet xlWorkSheetImport;
Excel.Worksheet xlWorkSheetOriginal;
xlApp = new Excel.ApplicationClass();
xlWorkBookImport = xlApp.Workbooks.Open("c:/open.xls");
xlWorkBookOriginal = xlApp.Workbooks.Open("c:/open1.xls");
xlWorkBookOriginal.Activate();
xlWorkSheetOriginal = (Excel.Worksheet)xlWorkBookOriginal.Worksheets.get_Item(1);
xlWorkSheetOriginal.get_Range("1:7").Delete();
xlWorkBookImport.Activate();
xlWorkSheetImport = (Excel.Worksheet)xlWorkBookImport.Worksheets.get_Item(1);
xlWorkSheetOriginal.Cells.AdvancedFilter(Excel.XlFilterAction.xlFilterCopy, Type.Missing, CopyToRange: "a1:a6", Unique: true);
xlApp.Visible = true;
}
任何人都可以帮我解决问题吗?
答案 0 :(得分:1)
我最终弄明白了myeslf:
public void excel()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBookImport;
Excel.Workbook xlWorkBookOriginal;
Excel.Worksheet xlWorkSheetImport;
Excel.Worksheet xlWorkSheetOriginal;
xlApp = new Excel.ApplicationClass();
xlWorkBookImport = xlApp.Workbooks.Open("c:/open.xls");
xlWorkBookOriginal = xlApp.Workbooks.Open("c:/open1.xls");
xlWorkBookOriginal.Activate();
xlWorkSheetOriginal = (Excel.Worksheet)xlWorkBookOriginal.Worksheets.get_Item(1);
xlWorkSheetOriginal.get_Range("1:7").Delete();
// added code
Excel.Range range = xlWorkSheetOriginal.get_Range("a1:v53");
//
xlWorkBookImport.Activate();
xlWorkSheetImport = (Excel.Worksheet)xlWorkBookImport.Worksheets.get_Item(1);
// added code
xlWorkSheetImport.Activate();
Excel.Range target = xlWorkSheetImport.get_Range("a1:f1");
//
// modified code
xlWorkSheetOriginal.Cells.AdvancedFilter(Action : Excel.XlFilterAction.xlFilterCopy,
CriteriaRange: Type.Missing, CopyToRange: target, Unique: false);
//
xlApp.Visible = true;
}