使用spire.xls,程序员可以非常方便地使用c#给excel工作表设置背景颜色和添加背景图片。以下示例将详细讲述如何使用spire.xls给一个现有excel工作表设置背景颜色,添加背景图片。
设置背景颜色
c#
//加载excel文档并获取第一个工作表
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[0];
//为excel range 分别设置背景色
sheet.range["a1:e1"].style.color = color.lightseagreen;
sheet.range["a2:e19"].style.color = color.green;
sheet.range["a20:e38"].style.color = color.deepskyblue;
//保存文档
workbook.savetofile("setbackgroundcolor.xlsx", excelversion.version2010);
vb.net
'加载excel文档并获取第一个工作表
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
dim sheet as worksheet = workbook.worksheets(0)
'为excel range 分别设置背景色
sheet.range("a1:e1").style.color = color.lightseagreen
sheet.range("a2:e19").style.color = color.green
sheet.range("a20:e38").style.color = color.deepskyblue
'保存文档
workbook.savetofile("setbackgroundcolor.xlsx", excelversion.version2010)
设置背景图片
c#
//加载excel文档并获取第一个工作表
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
worksheet sheet = workbook.worksheets[0];
//加载一张图片并设置为背景图片
bitmap bm = new bitmap(image.fromfile("logo.png"));
sheet.pagesetup.backgoundimage = bm;
//保存文档
workbook.savetofile("setbackgroundimage.xlsx", excelversion.version2010);
vb.net
'加载excel文档并获取第一个工作表
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
dim sheet as worksheet = workbook.worksheets(0)
'加载一张图片并设置为背景图片
dim bm as new bitmap(image.fromfile("logo.png"))
sheet.pagesetup.backgoundimage = bm
'保存文档
workbook.savetofile("setbackgroundimage.xlsx", excelversion.version2010)