zoukankan      html  css  js  c++  java
  • 使用C#和Excel进行报表开发(5)

    内容摘要:在用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();
      }
    作者:wpf之家
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    《你说对就队》第四次作业:项目需求调研与分析
    《你说对就对队》第三次作业:团队项目的原型设计
    《你说对就队》第二次作业:团队项目选题报告
    《你说对就队》第一次作业 团队亮相
    实验十一 团队作业7:团队项目设计完善&编码1
    《你们都是魔鬼吗 团队》 第六次作业 团队项目系统设计改进与详细设计
    团队项目改进
    《你们都是魔鬼吗团队》第四次作业:项目需求调研与分析
    《你们都是魔鬼吗团队》第三次作业:团队项目的原型设计
    <你们都是魔鬼吗>第二次团队作业:团队项目选题
  • 原文地址:https://www.cnblogs.com/wpf123/p/2347389.html
Copyright © 2011-2022 走看看