.net 导出excel带图片

//如果单纯是想导出excel,可以移步这里:点击打开链接

  1. //此方法20条数据导出需要10秒左右,效率太低了,有时间需要再优化,关键是能导出图片了。  
  2. //先要需要引入Microsoft.Office.Interop.Excel.dll  
  3. //申明定义  
  4. Microsoft.Office.Interop.Excel.Workbook xlWorkBook;  
  5. Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;    
  6. protected void btnDown_Click(object sender, EventArgs e)  
  7.     {  
  8.           
  9.         string strWhere = " flag=1 ";  
  10.           
  11.         ds = dal.GetList(strWhere);//取数据  
  12.         dt = ds.Tables[0];  
  13.           
  14.         ImportDataToExcel(dt);  
  15.     }  
  16.     #region 导出  
  17.     private void ImportDataToExcel(DataTable dt)  
  18.     {  
  19.         if (dt != null)  
  20.         {  
  21.             #region 操作excel  
  22.             xlWorkBook = new Microsoft.Office.Interop.Excel.Application().Workbooks.Add(Type.Missing);  
  23.             xlWorkBook.Application.Visible = false;  
  24.             xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];  
  25.   
  26.             //设置标题  
  27.   
  28.             xlWorkSheet.Cells[1, 1] = "物品类型";  
  29.             xlWorkSheet.Cells[1, 2] = "物品名称";  
  30.             xlWorkSheet.Cells[1, 3] = "颜色";  
  31.             xlWorkSheet.Cells[1, 4] = "价格/元";  
  32.             xlWorkSheet.Cells[1, 5] = "重量/克";  
  33.             xlWorkSheet.Cells[1, 6] = "加工置换类型";  
  34.             xlWorkSheet.Cells[1, 7] = "是否提供来源";  
  35.   
  36.             //设置宽度,默认宽度和高度会改变图片的尺寸              
  37.             ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 19]).ColumnWidth = 50;  
  38.             ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 20]).ColumnWidth = 50;  
  39.             ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 21]).ColumnWidth = 50;  
  40.             ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 22]).ColumnWidth = 50;  
  41.             //设置字体  
  42.             xlWorkSheet.Cells.Font.Size = 12;  
  43.              
  44.             #endregion  
  45.  
  46.             #region 为excel赋值  
  47.   
  48.             for (int i = 0; i < dt.Rows.Count; i++)  
  49.             {  
  50.                 //设置高度  
  51.                 ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 19]).RowHeight = 200;  
  52.                 ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 20]).RowHeight = 200;  
  53.                 ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 21]).RowHeight = 200;  
  54.                 ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 22]).RowHeight = 200;  
  55.                 //为单元格赋值。  
  56.                 xlWorkSheet.Cells[i + 2, 1] = dt.Rows[i]["Type"] == null ? "" : SysDataMg.GetDataName(dt.Rows[i]["Type"].ToString(), "GoodType");  
  57.                 xlWorkSheet.Cells[i + 2, 2] = dt.Rows[i]["GoodName"] == null ? "" : dt.Rows[i]["GoodName"].ToString();  
  58.                 xlWorkSheet.Cells[i + 2, 11] = dt.Rows[i]["CarCode"] == null ? "" : "'" + dt.Rows[i]["CarCode"].ToString();  
  59.         //CarCode 是身份证号,在前面加个单引号,防止被excel保存为科学计数法  
  60.  
  61.                  
  62.                 #region  
  63.                 //可以直接取图片的地址  
  64.                 if (dt.Rows[i]["Img1"] != null)  
  65.                 {  
  66.                     string filename = dt.Rows[i] == null ? "" : Common.GetServerPath() + "WSJW/WSBB/" + dt.Rows[i]["Img1"].ToString();  
  67.                     //<span style="font-family: Arial, Helvetica, sans-serif;">Common.GetServerPath()  这儿方法是取当前网站所在硬盘的路径</span>  
  68.   
  69.                     int rangeindex = i + 2;//这里+2,是从第二行开始写入数据,第一行是标题  
  70.                     string rangename = "S" + rangeindex;//这里S是excel中列明  
  71.                     SavePic(rangename, filename);  
  72.                 }  
  73.                  
  74.                 #endregion  
  75.                   
  76.   
  77.             }  
  78.             #endregion  
  79.  
  80.             #region 保存excel文件  
  81.   
  82.             string filePath = Server.MapPath("excel") + "" + System.DateTime.Now.ToString().Replace(":""") + ".xls";  
  83.             xlWorkBook.SaveAs(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);  
  84.             xlWorkBook.Application.Quit();  
  85.             xlWorkSheet = null;  
  86.             xlWorkBook = null;  
  87.             GC.Collect();  
  88.             System.GC.WaitForPendingFinalizers();  
  89.  
  90.             #endregion  
  91.  
  92.  
  93.             #region 导出到客户端  
  94.               
  95.             Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");  
  96.             Response.AppendHeader("content-disposition""attachment;filename=" + Common.GetGuid() + ".xls");  
  97.             Response.ContentType = "Application/excel";  
  98.             Response.WriteFile(filePath);  
  99.             Response.End();  
  100.             #endregion  
  101.   
  102.             KillProcessexcel("EXCEL");  
  103.         }  
  104.     }  
  105.     #endregion  
  106.     private void SavePic(string rangename, string filename)  
  107.     {  
  108.   
  109.         Microsoft.Office.Interop.Excel.Range range = xlWorkSheet.get_Range(rangename, Type.Missing);  
  110.         range.Select();  
  111.         /  
  112.         float PicLeft, PicTop, PicWidth, PicHeight;    //距离左边距离,顶部距离,图片宽度、高度  
  113.         PicTop = Convert.ToSingle(range.Top);  
  114.         PicWidth = Convert.ToSingle(range.MergeArea.Width);  
  115.         PicHeight = Convert.ToSingle(range.Height);  
  116.         PicWidth = Convert.ToSingle(range.Width);  
  117.         PicLeft = Convert.ToSingle(range.Left);  
  118.           
  119.   
  120.         Microsoft.Office.Interop.Excel.Pictures pict = (Microsoft.Office.Interop.Excel.Pictures)xlWorkSheet.Pictures(Type.Missing);  
  121.         if (filename.IndexOf(".") > 0)  
  122.         {  
  123.             if (System.IO.File.Exists(filename))  
  124.             {  
  125.                 //pict.Insert(filename, Type.Missing);//显示原图   重叠在一起  
  126.                 xlWorkSheet.Shapes.AddPicture(filename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, PicLeft, PicTop, PicWidth, PicHeight);//指定位置显示小图  
  127.             }  
  128.         }  
  129.     }  
  130.     #region   
  131.     private void KillProcessexcel(string processName)  
  132.     { //获得进程对象,以用来操作  
  133.         System.Diagnostics.Process myproc = new System.Diagnostics.Process();  
  134.         //得到所有打开的进程  
  135.         try  
  136.         {  
  137.             //获得需要杀死的进程名  
  138.             foreach (Process thisproc in Process.GetProcessesByName(processName))  
  139.             { //立即杀死进程  
  140.                 thisproc.Kill();  
  141.             }  
  142.               
  143.         }  
  144.         catch (Exception Exc)  
  145.         {  
  146.             throw new Exception("", Exc);  
  147.         }  
  148.         finally  
  149.         {  
  150.         }  
  151.     }  
  152.     #endregion 

转载地址:http://blog.csdn.net/moniteryao/article/details/42718525

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值