简体   繁体   English

如何从Excel文档的单元格中读取图片并将其存储到C#.net的数据表中?

[英]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. 我有(Kingsoft Spreadsheet) Excel工作簿,其中有15列,在第15列中有保存在单元格中的图片。 I am reading the excel file and creating a data table with respect to that. 我正在读取excel文件并为此创建一个数据表。 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. 第15列中可以有许多行,每行具有不同的图像。

C# code: C#代码:

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. 我无法找到帮助从excel的特定单元格读取图像。

Please help if anyone can. 如果有人可以,请帮忙。

Thanks in advance! 提前致谢!

i think you are not imported namespaces 我认为您不是导入的名称空间

try importing following namespaces 尝试导入以下名称空间

using System.Drawing; 使用System.Drawing; using OfficeOpenXml.Drawing; 使用OfficeOpenXml.Drawing;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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