简体   繁体   中英

How to read a picture from a cell of excel document and store into Data table in C#.net?

I have (Kingsoft Spreadsheet) Excel workbook having 15 columns, in the 15th column there is picture saved in a cell. I am reading the excel file and creating a data table with respect to that. All things are working well except to store picture in data table. There can be number of rows each having different image in 15th column.

C# code:

public static DataTable GetDataTableFromExcel(string path, bool hasHeader)
{
    using (var pck = new OfficeOpenXml.ExcelPackage())
    {
        using (var stream = File.OpenRead(path))
        {
            pck.Load(stream);
        }
        var ws = pck.Workbook.Worksheets.First();    

        DataTable tbl = new DataTable();
        foreach (var firstRowCell in ws.Cells[1, 1, 1, ws.Dimension.End.Column])
        {
            tbl.Columns.Add(hasHeader ? firstRowCell.Text : string.Format("Column {0}", firstRowCell.Start.Column));
        }
        var startRow = hasHeader ? 2 : 1;

        for (int rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
        {
           // Inserting Data from Excel to Datatable.
            var wsRow = ws.Cells[rowNum, 1, rowNum, 15];
            DataRow row = tbl.Rows.Add();
            foreach (var cell in wsRow)
            {
                row[cell.Start.Column - 1] = cell.Text;
            }

        }
        return tbl;
    }
}

Note: I am using Kingsoft spreadsheets.

I am unable to find help to read image from a particular cell of excel.

Please help if anyone can.

Thanks in advance!

i think you are not imported namespaces

try importing following namespaces

using System.Drawing; using OfficeOpenXml.Drawing;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM