C#设置Excel行高、列宽 - mol1995

时间:2024-02-20 10:10:55

C#设置Excel行高、列宽

设置固定值 worksheet.Columns[1].ColumnWidth = 15;

 设置自动换行 worksheet.Columns.WrapText = true;

设置自动行高、列宽

 xlApp.Columns.AutoFit();
 xlApp.Rows.AutoFit();

对齐方式

worksheet.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; // 对齐方式

worksheet.Columns[3].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; // 对齐方式

 

其它

•sheet.Range["A1", "F" + (c + 2)].Borders.LineStyle = 1;  // 边框粗细

•sheet.Range["A1", "F" + (c + 2)].HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter; // 对齐方式

•sheet.Range["A1", "F1"].MergeCells = true;  // 合并单元格

•sheet.Range["A1", "F2"].Font.Bold = true;  // 加粗

•sheet.Range["A1", "F1"].Font.Size = 13;  // 字号

•sheet.Range["A1", "F" + (c + 2)].Font.Size = 11;

•sheet.Range["A1", "F" + (c + 2)].RowHeight = 21;  // 行高

•sheet.Range["A1"].RowHeight = 35;

•sheet.Range["A1", "F1"].ColumnWidth = 17;  // 列宽

•sheet.Range["B1"].ColumnWidth = 20;

•sheet.Range["A2", "F2"].Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.FromArgb(197, 217, 241));  // 单元格背景颜色