本文实例讲述了C#控制Excel Sheet使其自适应页宽与列宽的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
///// <summary>
/// 1.Clear CircleReference
/// 2.Set Page to Fit Wide
/// 3.Set Column Text fit
/// </summary>
/// <param name="app"></param>
/// <param name="ws"></param>
private void WorkSheetPageSet(Microsoft.Office.Interop.Excel.Application app, Worksheet ws)
{
ClearCircleReference(ws);
SetPagetoFitWide(ws);
SetColumnFit(ws);
}
///// <summary>
/// Set Column Text fit
/// </summary>
/// <param name="sheet"></param>
private static void SetColumnFit(Worksheet sheet)
{
char column = 'B' ;
for ( int i = 0; i < 25; i++)
{
Range range = sheet.get_Range(String.Format( "{0}1" , column.ToString()),
String.Format( "{0}1" , column.ToString()));
if (range != null )
{
range.EntireColumn.AutoFit();
}
column++;
}
}
///// <summary>
/// Clear CircleReference
/// </summary>
/// <param name="sheet">Worksheet object</param>
private void ClearCircleReference(Worksheet sheet)
{
Range range = sheet.CircularReference;
while (range != null )
{
range.Clear();
range = sheet.CircularReference;
}
}
///// <summary>
/// Set Page to Fit Wide
/// </summary>
/// <param name="ws">Worksheet object</param>
private static void SetPagetoFitWide(Worksheet ws)
{
ws.PageSetup.Zoom = false ;
ws.PageSetup.FitToPagesWide = 1;
ws.PageSetup.FitToPagesTall = false ;
}
|
希望本文所述对大家C#程序设计有所帮助。