aspose 擷取Excel中插入的對象

來源:互聯網
上載者:User

引入using Aspose.Cells;
aspose.cells.dll的是http://download.csdn.net/detail/keyrainie/4547523

此demo只是講出取Excel插入附件的原理,代碼較簡單
private void AsponseTest()

        {
            Workbook awork = new Workbook(textBox1.Text);
            Worksheet asheet = awork.Worksheets[0];

            Workbook awork2 = new Workbook();
            Worksheet asheet2 = awork2.Worksheets[0];
          
            try
            {
                byte[] imageData = null;
                byte[] objectData = null;
                string sourceFullName = "";
                int upperLeftRow = 0;
                int upperLeftColumn = 0;
                int height = 0;
                int width = 0;
                bool displayAsIcon = false;
                Aspose.Cells.Drawing.OleFileType fileType = Aspose.Cells.Drawing.OleFileType.Xls;

                string progID = "";
//迴圈的去除插入的對象

                foreach (Aspose.Cells.Drawing.OleObject item in asheet.OleObjects)
                {
                    
                    //擷取對象屬性,為產生新的Excel插入對象使用
                    imageData = item.ImageData;   //對象表徵圖
                    objectData = item.ObjectData;   //對象內同
                    sourceFullName = item.SourceFullName;
                    upperLeftRow = item.UpperLeftRow;
                    upperLeftColumn = item.UpperLeftColumn;
                    height = item.Height;
                    width = item.Width;
                    displayAsIcon = item.DisplayAsIcon;
                    fileType = item.FileType;

                    progID = item.ProgID;
                    asheet2.OleObjects.Add(upperLeftRow, upperLeftColumn, height, width, imageData);

                    
                    //此功能是讀取出一個圖片,並另存新檔
                    MemoryStream outputImage = new MemoryStream(imageData, 0, (int)imageData.Length);
                    Image returnImage = Image.FromStream(outputImage);
                    pictureBox1.Image = returnImage;
                    File.WriteAllBytes(@"C:\Users\ZhangYu\Desktop\2.txt", objectData);
                }

                //產生新的Excel,插入附件,此方法暫時只是舉例插入一個對象,若需要插入多個對象,則看情況改邏輯就可以
                foreach (Aspose.Cells.Drawing.OleObject item in asheet2.OleObjects)
                {
                    item.ObjectData = objectData;
                    item.SourceFullName = sourceFullName;
                    item.DisplayAsIcon = displayAsIcon;
                    item.FileType = fileType;
                    item.SetNativeSourceFullName(sourceFullName);
                    item.ProgID = progID;
                }
                int count = asheet.OleObjects.Count;
                awork2.Save(targetPath);

            }
            catch (Exception ex)
            {

            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.