21.2.6 字体和边框
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
通过设置Rang.Font对象的几个成员就可以修改字体,设置Range.Borders就可以修改边框样式。
【例 21.6】【项目:code21-006】修改字体、边框。
private void Button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
Workbook Wbook = xls.Workbooks.Open("c:\\lessons\\Northwind.xls");
Worksheet Wsheet = Wbook.Worksheets["雇员"];
//修改第一行的字体
Range currentRange = Wsheet.Rows[1];
currentRange.Font.Color = XlRgbColor.rgbBlack;
currentRange.Font.Bold = true;
currentRange.Font.Name = "宋体";
currentRange.Font.Size = 12;
//修改第一列除第一个单元格外的字体
currentRange = Wsheet.Range["A2:A11"];
currentRange.Font.Bold = true;
//设置库存量为0的单元格为加粗红色
for(int i = 2;i<= Wsheet.UsedRange.Rows.Count;i++)
{
currentRange = Wsheet.Range["D" + i];
if(currentRange.Value !=null)
{
if(currentRange.Value.ToString() == "0")
{
currentRange.Font.Color = XlRgbColor.rgbRed;
currentRange.Font.Bold = true;
}
}
}
//为整个表格有效区域设置蓝色边框
currentRange = Wsheet.Range[Wsheet.Cells[1, 1], Wsheet.Cells[Wsheet.UsedRange.Rows.Count, Wsheet.UsedRange.Columns.Count]];
currentRange.Borders.LineStyle = XlLineStyle.xlContinuous;
currentRange.Borders.Color = XlRgbColor.rgbBlue;
Wbook.Save();
xls.Quit();
MessageBox.Show("处理完毕");
}
运行结果如下图所示:
图21-9 工作表修改前后对比
学习更多vb.net知识,请参看vb.net 教程 目录
学习更多C#知识,请参看C#教程 目录