[轉] 從資料庫中讀取圖片並匯入Excel檔案,C#方式

來源:互聯網
上載者:User

標籤:

原文地址, 作者 Lvyou1980

直接源碼吧。

using System;using System.IO;using System.Data;using System.Drawing;using System.Drawing.Imaging;using System.Windows.Forms;using Excel;private void EduceExcel() {  string picPath=Directory.GetCurrentDirectory()+‘\\Excelpic.jpg‘;  string ExcelName=Directory.GetCurrentDirectory()+‘\\ExcelModul.xls‘;  string fileName = ‘Excel‘+DateTime.Now.ToString().Replace(‘:‘, ‘‘) + ‘.xls‘;  Excel.Application app =new Excel.Application();//建立Excel對象   app.Visible=true;//讓Excel檔案可見   Workbook objbook;   objbook = app.Workbooks.Add(ExcelName);   Worksheet worksheet;   worksheet =(Worksheet)objbook.Worksheets[1];   Excel.Pictures pics=(Excel.Pictures)worksheet.Pictures(Type.Missing);//建立圖片集合對象   int TD=1;   int p=1;   PicItemColletion picItems= PicItem.Getpictems();//讀取資料庫圖片資料集合    foreach(PicItem PI in picItems)    {     if(PI.ImageData != null)     {     MemoryStream stream = new MemoryStream(PI.ImageData, 0, PI.ImageData.Length);          Bitmap bitmap = new Bitmap(stream);     bitmap.Save(picPath,System.Drawing.Imaging.ImageFormat.Jpeg);//儲存臨時圖片檔案到硬碟裡      if(File.Exists(picPath))      {      stream.Close();//關閉影像檔流     Range range_1 =range=worksheet.get_Range(‘A‘+TD.ToString(),‘A‘+TD.ToString());     //擷取填充儲存格範圍                   pics.Insert(picPath,Type.Missing);//擷取圖片      Excel.Picture pic = (Excel.Picture )pics.Item(p);//建立圖片集合某一圖片對象       pic.Left = (double)range_1.Left;       pic.Top = (double)range_1.Top;       pic.Height =(double)range_1.Height;       pic.Width =(double)range_1.Width;       TD=TD+1;       p=p+1;       }    }   if(File.Exists(picPath))   {    File.Delete(picPath);//刪除圖片   } }

 

[轉] 從資料庫中讀取圖片並匯入Excel檔案,C#方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.