Openxml使用C#在Excel中读取图像

时间:2018-06-12 20:06:54

标签: c# excel openxml openxml-sdk

我正在使用共享字符串阅读excel文档,我想检查某个单元格是否包含图像。如果是,那么我将用文本替换该图像并将其保存为字符串。

using (FileStream fs = new FileStream(excelFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        using (SpreadsheetDocument doc = SpreadsheetDocument.Open(fs, false))
                        {
                            WorkbookPart workbookPart = doc.WorkbookPart;
                            SharedStringTablePart sstpart = workbookPart.GetPartsOfType<SharedStringTablePart>().First();
                            SharedStringTable sst = sstpart.SharedStringTable;
                            WorksheetPart worksheetPart = workbookPart.WorksheetParts.Last();

                            Worksheet sheet = worksheetPart.Worksheet;

                            var cells = sheet.Descendants<Cell>();
                            var rows = sheet.Descendants<

                            // One way: go through each cell in the sheet
                            foreach (Cell cell in cells)
                            {
                                if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
                                {
                                    int ssid = int.Parse(cell.CellValue.Text);
                                    string str = sst.ChildElements[ssid].InnerText;

                                    excelcontent += str+"<br>";
                                    if (cell.CellValue ==  worksheetPart.DrawingsPart.GetPartsOfType<ImagePart>())
                                    {
                                        excelcontent += textval += "<a href='../images/" + FileNameWithoutExtention + "-" + imagecount + ".jpg'><br><img src ='../images/" + FileNameWithoutExtention + "-" + imagecount + ".jpg' alt = '" + title + "-" + Image_naming + "'></a><br>";
                                    }
                                }
                                else if (cell.CellValue != null)
                                {
                                    excelcontent += textval += "<a href='../images/" + FileNameWithoutExtention + "-" + imagecount + ".jpg'><br><img src ='../images/" + FileNameWithoutExtention + "-" + imagecount + ".jpg' alt = '" + title + "-" + Image_naming + "'></a><br>";
                                    excelcontent += cell.CellValue.Text + "<br>";
                                }
                            }

但它不能识别细胞中的图像。我该怎么办?

0 个答案:

没有答案