zoukankan      html  css  js  c++  java
  • Retrieve pictures from Excel file using OLEDB

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

    work great for me in my web application with a little change it was not copying image to 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();
  • 相关阅读:
    二、JAVA通过JDBC连接mysql数据库(操作)
    一、JAVA通过JDBC连接mysql数据库(连接)
    while loading persisted sessions [java.io.EOFException]的三种解决办法!
    8大排序算法的java实现--做个人收藏
    数据库索引实现原理—B_TREE
    数据库查询优化方法
    webservice原理及基于cxf开发的基本流程
    Java多线程之Thread、Runnable、Callable及线程池
    随机数问题--已知有个Random7()的函数,返回1到7随机自然数,让利用这个Random7()构造Random10()随机1~10.
    深入Java—String源代码
  • 原文地址:https://www.cnblogs.com/lee2011/p/10656178.html
Copyright © 2011-2022 走看看