近日在畢業(yè)設(shè)計(jì)中遇到要解決用C#編程從數(shù)據(jù)庫(kù)中讀取圖片數(shù)據(jù)導(dǎo)進(jìn)Excel文件的問(wèn)題,經(jīng)過(guò)兩三天的苦苦搜索資料想方設(shè)法解決問(wèn)題,,終于獲得解決方法,,代碼如下(程序要另外引用Excel.dll,需要獲取文件請(qǐng)?jiān)L問(wèn): http://sunrise./Lvyou/Forum/ForumContent.asp?ID=182):
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對(duì)象 app.Visible=true;//讓Excel文件可見(jiàn) Workbook objbook; objbook = app.Workbooks.Add(ExcelName); Worksheet worksheet; worksheet =(Worksheet)objbook.Worksheets[1]; Excel.Pictures pics=(Excel.Pictures)worksheet.Pictures(Type.Missing);//建立圖片集合對(duì)象
int TD=1; int p=1; PicItemColletion picItems= PicItem.Getpictems();//讀取數(shù)據(jù)庫(kù)圖片數(shù)據(jù)集合 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);//保存臨時(shí)圖片文件到硬盤里 if(File.Exists(picPath)) { stream.Close();//關(guān)閉圖像文件流
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);//建立圖片集合某一圖片對(duì)象
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);//刪除圖片 } }
|