版權聲明:本文為博主原創文章,轉載請在顯著位置標明本文出處以及作者網名,未經作者允許不得用于商業目的。
通過設置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#教程 目錄