Asp.net汇出图片到Excel
来源:互联网 发布:做淘宝不适合苹果电脑 编辑:程序博客网 时间:2024/04/28 19:07
1.将图片字节转换为图片存储在本地
byte[] bt= chart.GetPicture(); string strUrl = Server.MapPath(".")+"/GifToExcel/Report1.gif"; Response.ContentType = "image/gif";//上传时,存储的图片类型 //输出图片文件二进制数据 Response.OutputStream.Write(bt,0,bt.Length); System.IO.MemoryStream s = new System.IO.MemoryStream(bt); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(s); bitmap.Save(strUrl);
2.将本地图片汇入到Excel的类
using System;using System.Collections.Generic;using System.Linq;using System.Web;using ex=Microsoft.Office.Interop.Excel;using System.Reflection;
/// <summary>/// ExcelReport 的摘要描述/// </summary>public class InsertPictureToExcel{ /// <summary> /// 將圖片匯出到Excel /// </summary> /// <param name="excelTemple">excel模板路徑</param> /// <param name="img">要匯入到excel的圖片路徑</param> /// <param name="position">圖片放到excel的哪個位置</param> public static void imgToExcel(string excelTemple, string img, string position) { object missing = Missing.Value; //定义一个Excel应用程序 ex.ApplicationClass excelObj = new ex.ApplicationClass(); excelObj.DisplayAlerts = false; excelObj.Visible = false; ex.Workbooks wbooks = excelObj.Workbooks; ex.Workbook wbook = wbooks.Open(HttpContext.Current.Server.MapPath(excelTemple), missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
ex.Sheets sheets = (ex.Sheets)wbook.Worksheets; ex._Worksheet sheet = (ex._Worksheet)sheets.get_Item(1); ex.Range exRange = (ex.Range)sheet.get_Range(position, missing); exRange.Select(); //声明一个pictures对象,用来存放柱状图 ex.Pictures pics = (ex.Pictures)sheet.Pictures(missing); //插入图片 pics.Insert(HttpContext.Current.Server.MapPath(img), missing); string imgName = HttpContext.Current.Server.MapPath(img).Substring(0, HttpContext.Current.Server.MapPath(img).LastIndexOf('.')); wbook.SaveAs(imgName + ".xlsx", missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing); wbook.Close(false, missing, missing);
excelObj.Quit(); //必须关闭释放所引用的COM对象,关闭Excel进程,否则会占用服务器资源 System.GC.Collect(); System.GC.WaitForPendingFinalizers();
}}
3.JavaScript汇出图片到Excel中
<script type="text/javascript">function fnExportExcel(){ try{ var oXL=new ActiveXObject("Excel.Application"); }catch(e){ alert("Excel沒有安裝或瀏覽器設置不正確,請啟用所有Active控件和插件"); //工具-> Internet??-> 安全->自定???-> ??有??安全??的ActiveX控件?行初始化 ???用 return false; } var oWB=oXL.Workbooks.Add(); var oSheet=oWB.ActiveSheet; oSheet.Cells(1,1).Select(); oSheet.Pictures.Insert(document.getElementById("img1").src); oXL.Visible = true; oXL.UserControl = true; }</script>
在Web.config的<system.web>里加
<identity impersonate="true"/>
<!-- impersonate 指定是否对每一个请求使用客户端模拟 -->
- Asp.net汇出图片到Excel
- ASP.NET资料汇出Excel方法
- C#.NET Excel汇出功能
- C#快速汇出DataTable到Excel文件
- asp.net 导出到Excel
- asp.net 导出到excel
- sliverlight 汇出excel格式
- Ajax中使用用户控件(有用到reponse.write) 汇出资料到excel
- Asp.Net上传带图片的Excel
- asp.net 导出excel带图片
- C#透过DataTable汇出Excel
- Asp.net页面输出到EXCEL (转)
- Asp.Net页面输出到EXCEL
- Asp.Net页面输出到EXCEL
- Asp.Net页面输出到EXCEL
- Asp.Net页面输出到EXCEL
- ASP.NET导出数据到Excel
- ASP.NET中将数据输出到Excel
- LeetCode: Anagrams
- H.264的学习资源
- 判断点在多边形内外(角度判别法)
- n节点组成二叉树的个数
- [GVim]使用快捷键指南
- Asp.net汇出图片到Excel
- Java读写txt文件中文乱码问题
- 调整数组顺序实现:奇数在偶数前面 以及 “奇偶奇偶”相间
- 反射应用-取得指定的Annotation中的内容
- Bitmap的释放流程
- 深入理解Flash的应用程序域
- 开关、断路器、隔离器的区别
- 【算法】用递归颠倒一个栈
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解