从数据库中导出图片插入到Excel

上传人:洪易 文档编号:40499654 上传时间:2018-05-26 格式:DOC 页数:12 大小:72KB
返回 下载 相关 举报
从数据库中导出图片插入到Excel_第1页
第1页 / 共12页
从数据库中导出图片插入到Excel_第2页
第2页 / 共12页
从数据库中导出图片插入到Excel_第3页
第3页 / 共12页
从数据库中导出图片插入到Excel_第4页
第4页 / 共12页
从数据库中导出图片插入到Excel_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《从数据库中导出图片插入到Excel》由会员分享,可在线阅读,更多相关《从数据库中导出图片插入到Excel(12页珍藏版)》请在金锄头文库上搜索。

1、1.请问如何把 sqlserver 数据库中的图片导入到 excel 的指定表格中打印?数据表有“photo”的字段保存了图片,先想把图片导出到 excel 中打印,需指定 excel 的表格,入 C3 中打印!以下是我用的方法,贴出来,其他人如用到的可参考一下 思路:先从数据库中把图片保存到根目录中(文件名:temp.jpg) ,然后从 excel 中导入该图片,导入后删除 temp.jpgIf Not IsNull(mRst(“photo“) Then读取显示数据库中的图像Set StmPic = New ADODB.StreamWith StmPic.Type = adTypeBinar

2、y.Open.Write mRst.Fields(“photo“) 写入数据库中的数据至 Stream 中 .SaveToFile StrPicTemp, adSaveCreateOverWrite.SaveToFile App.Path 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.GetCurr

3、entDirectory()+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(ExcelN

4、ame);Worksheet worksheet;worksheet =(Worksheet)objbook.Worksheets1;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 = n

5、ew 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.Ins

6、ert(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);/删除图片 3.请教各位:我想用 c

7、#,通过闹钟实现定时把 sqlserver 的数据导入到一个 excel 文件,同时生成一个 EXCEL 文件, 不是手动保存,那位大侠知道的指导下,谢谢。要是有什么原代码就太感谢了。这是我写的一个方法,也找了不少资料,参数是,dataset,要生成的 excel 的路径,根据 strColumnName 这一列的值 生成不同的 excel 文件名称为 1.xls,2.xsl. 希望对你有帮助.要先添加对 excel 的引用,在引用中的 com 中选 Public Sub CreateExcel(ByVal _strExcelPath As String, ByVal _ExcelDataSe

8、t As DataSet, ByVal _strColumnName As String)Dim xlSheet As Excel.WorksheetDim xlBook As Excel.WorkbookDim xlDBTable As Excel.DataTableDim strPath As StringTrydefine an arry list Dim arry As ArrayListarry = New ArrayListDim dr As DataRowDim i As Integer = 0Dim strAll As Stringfirst,get the deffent c

9、olumns valuethen send the values into a arrylistdefine a dataview in order to sortDim dv As DataViewdv = New DataView(_ExcelDataSet.Tables(0)sort by the columndv.Sort = _strColumnNameDim tmp As StringDim drv As DataRowViewFor Each drv In dvIf tmp drv.Row(_strColumnName) Thentmp = Convert.ToString(dr

10、v.Row(_strColumnName)arry.Add(tmp)End IfNextDim k As Integer = 0For k = 0 To arry.Count - 1Dim xlApp As New Excel.ApplicationxlBook = xlApp.Workbooks.AddxlSheet = xlBook.Worksheets(“sheet1“)the files full namestrPath = _strExcelPath 15 using System.IO;16 using System.Data;17 using System.Reflection;

11、18 using System.Diagnostics;19 using System.Configuration;20 using System.Collections;21 using Excel;22 23 namespace ProtoType24 25 / 26 / 套用模板输出 Excel,生成 xls 文件和 html 文件27 / Author: Liu Wen28 / Date Created: 2006-829 / 30 public class ExportExcel31 32 #region variable member33 protected string temp

12、lateFile = null;34 protected string excelFile = null;35 protected string htmlFile = null;36 protected object missing = Missing.Value;37 Excel.ApplicationClass app;38 Excel.Workbook book;39 Excel.Worksheet sheet;40 Excel.Range range;41 private DateTime beforeTime; /Excel 启动之前时间42 private DateTime aft

13、erTime; /Excel 启动之后时间43 #endregion44 45 / 46 / 构造函数,将一个已有 Excel 工作簿作为模板,并指定输出路径47 / 48 / Excel 模板文件路径 49 / Excel 输出文件路径 50 / Html 输出文件路径 51 public ExportExcel(string templateFile, string excelFile, string htmlFile)52 53 if(templateFile = null)54 throw new Exception(“Excel 模板文件路径不能为空!“);55 56 if(exce

14、lFile = null)57 throw new Exception(“Excel 输出文件路径不能为空!“);58 59 if(htmlFile = null)60 throw new Exception(“Html 输出文件路径不能为空!“);61 62 if(!File.Exists(templateFile)63 throw new Exception(“指定路径的 Excel 模板文件不存在!“);64 65 this.templateFile = templateFile;66 this.excelFile = excelFile;67 this.htmlFile = htmlF

15、ile;68 69 /创建一个 Application 对象70 beforeTime = DateTime.Now;71 app = new ApplicationClass();72 /app.Visible = true;73 afterTime = DateTime.Now;74 75 /打开模板文件,得到 WorkBook 对象76 try77 78 book = app.Workbooks.Open(templateFile,missing,missing,missing,missing,missing,79 missing,missing,missing,missing,missing,missing,missing,missing,missing);80 81 catch (Exception e)82 83 throw e;84 85 /得到 WorkSheet 对象86 sheet = (Excel.Worksheet)book.Sheets.get_Item(1);87 88 89 / 90 / 将 DataTable 数据导出到 Excel(可动态插入行)91 / 92 /

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号