博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net 导出excel 以及插入图片
阅读量:4597 次
发布时间:2019-06-09

本文共 4705 字,大约阅读时间需要 15 分钟。

在网上找了相关参考资料,作个记录 下载Microsoft.Office.Interop.Excel;

参考资料

引用:命名空间

using Excel=Microsoft.Office.Interop.Excel;

Microsoft.Office.Core;

 

代码如下:

#region 导出        protected void btntoexcel_Click(object sender, EventArgs e)        {            Glant.BLL.BLLProduct bll = new Glant.BLL.BLLProduct();                  System.Data.DataTable dt = bll.GetList("id=661 or id=657").Tables[0];//获取要导出的dt;            if (dt != null)            {                #region 操作excel                Excel.Workbook xlWorkBook;                Excel.Worksheet xlWorkSheet;                xlWorkBook = new Excel.Application().Workbooks.Add(Type.Missing);                xlWorkBook.Application.Visible = false;                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[1];                //设置标题                xlWorkSheet.Cells[1, 1] = "标题1";                xlWorkSheet.Cells[1, 2] = "标题2";                xlWorkSheet.Cells[1, 3] = "标题3";                xlWorkSheet.Cells[1, 4] = "图片";                //设置宽度                            ((Excel.Range)xlWorkSheet.Cells[1, 2]).ColumnWidth = 15;                ((Excel.Range)xlWorkSheet.Cells[1, 4]).ColumnWidth = 50;//图片的宽度                                 //设置字体                xlWorkSheet.Cells.Font.Size = 12;                xlWorkSheet.Cells.Rows.RowHeight = 100;                #region 为excel赋值                for (int i = 0; i < dt.Rows.Count; i++)                {                    //为单元格赋值。                    xlWorkSheet.Cells[i + 2, 1] = dt.Rows[i]["sku"].ToString();                    xlWorkSheet.Cells[i +2, 2] = dt.Rows[i]["supplier_sku"].ToString();                    xlWorkSheet.Cells[i + 2, 3] = dt.Rows[i]["name"].ToString();                                       #region                    //可以直接取图片的地址                    string filename = Server.MapPath("~/" + dt.Rows[i]["smallpic_url"].ToString());                    //也可以用下面的方法把图片从数据库里取出来。                    //byte[] filedata = (byte[])dtimg.Rows[j]["img"];                    //System.IO.MemoryStream ms = new System.IO.MemoryStream(filedata);                    //System.Drawing.Image img = System.Drawing.Image.FromStream(ms);                    //img.Save(filename);                    #endregion                    //int rangeindex = i+1;                    //string rangename = "D" + rangeindex;                    //Excel.Range range = xlWorkSheet.get_Range(rangename, Type.Missing);                    //range.Select();                    //Excel.Pictures pict = (Excel.Pictures)xlWorkSheet.Pictures(Type.Missing);                    //pict.Insert(filename, Type.Missing);                                         //Left , Top , Width and Height.                    xlWorkSheet.Shapes.AddPicture(filename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue,                        220, 100+i*100, 100, 100);                 }                #endregion                #region 保存excel文件                string filePath = Server.MapPath("/") + "" + System.DateTime.Now.ToString().Replace(":", "") + "导出.xls";                xlWorkBook.SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,                    Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);                xlWorkBook.Application.Quit();                xlWorkSheet = null;                xlWorkBook = null;                GC.Collect();                System.GC.WaitForPendingFinalizers();                #endregion                #endregion                #region 导出到客户端                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");                Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("导出", System.Text.Encoding.UTF8) + ".xls");                Response.ContentType = "Application/excel";                Response.WriteFile(filePath);                Response.End();                #endregion                KillProcessexcel("EXCEL");            }        }        #endregion        #region 杀死进程        private void KillProcessexcel(string processName)        { //获得进程对象,以用来操作            System.Diagnostics.Process myproc = new System.Diagnostics.Process();            //得到所有打开的进程            try            {                //获得需要杀死的进程名                foreach (Process thisproc in Process.GetProcessesByName(processName))                { //立即杀死进程                    thisproc.Kill();                }            }            catch (Exception Exc)            {                throw new Exception("", Exc);            }        }        #endregion

 

转载于:https://www.cnblogs.com/yfdong22/archive/2012/11/13/2767581.html

你可能感兴趣的文章
JAVA 笔记(一)
查看>>
jdk+Tomcat部署安装
查看>>
js 循环读取 json的值
查看>>
c# 范型Dictionary实用例子
查看>>
C#实现动态页面静态化
查看>>
win10 系统右键菜单不显示文字(只有小图标)修复方法
查看>>
PAT A1009 Product of Polynomials (25 分)——浮点,结构体数组
查看>>
Xen虚拟机克隆实战
查看>>
js文件三斜杠注释///reference path用途
查看>>
Ruby(或cmd中)输入命令行编译sass
查看>>
ThreadPoolExecutor线程池
查看>>
开源许可证的选择
查看>>
ZJOI2019二试游记
查看>>
一致性hash
查看>>
Oracle创建用户、角色、授权、建表
查看>>
umdh windbg分析内存泄露
查看>>
UITableView, 表视图
查看>>
教训 Mac下装windows系统 失败后 磁盘空间丢失
查看>>
poj2376 Cleaning Shifts 区间贪心
查看>>
下面介绍一下 Yii2.0 对数据库 查询的一些简单的操作
查看>>