Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

c# - WPF导入Excel(带有颜色单元格)到datagrid(WPF import excel (with color cell) to datagrid)

I want to import an excel that contain few color background cells into datagrid.

(我想将包含少量彩色背景单元格的Excel导入到datagrid中。)

The table only show all the data without colored cells when i bind the datatable to the datagrid.

(当我将数据表绑定到数据网格时,该表仅显示所有没有彩色单元格的数据。)

How to display the cell color into the datagrid?

(如何将单元格颜色显示到数据网格中?)

The excel figure link ExcelSheet and my codes are attached.

(excel图形链接ExcelSheet和我的代码已随附。)

Thanks.

(谢谢。)

...

(...)

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(ExcelConnString);
            //Read excel sheet <State>
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int xlRow = xlRange.Rows.Count;
            int xlCol = xlRange.Columns.Count;

            DataRow dataRow = null;

            for (int i = 1; i <= xlRow; i++)
            {
                if (i != 1)
                    //Add new Row
                    dataRow = dataTable.NewRow();

                for (int j = 1; j <= xlCol; j++)
                {
                    if (i == 1)
                    {
                        //Add column Header
                        string value = Convert.ToString(xlRange.Cells[1, j].Value2);
                        dataTable.Columns.Add(value);
                    }
                    else
                    {
                        //Add value at [i,j]
                        dataRow[j - 1] = xlRange.Cells[i, j].value;
                        //read cell background color
                        string CellColour = xlWorksheet.Cells[i, j].Interior.Color.ToString();
                    }
                }
                if (dataRow != null)
                    dataTable.Rows.Add(dataRow);
            }
            datagridview1.ItemSource = dataTable.DefaultView;
...
  ask by xxxabf translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...