本文实例讲述了C#控制Excel Sheet使其自适应页宽与列宽的方法。分享给大家供大家参考,具体如下:
///// <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#相关内容感兴趣的读者可查看本站专题:《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#面向对象程序设计入门教程》及《C#程序设计之线程使用技巧总结》
希望本文所述对大家C#程序设计有所帮助。
新闻热点
疑难解答