程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 通過C#控制Excel Sheet,使其自適應頁寬以及列寬

通過C#控制Excel Sheet,使其自適應頁寬以及列寬

編輯:C#入門知識

1/**//// <summary>
 2        /// 1.Clear CircleReference
 3        /// 2.Set Page to Fit Wide
 4        /// 3.Set Column Text fit
 5        /// </summary>
 6        /// <param name="app"></param>
 7        /// <param name="ws"></param>
 8        private void WorkSheetPageSet(Microsoft.Office.Interop.Excel.Application app, Worksheet ws)
 9        {
10            ClearCircleReference(ws);
11
12            SetPagetoFitWide(ws);
13
14            SetColumnFit(ws);
15        }
16
17        /**//// <summary>
18        /// Set Column Text fit
19        /// </summary>
20        /// <param name="sheet"></param>
21        private static void SetColumnFit(Worksheet sheet)
22        {
23            char column = B;
24            for (int i = 0; i < 25; i++)
25            {
26
27                Range range = sheet.get_Range(String.Format("{0}1", column.ToString()),
28                    String.Format("{0}1", column.ToString()));
29                if (range != null)
30                {
31                    range.EntireColumn.AutoFit();
32                }
33                column++;
34            }
35        }
36
37        /**//// <summary>
38        /// Clear CircleReference
39        /// </summary>
40        /// <param name="sheet">Worksheet object</param>
41        private void ClearCircleReference(Worksheet sheet)
42        {
43            Range range = sheet.CircularReference;
44            while (range != null)
45            {
46                range.Clear();
47                range = sheet.CircularReference;
48            }
49        }
50
51        /**//// <summary>
52        /// Set Page to Fit Wide
53        /// </summary>
54        /// <param name="ws">Worksheet object</param>
55        private static void SetPagetoFitWide(Worksheet ws)
56        {
57            ws.PageSetup.Zoom = false;
58            ws.PageSetup.FitToPagesWide = 1;
59            ws.PageSetup.FitToPagesTall = false;
60        }
 

    

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