Имеет ли VSTO доступ к содержимому флажков фильтра Excel?

1

Можно ли программно извлекать и устанавливать отметки на скриншоте?

Изображение 174551

ТИА

  • 1
    Эта возможность будет особенно приветствоваться пользователями Office 2007, когда им приходится иметь дело с большим количеством строк.
Теги:
excel
visual-studio-2010
vsto

2 ответа

1
Лучший ответ

Я создал консольное приложение в Visual Studio 2010 и добавил COM-ссылку на библиотеку объектов Microsoft Excel X.0. X будет отличаться в зависимости от версии Excel, которую вы установили. Вот код, который вы должны использовать:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace OpenExcel {
class Program {
    static void Main(string[] args) {
        Excel.Application xlApp = null;
        try {
            xlApp = new Excel.Application();
            xlApp.Visible = true;
            Excel.Workbook xlWb = xlApp.Workbooks.Open(@"C:\Users\joe.bob\Desktop\tmp\Book1.xlsx"); //include the path to your real Excel file
            Excel.Worksheet xlWSht = xlWb.Sheets["Sheet1"]; //include the name of your worksheet where you have the data
            //here the data is on a Worksheet called Sheet1

            int startRow = 9; //this is the row where the data starts
            string startCol = "A"; //the start column
            int endRow = 15;
            string endCol = "F";
            int filterColumn = 6; //this is an OFFSET
            string[] filterList = new string[] { "DTP-3432", "DTP-343243" }; //this is the list of values you want to show
            Excel.Range myData = xlWSht.get_Range(startCol + startRow, endCol + endRow);
            myData.AutoFilter(filterColumn, filterList.Length > 0 ? filterList : Type.Missing, Excel.XlAutoFilterOperator.xlFilterValues, Type.Missing, true);
            Console.WriteLine("Press a key to quit...");
            Console.ReadKey();
        }
        finally {
            if (xlApp != null) {
                xlApp.Quit();
            }
        }
    } //end main
} //end program
} //end namespace

Для получения дополнительной информации см. Следующую ссылку: http://msdn.microsoft.com/en-us/library/office/microsoft.office.interop.excel.range.autofilter%28v=office.15%29.aspx

http://blogs.msdn.com/b/erikaehrli/archive/2005/10/27/excelmanagedautofiltering.aspx

  • 0
    «X будет отличаться в зависимости от установленной версии Excel» Это будет Excel 2007.
0

Вот как тянуть проверенные предметы в VBA. Он легко перейдет на С#/VSTO:

Sub GetFilterCriteria()
Dim i As Long
Dim afCriteria1 As Variant

afCriteria1 = ActiveSheet.AutoFilter.Filters(1).Criteria1
For i = LBound(afCriteria1) To UBound(afCriteria1)
    Debug.Print afCriteria1(i)
Next i
End Sub

Ещё вопросы

Сообщество Overcoder
Наверх
Меню