首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用"ExcelDataReader“获取背景颜色

使用"ExcelDataReader“获取背景颜色
EN

Stack Overflow用户
提问于 2019-12-19 20:58:02
回答 1查看 647关注 0票数 2

我想用"ExcelDataReader“库得到一个单元格的背景颜色。

有谁能给我个提示吗?

到目前为止,我所拥有的是:

代码语言:javascript
复制
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); */
}

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2019-12-19 21:39:43

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

在单元格中设置颜色的步骤

代码语言:javascript
复制
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;

从单元格获取颜色的步骤

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59410272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档