程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> excel選中范圍的邊框線型設置 因為多數邊框線為實線故作為例

excel選中范圍的邊框線型設置 因為多數邊框線為實線故作為例

編輯:.NET實例教程


private _Workbook _workBook = null;
        private Worksheet _workSheet = null;
        private Worksheet _workSheet2 = null;
        private Excel.Application _ExcelApplicatin = null;
        private Excel.Chart _ExcelChart = null;

          _excelApplicatin = new Excel.Application();
          _ExcelApplicatin.Visible = true;
          _ExcelApplicatin.DisplayAlerts = true;
          _workBook = _ExcelApplicatin.Workbooks.Add(XlSheetType.xlWorksheet);
          _workSheet = (Worksheet)_workBook.ActiveSheet;
          _workSheet.Name = "多數邊框線為實線";
          Excel.Range ExcelRange = _workSheet.get_Range(_workSheet.Cells[2, 2], _workSheet.Cells[6, 6]);
          ExcelBorderLineStlye(ExcelRange,"Up","Bottom"," Left" "Right");

         //excelRange :Excel選中范圍的邊框線型設置   因為多數邊框線為實線
        //NO :表示邊框沒有; "Up Bottom Left Right":表示線型為粗線 ;" "" "表示線型細線

        private void ExcelBorderLineStlye(Range ExcelRange, string Up, string Bottom, string Left, string Right)
        {
            List<string> borderWeightDivision = new List<string>();
            borderWeightDivision.Add(Up);
            borderWeightDivision.Add(Bottom);
            borderWeightDivision.Add(Left);
            borderWeightDivision.Add(Right);

            for (int i = 0; i < borderWeightDivision.Count; i++)
            {
                if (borderWeightDivision[i].Length > 0)
                {
                    if (!borderWeightDivision[i].Equals("NO"))
                    {
     &nb
sp;                  switch (i)
                        {
                            //Top
                            case 0:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeTop).Weight = XlBorderWeight.xlMedium;
                                break;
                            //Bottom
                            case 1:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlMedium;
                                break;
                            //"Left"
                            case 2:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeLeft).Weight = XlBorderWeight.xlMedium;
                                break;
                            //"Right"
              &nbsp;             case 3:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeRight).Weight = XlBorderWeight.xlMedium;
                                break;
                        }
                    }
                    else
                    {
                        switch (i)
                        {
                            //Top
                            case 0:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlLineStyleNone;
                                break;
                            //Bottom
                            case 1:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlLineStyleNone;
                
;    break;
                            //"Left"
                            case 2:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlLineStyleNone;
                                break;
                            //"Right"
                            case 3:
                                ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle .xlLineStyleNone;
                                break;
                        }
                    }
                }
                else
                {
                    switch (i)
                    {
                        //Top
                        case 0:
                            ExcelRange.Borders.get_Item(XlBordersI
ndex.xlEdgeTop).Weight = XlBorderWeight.xlThin;
                            break;
                        //Bottom
                        case 1:
                            ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlThin;
                            break;
                        //"Left"
                        case 2:
                            ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeLeft).Weight = XlBorderWeight.xlThin;
                            break;
                        //"Right"
                        case 3:
                            ExcelRange.Borders.get_Item(XlBordersIndex.xlEdgeRight).Weight = XlBorderWeight.xlThin;
                            break;
                    }
                } 
            }
        }            

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved