·天新网首页·加入收藏·设为首页·网站导航
数码笔记本手机摄像机相机MP3MP4GPS
硬件台式机网络服务器主板CPU硬盘显卡
办公投影打印传真
家电电视影院空调
游戏网游单机动漫
汽车新车购车试驾
下载驱动源码
学院开发设计
考试公务员高考考研
业界互联网通信探索
您现在的位置:天新网 > 软件开发 > .Net开发 > C#
使用C#和Excel进行报表开发(5)
http://www.21tx.com 2007年11月20日

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

上一篇: C# Mines(布雷) 代码
下一篇: 使用C#和Excel进行报表开发(6)

关于我们 | 联系我们 | 加入我们 | 广告服务 | 投诉意见 | 网站导航
Copyright © 2000-2011 21tx.com, All Rights Reserved.
晨新科技 版权所有 Created by TXSite.net