使用"ExcelDataReader"获取背景颜色. [英] Get background colour with "ExcelDataReader"

查看:165
本文介绍了使用"ExcelDataReader"获取背景颜色.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用"ExcelDataReader"库获取Excel单元格的背景色.
有人可以给我提示吗?
到目前为止,我有:

I want to get the background colour of an Excel cell using "ExcelDataReader" library.
Can anybody give me a hint?
What I have so far is:

DataRowCollection sheet;
string fileName = "....";

private void OpenExcel_and_CloseExcel(string articleNumber)
{
  if (sheet != null) sheet.Clear();

  var stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // can open already opened xls
  var reader = ExcelReaderFactory.CreateReader(stream); // xls, xlsx
  var result = reader.AsDataSet(); // the result of each spreadsheet is now in result.Tables[...]
  var dt = result.Tables[_tableName];
  sheet = dt.Rows;

  /* For debuging:
  string text = sheet[42][6].ToString();
  int r = dt.Rows.Count;
  int c = dt.Columns.Count;
  MessageBox.Show("text: " + text +", "+ r +", "+c); */
}

谢谢!

推荐答案

我不确定这是否有帮助,但不是使用"ExcelDataReader",而是使用Excel interop dll制作一个Excel实例并在单元格中设置/获取颜色.

Not sure if this helps but instead of "ExcelDataReader" I used Excel interop dll to make an Excel instance and set/get color in cell.

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp;
Excel.WorkBook xlWB;
Excel.Worksheet xlWS;

xlApp = new Excel.Application();
xlWB = xlApp.Workbooks.Open("C:\path\to\file.xlsx");
xlWS = xlWB.Worksheets["Sheet1"];

xlWS.Cells[1, "D"].Interior.Color = Color.GreenYellow;

要从单元格获取颜色

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlApp;
Excel.WorkBook xlWB;
Excel.Worksheet xlWS;

xlApp = new Excel.Application();
xlWB = xlApp.Workbooks.Open("C:\path\to\file.xlsx");
xlWS = xlWB.Worksheets["Sheet1"];

int color_n = System.Convert.ToInt32((xlWS.Cells[1, "D"]).Interior.Color);
Color color = System.Drawing.ColorTranslator.FromOle(color_n);
MessageBox.Show(color.ToString()); // Outputs the color of a cell

这篇关于使用"ExcelDataReader"获取背景颜色.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