Extract images from Excel file using OLEDB

I have an Excel worksheet with two columns, one with a number and the other with an image. I want to read this data from C # with an oledb connection, I can easily read the number, but the pictures are not in the second column, so in C # I just get the first column.

now how can i read images? I want to extract numbers and related images from this excel sheet.

+3
source share
3 answers

I can’t, I'm afraid.

Pictures do not live in cells - you can put them in a cell, and you can determine their size so that they look like they are in a cell, but they in no way occupy this cell.

VBA COM-, OLEDB.

+2

, , .

, Windows, Picturebox "pictureBox1".

Excel (Microsoft.Office.Interop.Excel).

, , . , , TopLeftCell BottomRightCell.

, , .

        string file = @"C:\sample.xlsx";

        if(System.IO.File.Exists(file))
        {

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = true; //FOR TESTING ONLY
            Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(file,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing,
                                Type.Missing);

            Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];   //Selects the first sheet
            Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)ws.Cells[1, 1];      //Select cell A1
            object cellValue = range.Value2;

            #region Extract the image
            Microsoft.Office.Interop.Excel.Picture pic = (Microsoft.Office.Interop.Excel.Picture)ws.Pictures(1);

            if (pic != null)
            {
                //This code will detect what the region span of the image was
                int startCol = (int)pic.TopLeftCell.Column;
                int startRow = (int)pic.TopLeftCell.Row;
                int endCol = (int)pic.BottomRightCell.Column;
                int endRow = (int)pic.BottomRightCell.Row;


                pic.CopyPicture(Microsoft.Office.Interop.Excel.XlPictureAppearance.xlScreen, Microsoft.Office.Interop.Excel.XlCopyPictureFormat.xlBitmap);
                if (Clipboard.ContainsImage())
                {
                    Image img = Clipboard.GetImage();
                    this.pictureBox1.Image = img;
                }
            }
            #endregion

            //Close the workbook
            wb.Close(false,Type.Missing,Type.Missing);

            //Exit Excel
            excelApp.Quit();
        }
+5

Nick answer works fine for me in my web application with a little change, this is not copying the image to the clipboard

 Thread thread = new Thread(() =>
                {
                    foreach (var pic in ws.Pictures())
                    {
                        if (pic != null)
                        {
                            //This code will detect what the region span of the image was
                            int startCol = pic.TopLeftCell.Column;
                            int startRow = pic.TopLeftCell.Row;
                            int endCol = pic.BottomRightCell.Column;
                            int endRow = pic.BottomRightCell.Row;
                            pic.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
                            if (Clipboard.GetDataObject() != null)
                            {
                                Image img = Clipboard.GetImage();
                            }
                        }
                    }
                });
                thread.SetApartmentState(ApartmentState.STA);
                //Set the thread to STA
                thread.Start();
                thread.Join();

Works for me

+1
source

Source: https://habr.com/ru/post/1740990/


All Articles