Method for C to control Excel Sheet to adapt page width and column width
- 2021-10-15 11:18:25
- OfStack
This paper gives an example of how C # controls Excel Sheet to adapt page width and column width. Share it for your reference, as follows:
///// <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;
}
For more readers interested in C # related content, please check the topics on this site: "Summary of C # Operating Excel Skills", "Summary of XML File Operation Skills in C #", "Tutorial on Usage of C # Common Controls", "Summary on Usage of WinForm Controls", "Tutorial on Data Structures and Algorithms of C #," Introduction to C # Object-Oriented Programming "and" Summary on Thread Use Skills of C # Programming "
I hope this article is helpful to everyone's C # programming.