我想用"ExcelDataReader“库得到一个单元格的背景颜色。
有谁能给我个提示吗?
到目前为止,我所拥有的是:
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); */
}谢谢!
发布于 2019-12-19 21:39:43
我不确定这是否有帮助,但我没有使用"ExcelDataReader“,而是使用Excel interop dll来创建一个Excel实例,并在单元格中设置/获取颜色。
在单元格中设置颜色的步骤
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 cellhttps://stackoverflow.com/questions/59410272
复制相似问题