这段时间一直在做office的工作。前2天获取单元格的颜色的问题一直没搞明确。

開始我想用的就是Npoi.主要前一部分的工作都是用Npoi完毕的

row.GetCell(j).CellStyle.FillBackgroundColorColor 获取IColor接口。通过IColor的RGB属性获取但是经过大量用例測试这里获取的rgb并不准确仅仅有部分颜色对的上。

如图

后来我甚至问了npoi的创始人也没有给我一个明白的回复。

我自己推測由于row.GetCell(j).CellStyle.FillBackgroundColor 是short类型npoi是不是仅仅支持他枚举的颜色

后来经过翻阅官网的demo发现npoi能够通过rgb设置颜色

/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */ /* ================================================================
* Author: Tony Qu
* Author's email: tonyqus (at) gmail.com
* NPOI HomePage: http://www.codeplex.com/npoi
* Contributors:
*
* ==============================================================*/ using System;
using System.Collections.Generic;
using System.Text; using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util; namespace CustomColorInXls
{
class Program
{
static void Main(string[] args)
{
InitializeWorkbook(); HSSFPalette palette = workbook.GetCustomPalette();
palette.SetColorAtIndex(HSSFColor.PINK.index, (byte)255, (byte)1, (byte)222);
//HSSFColor palette.GetColor()
//HSSFColor myColor = palette.AddColor((byte)253, (byte)0, (byte)0); ISheet sheet1 = workbook.CreateSheet("Sheet1");
ICellStyle style1 = workbook.CreateCellStyle();
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.PINK.index;
style1.FillPattern = FillPatternType.SOLID_FOREGROUND;
sheet1.CreateRow(0).CreateCell(0).CellStyle = style1;
short c = sheet1.GetRow(0).Cells[0].CellStyle.FillForegroundColor;
short []sh = palette.GetColor(c).GetTriplet(); WriteToFile();
} static HSSFWorkbook workbook; static void WriteToFile()
{
//Write the stream data of workbook to the root directory
FileStream file = new FileStream(@"test.xls", FileMode.Create);
workbook.Write(file);
file.Close();
} static void InitializeWorkbook()
{
workbook = new HSSFWorkbook(); ////create a entry of DocumentSummaryInformation
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "NPOI Team";
workbook.DocumentSummaryInformation = dsi; ////create a entry of SummaryInformation
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Subject = "NPOI SDK Example";
workbook.SummaryInformation = si;
}
}
}

并且palettle能够通过public HSSFColor GetColor(short index);方法将short转化为HSSFColor而通过HSSFColor类的public virtual short[] GetTriplet();方法能够获取rgb.

可是这里存在2个问题

1.

palette.SetColorAtIndex(HSSFColor.PINK.index, (byte)255, (byte)1, (byte)222);这里是设置的时候固定的设置。

而人工操作是否能有这样的固定的设置。

2.

支持excel2007的XSSFWorkbook并没有GetCustomPalette方法。

而通过反编译器我也没找到能获取Palette的类似的类

后通过官网excel2003和excel2007的demo例如以下code

2003

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXp5ZjE5OTI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

2007

npoi to excel2007无法获取单元格rgb的颜色 假设颜色不一样会向npoi支持的short转化

实在没法了。仅仅有祭出com组件了。

