彩票走势图

使用C#和Excel进行报表开发(五)-操作单元格边框和颜色

转帖|其它|编辑:郝浩|2011-10-28 15:08:51.000|阅读 803 次

概述:在用Excel作报表的时候,可能需要操作单元格的边框和填充颜色和纹理等操作,下面的代码说明如何设置选中的单元格的填充纹理和边框。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  在用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();

}


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn

文章转载自:博客园

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP