Récupérer des images à partir du file Excel à l'aide d'OLEDB

J'ai une feuille Excel avec deux colonnes, une est un nombre, et la deuxième colonne a une image. Je veux lire ces données à partir de c # avec la connection oledb, je peux lire facilement le numéro, mais les images ne sont pas contenues dans la deuxième colonne, alors, dans c #, je viens d'get la première colonne.

maintenant, comment puis-je lire les images? Je veux extraire les nombres et les images connexes de cette feuille Excel.

Pas possible, j'ai peur.

Les images ne vivent pas dans les cellules – vous pouvez les placer sur la cellule, et vous pouvez les dimensionner comme si elles se trouvaient dans la cellule, mais elles n'accordent en rien cette cellule.

Vous pouvez manipuler le contenu de l'image d'une feuille de calcul à l'aide de l'interface VBA et COM, mais pas OLEDB.

C'est un sujet plus ancien, mais j'ai pensé que j'appendais un peu de mon code jusqu'à présent.

Cet exemple suppose que vous disposez d'une application Windows que vous avez placée sur Picturebox sur «PictureBox1».

Il suppose également d'append une reference à Excel (Microsoft.Office.Interop.Excel).

Les images sont liées à votre classur et ne font pas partie des cellules elles-mêmes, comme Jay l'a mentionné. Vous pouvez find où l'image devrait aller très facilement en utilisant TopLeftCell et BottomRightCell.

Maintenant, vous devrez écrire une boucle pour extraire toutes les images de notre document mais je vous laisse.

ssortingng 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(); } 

La réponse de Nick fonctionne très bien pour moi dans mon application Web avec un petit changement, il ne copiait pas d'image dans le presse-papier

  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(); Thread thread = new Thread (() =>  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(); foreach (var pic dans ws.Pictures ())  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(); si (pic! = null)  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(); // Ce code détectera ce que la scope de la région de l'image était  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(); int startCol = pic.TopLeftCell.Column;  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(); int startRow = pic.TopLeftCell.Row;  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(); int endCol = pic.BottomRightCell.Column;  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(); int endRow = pic.BottomRightCell.Row;  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(); pic.CopyPicture (XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);  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(); si (Clipboard.GetDataObject ()! = null)  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(); Image img = Clipboard.GetImage ();  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(); thread.SetApartmentState (ApartmentState.STA);  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(); // Définir le fil à STA  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(); thread.Start ();  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(); 

Travaille pour moi