代码例如以下:

Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
//打开文件,n.FullPath是文件路径
workbook = application.Application.Workbooks.Open(copyPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Range range = null;// 创建一个空的单元格对象
range = worksheet.get_Range(worksheet.Cells[rowNum + 1, ColumnNum + 1], worksheet.Cells[rowNum + 1, ColumnNum + 1]);
if (range.Value2 != null)
{
string content = range.Value2.ToString();
}
string color = range.Interior.Color.ToString();
Common com = new Common();
Color col = com.RGB(int.Parse(color));
return new byte[3] { col.R, col.G, col.B };

RGB方法例如以下:

 public Color RGB(int color)
{
int r = 0xFF & color;
int g = 0xFF00 & color;
g >>= 8;
int b = 0xFF0000 & color;
b >>= 16;
return Color.FromArgb(r, g, b);
}

string color的这个color的范围是整个颜色的范围OK问题解决。但是动用了com组件。假设大家有更好的办法欢迎留言。

c#怎样获取excel单元格的RGB颜色的更多相关文章

  1. POI获取excel单元格红色字体,淡蓝色前景色的内容

    如果是Microsoft Excel 97-2003 工作表 (.xls) if(31 == cell.getCellStyle().getFillForegroundColor()) //判断单元格 ...

  2. 以字符串形式获取excel单元格中的内容

    public static String getCellValue(XSSFCell cell) { if (cell == null) { return ""; } switch ...

  3. excel小技巧-用于测试用例的编号栏:“获取当前单元格的上一格的值+1”=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1

    编写用例的时候使用,经常修改用例的时候会需要增加.删除.修改条目,如果用下拉更新数值的方式会很麻烦. 1.使用ctrl下拉,增删移动用例的时候,需要每次都去拉,万一列表比较长,会很麻烦 2.使用ROW ...

  4. C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置

    一.对行高,列宽.单元格边框等的设置 这篇简短的文字对单元格的操作总结的比较全面,特此转载过来. private _Workbook _workBook = null; private Workshe ...

  5. 简单介绍Excel单元格行列指示的实现原理(俗称聚光灯功能)

    原始出处:www.cnblogs.com/Charltsing/p/CellLight.html QQ:564955427 Excel单元格行列指示的实现原理(俗称聚光灯功能) 单元格行列指示功能在录 ...

  6. poi 升级至4.x 的问题总结(POI Excel 单元格内容类型判断并取值)

    POI Excel 单元格内容类型判断并取值 以前用 cell.getCachedFormulaResultType() 得到 type 升级到4后获取不到了 换为:cell.getCellType( ...

  7. python excel单元格及样式

    python excel单元格及样式: #!/usr/bin/env python # -*- coding: utf-8 -*-” #只对当前文件的中文编码有效 # Filename : Write ...

  8. Spire.Cloud.SDK for Java 合并、拆分Excel单元格

    Spire.Cloud.SDK for Java 是Spire.Cloud云产品系列中,用于处理Word.Excel.PowerPoint以及PDF文档的JAR文件,可执行文档编辑.转换.保存等操作. ...

  9. C#/VB.NET 在Excel单元格中应用多种字体格式

    在Excel中,可对单元格中的字符串设置多种不同样式,通常只需要获取到单元格直接设置样式即可,该方法设置的样式会应用于该单元格中的所有字符.如果需要对单元格中某些字符设置样式,则可以参考本文中的方法. ...

随机推荐

  1. FZOJ2110: Star

    Problem Description Overpower often go to the playground with classmates. They play and chat on the ...

  2. ASP.NET - TreeView

    设置节点图片 : Windows资源管理器左侧的树型资源结构图中,各节点都有图片连接,例如磁盘的图片.光盘的图片和文件夹的图片等,使资源的表现更加形象.IEWebControls的TreeView控件 ...

  3. Servlet的学习之Session(3)

    在上一篇<Servlet的学习之Session(2)>我们知道了Session能实现一个会话过程中保存数据或者多个会话中实现同一个Session的关键因素就是Cookie,只是Cookie ...

  4. 研究一下TForm.WMPaint过程(也得研究WM_ERASEBKGND)——TForm虽然继承自TWinControl,但是自行模仿了TCustomControl的全部行为,一共三种自绘的覆盖方法,比TCustomControl还多一种

    先擦除背景: procedure TCustomForm.WMEraseBkgnd(var Message: TWMEraseBkgnd); begin if not IsIconic(Handle) ...

  5. How to find configuration file MySQL uses?(转)

    http://www.dbasquare.com/2012/04/01/how-to-find-mysql-configuration-file/ A customer called me today ...

  6. Linux开发环境的搭建和使用——Linux 常用的命令使用

    概要 视或电影中看到过类似的场景,黑客面对一个黑色的屏幕,上面飘着密密麻麻的字符,梆梆一顿敲,就完毕了窃取资料的任务. Linux 刚出世时没有什么图形界面.全部的操作全靠命令完毕.就如同电视里的黑客 ...

  7. freemarker的TemplateExceptionHandler使用

    系统使用freemarker作为页面展示层,为了解决系统统一异常的问题.于是配置了struts2的统一异常解决的方法(这个网上资料非常多,大家能够查看),但是发现freemarker出现异常后,str ...

  8. pc2日记——有惊无险的第二天2014/08/29

    今天下午如期的用pc2进行了第二场比赛.因为昨天的出错经历和早上充足的准备,下午的比赛尽管在開始的时候出了点小小的问题,但总的来说还是非常成功的. 早上八点过去504開始又一次配置client,由于开 ...

  9. Firemonkey绑定对象列表

    在实现Firemonkey绑定对象列表的过程中,我遇到的一些现有教程当中没有提到的细节,分享一下. 1.追加对象 用Navigator插入记录,位置总是在当前记录之前插入,没有在最后追加一个对象的方法 ...

  10. 关于ListCtrol自绘的技巧

    一.给控件添加排序功能report风格的list控件很多情况下都需要支持排序功能,而且最好支持按不同列进行排序.CListCtrl的类方法SortItems支持排序功能,但是在排序过程中,两个数据真正 ...