C#控制Excel Sheet使其自适应页宽与列宽的方法
程序员文章站
2022-05-18 14:23:08
本文实例讲述了c#控制excel sheet使其自适应页宽与列宽的方法。分享给大家供大家参考,具体如下:
/////
/// 1...
本文实例讲述了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#程序设计有所帮助。