1. 程式人生 > >VSTO---EXCEL常用處理方法

VSTO---EXCEL常用處理方法

1. 當前工作簿:workBook = Globals.ThisAddIn.Application.ActiveWorkbook;
2. 當前工作表:workSheet = (Excel.Worksheet)workBook.ActiveSheet;
3.工作簿名:    workBookName = workBook.Name;
4.工作表名:    workSheetName = workSheet.Name;

5.排序:
                Range myRange = (Excel.Range)workSheet.Cells[1, 1];
                workSheet.Sort.SortFields.Add(myRange, Excel.XlSortOn.xlSortOnValues,    Excel.XlSortOrder.xlAscending, Type.Missing, Excel.XlSortDataOption.xlSortNormal);
                myRange = workSheet.get_Range("A1", "A10");
                workSheet.Sort.SetRange(myRange);
                workSheet.Sort.Header = Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo;
                workSheet.Sort.MatchCase = false;
                workSheet.Sort.Orientation = Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns;
                workSheet.Sort.SortMethod = Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin;
                workSheet.Sort.Apply();

6.設定單元格格式:
      //顯示格式
      setRange.NumberFormatLocal = "$#,##0_);[紅色]($#,##0)";  
      //背景色
      setRange.Interior.ColorIndex = 3;
      //邊框
      setRange.Cells.Borders.ColorIndex = 1;
      //設定單元格中不同字元為不同顏色,這個功能只有到07後才有的  
      Range rangeStyle = (Range)mgrSummary.UsedRange[changeStart, 1];
         if (rangeStyle != null)
         {
               object styleValue = rangeStyle.Value2;
               if (styleValue != null)
               {
                    Characters changeStyle = rangeStyle.get_Characters(0, rangeStyle.Value2.ToString().Length);
                     changeStyle.Font.Color = Color.Red.ToArgb();
                }
         }

7.查詢單元格內容:
      usedRange.Find(string, miss, XlFindLookIn.xlFormulas, XlLookAt.xlWhole, XlSearchOrder.xlByRows, XlSearchDirection.xlNext, false, false, miss);
8.單元格區域拷貝:
                worksheetRange = worksheet.get_Range(columnName, miss);
                newWorksheetRange = newWorkSheet.get_Range(columnName, miss);
                worksheetRange.Copy(newWorksheetRange);

9.產生資料透視表
        private Worksheet GeneratetPivot(Worksheet worksheet, PivotCaches pivotCaches)
        {
            Worksheet worksheetPivot = (Worksheet)worksheets.Add(miss, worksheet, 1, miss);
            worksheetPivot.Name = PivotName;
            PivotCache pivotCache = pivotCaches.Add(XlPivotTableSourceType.xlDatabase, worksheetLoan.UsedRange);
            Range range = worksheetPivot.get_Range("A1", miss);

            //建立資料透視表
            PivotTable pivotTable = pivotCache.CreatePivotTable(range, tPivotName, true, XlPivotTableVersionList.xlPivotTableVersionCurrent);
            //Adds row, column, and page fields to a PivotTable report or PivotChart report
            pivotTable.AddFields("Name", miss, miss, true);
            pivotTable.AddFields("Amount", miss, miss, true);
            PivotField pivotField = (PivotField)pivotTable.PivotFields("Name");
            pivotField.Orientation = XlPivotFieldOrientation.xlRowField;
            pivotField = (PivotField)pivotTable.PivotFields("Amount");
            pivotField.Orientation = XlPivotFieldOrientation.xlDataField;
            pivotField.Function = XlConsolidationFunction.xlSum;
            return worksheetPivot;
        }