使用C#和Excel进行报表开发(五)-操作单元格边框和颜色 【转】
在用Excel作報表的時候,可能需要操作單元格的邊框和填充顏色和紋理等操作,下面的代碼說明如何設置選中的單元格的填充紋理和邊框
?
try
{
?????? ThisApplication = new Excel.Application();
?
????? ThisWorkbook = ThisApplication.Workbooks.Open("z:\\Book1.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
????????????????????
?
?????? ThisApplication.DisplayAlerts = false;
?????? xlSheet = (Excel.Worksheet)ThisWorkbook.Worksheets.get_Item(1);
?????? Excel.Range range = xlSheet.get_Range("G4","H5");
?????? range.Value = "123";
???????????????????????????
?????? Excel.Style st = ThisWorkbook.Styles.Add("PropertyBorder", Type.Missing);
???????????????????????????????????????????????????????
?????? range.Interior.Pattern = Excel.XlPattern.xlPatternCrissCross;
???????range.Borders.Weight = 2;
?????? range.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous;
???????range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous;
???????range.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
???????range.Borders.get_Item(XlBordersIndex.xlDiagonalDown).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
???????range.Borders.get_Item(XlBordersIndex.xlDiagonalUp).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
???????range.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
???????range.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
???????range.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlDot;
???????range.Borders.get_Item(XlBordersIndex.xlEdgeLeft).Color = System.Drawing.ColorTranslator.ToOle(Color.Red);
???????????????????????????
?????? ThisWorkbook.SaveAs("z:\\Book2.xls", Type.Missing, Type.Missing,
????????????? Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
????????????? Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
?????? MessageBox.Show(ex.Message);
}
finally
{
?????? ThisWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
?????? ThisApplication.Workbooks.Close();
?
?????? ThisApplication.Quit();
???????System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisWorkbook);
???????System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisApplication);
?????? ThisWorkbook = null;
?????? ThisApplication = null;
?????? GC.Collect();
?????? this.Close();
}
轉載于:https://www.cnblogs.com/fmxyw/archive/2008/09/09/1287848.html
總結
以上是生活随笔為你收集整理的使用C#和Excel进行报表开发(五)-操作单元格边框和颜色 【转】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ps2模拟器bios3dm_龙漫ps2模
- 下一篇: c# char unsigned_dll