使用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进行报表开发(五)-操作单元格边框和颜色 【转】相关推荐
- 使用C#和Excel进行报表开发(一)--开始
C#操作Excel进行报表开发系列共写了八篇,也已经有很久没有新东西了,现在整理一下,方便以后查阅,如果有写新的,会同时更新.需要注意的是因为Office的版本不同,实际的代码可能会有所不同,但是都是 ...
- Excel 2010光标移动到单元格边框时,不显示小十字(亲自实践)
今天被问到一个现象,某台机子打开所有Excel,光标移动到单元格边框时,不显示小十字 也就是无法进行公式填充 2010版跟07版的解决方法的位置还不一样 2010版解决方法如下: Excel选项--高 ...
- 使用C#和Excel进行报表开发(四)-复制单元格 【转】
在用Excel作报表的时候,可能会碰到分页的情况,这样就要分成多个Sheet显示,如果要在每页都保留表头,就需要将第一页作为模板设计的表头部分拷贝到其他的Sheet上,这时就要用用到Excel中的单元 ...
- 使用C#和Excel进行报表开发(5)
内容摘要:在用Excel作报表的时候,可能需要操作单元格的边框和填充颜色和纹理等操作,下面的代码说明如何设置选中的单元格的填充纹理和边框. 在用Excel作报表的时候,可能需要操作单元格的边框和填充颜 ...
- poi 导出excel实战与word新增行与excel添加背景色与设置单元格边框
提示:本文尽可能简洁通俗的讲解[poi 导出excel实战] ,如需导入可见文尾 一.导入依赖: <!--Apache poi--><!--xls(03)--><depe ...
- poi 合并单元格添加边框_如何在Excel中的文本和单元格边框之间添加空间
poi 合并单元格添加边框 Adding a bit of extra padding to your cell boundaries can make it easier for you to re ...
- ABAP对excel的操作(为单元格填充背景颜色、设置border等)
文章目录 需求 一.效果 二.代码 寄语 需求 今日需要对excel的部分内容进行强调色展示(填充背景色).设置border等等,发现网上都没有找到我想要的,然后自己找到了demo供大家参考 一.效果 ...
- ABAP对excel的操作(合并单元格,设置边框)
文章目录 前言 一.效果 二.代码 寄语 前言 合并单元格,设置边框 一.效果 二.代码 代码如下(示例): *&--------------------------------------- ...
- html table转excel单元格带背景颜色
html table转excel单元格带背景颜色 在浏览器端将html的table转成Excel,可以参考下面的库 https://github.com/JackGit/table2excel.js ...
最新文章
- ​数据分析必读干货:简单而实用的3大分析方法
- ofbiz之entity 实体解析 扩展 视图 复合列写法
- dapper 注意事项之GUID
- mac 下idea光标问题
- Vue 阻止事件冒泡
- bilibili怎么用用户名登录_b站(bilibili)账号只记得用户名忘了密码怎么办?实名认证能找回吗...
- 《高效能人士的7个习惯》读后摘记
- Pattern Recognition and Machine Learning(模式识别与机器学习)第一章导读
- python贴吧爬虫-python贴吧爬虫
- delphi计算机语言排名,2020年3月TIOBE编程语言排行榜 Java继续蝉联榜首
- 51单片机制作计算机1602显示,51单片机对LCD1602液晶显示器的控制
- 笔记:常见的约束问题求解算法——乘子法和Frank-Wolfe算法
- VSU(虚拟交换单元)组网提升网络可用性
- 展望|人脸识别技术发展现状及未来趋势
- linux服务被植入挖矿(2t3ik与ddgs)解决方式
- 【bug解决】TypeError: forward() missing 1 required positional argument: ‘x‘
- 694. Number of Distinct Islands
- 《c语言修炼内功之第二种境界(看代码就是内存)之关键字系列一》
- C++内存管理方式——new/delete
- 极验接入教程纯html,在AspNetCore中使用极验做行为认证的验证流程