直接源码吧。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
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); //删除图片
}
}
|