一、参考文章
http://blog.csdn.net/meifage9/article/details/6960747
C# 操作 Excel 单元格自动填充,居中对齐,字体颜色设置
- //设置标题
- Excel.Range titleRange = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnCount]);//选取单元格,选取一行或多行
- titleRange.Merge(true);//合并单元格
- titleRange.Value2 = strTitle; //设置单元格内文本
- titleRange.Font.Name = "宋体";//设置字体
- titleRange.Font.Size = 18;//字体大小
- titleRange.Font.Bold = true;//加粗显示
- titleRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
- titleRange.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
- titleRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;//设置边框
- titleRange.Borders.Weight = Excel.XlBorderWeight.xlMedium;
二、实战案例
1、案例代码
- /* 创建Excel实例及一个Range实例*/
- Microsoft.Office.Interop.Excel.Application appExcel;
- Microsoft.Office.Interop.Excel.Range rangedata; // <=>等价是一Row
-
- /* 设置A1-H1行头字体的大小 */
- rangedata = (Microsoft.Office.Interop.Excel.Range)appExcel.get_Range("A1", "H1");
- rangedata.Font.Size = 20;
- rangedata.Font.Bold = true;
-
- /* 设置行头字体居中 */
- rangedata.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //HorizontalAlignment.Center;
- rangedata.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter ;
- /* 设置每个单元格字体居中 */
- appExcel.Columns.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
-
- /* 设置每个单元格的宽度*/
- appExcel.Columns.ColumnWidth = 14;
2、运行效果图
图 单元格居中前效果
图 单元格居中后效果
阅读(5572) | 评论(0) | 转发(0) |