在用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进行报表开发(五)-操作单元格边框和颜色 【转】相关推荐

  1. 使用C#和Excel进行报表开发(一)--开始

    C#操作Excel进行报表开发系列共写了八篇,也已经有很久没有新东西了,现在整理一下,方便以后查阅,如果有写新的,会同时更新.需要注意的是因为Office的版本不同,实际的代码可能会有所不同,但是都是 ...

  2. Excel 2010光标移动到单元格边框时,不显示小十字(亲自实践)

    今天被问到一个现象,某台机子打开所有Excel,光标移动到单元格边框时,不显示小十字 也就是无法进行公式填充 2010版跟07版的解决方法的位置还不一样 2010版解决方法如下: Excel选项--高 ...

  3. 使用C#和Excel进行报表开发(四)-复制单元格 【转】

    在用Excel作报表的时候,可能会碰到分页的情况,这样就要分成多个Sheet显示,如果要在每页都保留表头,就需要将第一页作为模板设计的表头部分拷贝到其他的Sheet上,这时就要用用到Excel中的单元 ...

  4. 使用C#和Excel进行报表开发(5)

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

  5. poi 导出excel实战与word新增行与excel添加背景色与设置单元格边框

    提示:本文尽可能简洁通俗的讲解[poi 导出excel实战] ,如需导入可见文尾 一.导入依赖: <!--Apache poi--><!--xls(03)--><depe ...

  6. poi 合并单元格添加边框_如何在Excel中的文本和单元格边框之间添加空间

    poi 合并单元格添加边框 Adding a bit of extra padding to your cell boundaries can make it easier for you to re ...

  7. ABAP对excel的操作(为单元格填充背景颜色、设置border等)

    文章目录 需求 一.效果 二.代码 寄语 需求 今日需要对excel的部分内容进行强调色展示(填充背景色).设置border等等,发现网上都没有找到我想要的,然后自己找到了demo供大家参考 一.效果 ...

  8. ABAP对excel的操作(合并单元格,设置边框)

    文章目录 前言 一.效果 二.代码 寄语 前言 合并单元格,设置边框 一.效果 二.代码 代码如下(示例): *&--------------------------------------- ...

  9. html table转excel单元格带背景颜色

    html table转excel单元格带背景颜色 在浏览器端将html的table转成Excel,可以参考下面的库 https://github.com/JackGit/table2excel.js ...

最新文章

  1. ​数据分析必读干货:简单而实用的3大分析方法
  2. ofbiz之entity 实体解析 扩展 视图 复合列写法
  3. dapper 注意事项之GUID
  4. mac 下idea光标问题
  5. Vue 阻止事件冒泡
  6. bilibili怎么用用户名登录_b站(bilibili)账号只记得用户名忘了密码怎么办?实名认证能找回吗...
  7. 《高效能人士的7个习惯》读后摘记
  8. Pattern Recognition and Machine Learning(模式识别与机器学习)第一章导读
  9. python贴吧爬虫-python贴吧爬虫
  10. delphi计算机语言排名,2020年3月TIOBE编程语言排行榜 Java继续蝉联榜首
  11. 51单片机制作计算机1602显示,51单片机对LCD1602液晶显示器的控制
  12. 笔记:常见的约束问题求解算法——乘子法和Frank-Wolfe算法
  13. VSU(虚拟交换单元)组网提升网络可用性
  14. 展望|人脸识别技术发展现状及未来趋势
  15. linux服务被植入挖矿(2t3ik与ddgs)解决方式
  16. 【bug解决】TypeError: forward() missing 1 required positional argument: ‘x‘
  17. 694. Number of Distinct Islands
  18. 《c语言修炼内功之第二种境界(看代码就是内存)之关键字系列一》
  19. C++内存管理方式——new/delete
  20. 极验接入教程纯html,在AspNetCore中使用极验做行为认证的验证流程

热门文章

  1. C#制作、打包、签名、发布Activex全过程
  2. 读取Mc1000的 唯一 ID 机器号
  3. 用 JavaScript 的方式理解递归
  4. 10分钟腾讯云配置免费https
  5. 32位PLSQL Developer无法登录Oracle 11g 64位
  6. Lua语法基础(1)---简介、基本数据类型、表达式
  7. java每日小算法(10)
  8. 【Java并发性和多线程】线程安全及不可变性
  9. 《C语言深度剖析》学习笔记三
  10. 最长单调子序列及计数(poj1952)