导出带图片的Excel报表

         最近工作需要用到导出报表中带用图片,在网上一番苦找和自己实验,终于导出了图片,下面分享方法,希望有用

先引用 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll程序集,然后在后台页面引用using Excel = Microsoft.Office.Interop.Excel;

然后后台写下面这段:

        

#region 导出Excel
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;  
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;    
            protected void BtnExport_Click(object sender, EventArgs e)
            {
                string SN = this.txt_SN.Text;
                DataTable dt= CpacSNBLL.GetCpacSNBLL(SN);
                ImportDataToExcel(dt); 
             }
             #region 导出  
                  private void ImportDataToExcel(DataTable dt)  
                {  
                    if (dt != null)  
                    {  
                        #region 操作excel  
                        xlWorkBook = new Microsoft.Office.Interop.Excel.Application().Workbooks.Add(Type.Missing);  
                        xlWorkBook.Application.Visible = false;  
                        xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];  
  
                        //设置标题  
  
                        xlWorkSheet.Cells[1, 1] = "Model";  
                        xlWorkSheet.Cells[1, 2] = "Line";  
                        xlWorkSheet.Cells[1, 3] = "Item";  
                        xlWorkSheet.Cells[1, 4] = "Process";  
                        xlWorkSheet.Cells[1, 5] = "SeqNo";  
                        xlWorkSheet.Cells[1, 6] = "Type";  
                        xlWorkSheet.Cells[1, 7] = "Description";  
                        xlWorkSheet.Cells[1, 8] = "Images";  
                        xlWorkSheet.Cells[1, 9] = "Create_By";  
                        xlWorkSheet.Cells[1, 10] = "Create_Date";  
                        xlWorkSheet.Cells[1, 11] = "ACreate_By ";  
                        xlWorkSheet.Cells[1, 12] = "Amend_Dte";  
  
                        //设置宽度,默认宽度和高度会改变图片的尺寸  
                        ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 4]).ColumnWidth = 18;
                        ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 7]).ColumnWidth = 22;
                        ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 6]).ColumnWidth = 18;
                        ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 8]).ColumnWidth = 20;
                        ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 9]).ColumnWidth = 18;
                        ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 10]).ColumnWidth = 20;
                        ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[1, 12]).ColumnWidth = 20; 
                        //设置字体  
                        xlWorkSheet.Cells.Font.Size = 12;  
             
                #endregion  
 
                #region 为excel赋值  
  
                for (int i = 0; i < dt.Rows.Count; i++)  
                {  
                    //设置高度  
                    //((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 6]).ColumnWidth = 30;
                    ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 8]).RowHeight = 100;
                    ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 10]).RowHeight = 80;
                    ((Microsoft.Office.Interop.Excel.Range)xlWorkSheet.Cells[i + 2, 12]).RowHeight = 80; 
                    //为单元格赋值。  
                    xlWorkSheet.Cells[i + 2,1] = dt.Rows[i]["CPAC_Model"] == null ? "" : dt.Rows[i]["CPAC_Model"].ToString();
                    xlWorkSheet.Cells[i + 2,2] = dt.Rows[i]["CPAC_line"] == null ? "" : dt.Rows[i]["CPAC_line"].ToString();
                    xlWorkSheet.Cells[i + 2,3] = dt.Rows[i]["CPAC_Item"] == null ? "" :  dt.Rows[i]["CPAC_Item"].ToString();
                    xlWorkSheet.Cells[i + 2,5] = dt.Rows[i]["CPAC_SeqNO"] == null ? "" : dt.Rows[i]["CPAC_SeqNO"].ToString();
                    xlWorkSheet.Cells[i + 2,6] = dt.Rows[i]["CPAC_Type"] == null ? "" :  dt.Rows[i]["CPAC_Type"].ToString();
                    xlWorkSheet.Cells[i + 2,7] = dt.Rows[i]["CPAC_Description"] == null ? "" :  dt.Rows[i]["CPAC_Description"].ToString();
                    //xlWorkSheet.Cells[i + 2,8] = dt.Rows[i]["CPAC_Images"] == null ? "" :  dt.Rows[i]["CPAC_Images"].ToString();
                    xlWorkSheet.Cells[i + 2,9] = dt.Rows[i]["Create_By"] == null ? "" : dt.Rows[i]["Create_By"].ToString();
                    xlWorkSheet.Cells[i + 2,10] = dt.Rows[i]["Create_Dte"] == null ? "" : dt.Rows[i]["Create_Dte"].ToString();
                    xlWorkSheet.Cells[i + 2,11] = dt.Rows[i]["Amend_By"] == null ? "" :  dt.Rows[i]["Amend_By"].ToString();
                    xlWorkSheet.Cells[i + 2,12] = dt.Rows[i]["Amend_Dte"] == null ? "" : dt.Rows[i]["Amend_Dte"].ToString();
                 
                    #region  
                    //可以直接取图片的地址  
                    if (dt.Rows[i]["CPAC_Images"] != null)  
                    {
                        string filename = dt.Rows[i] == null ? "" : Server.MapPath("ActionImg/") + dt.Rows[i]["CPAC_Images"].ToString();  
                        int rangeindex = i + 2;//这里+2,是从第二行开始写入数据,第一行是标题  
                        string rangename = "H" + rangeindex;//这里S是excel中列明  
                        SavePic(rangename, filename);  
                    }  
                 
                    #endregion  
                  
  
                }  
                #endregion  
 
                #region 保存excel文件  
                    string time = System.DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss").Replace(":", "");

                    string filePath = Server.MapPath("Excel/") + "CPAC-" + time + ".xlsx";

                    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);

                    xlWorkBook.Application.Quit();

                    xlWorkSheet = null;

                    xlWorkBook = null;

                    GC.Collect();

                    System.GC.WaitForPendingFinalizers(); 
 
                #endregion  
 
 
                #region 导出到客户端  
              
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            
                    Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode("CPAC-"+System.DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss").Replace(":",""), System.Text.Encoding.UTF8)+".xlsx");
           
                    Response.ContentType = "Application/excel";  
                    Response.WriteFile(filePath);  
                    Response.End();  
                #endregion  
                //结束进程
                KillProcessexcel("EXCEL");   
                }  
            }  
            #endregion  
            private void SavePic(string rangename, string filename)  
            {  
  
                    Microsoft.Office.Interop.Excel.Range range = xlWorkSheet.get_Range(rangename, Type.Missing);  
                    range.Select();  
                    /  
                    float PicLeft, PicTop, PicWidth, PicHeight;    //距离左边距离,顶部距离,图片宽度、高度  
                    PicTop = Convert.ToSingle(range.Top);  
                    PicWidth = Convert.ToSingle(range.MergeArea.Width);  
                    PicHeight = Convert.ToSingle(range.Height);  
                    PicWidth = Convert.ToSingle(range.Width);  
                    PicLeft = Convert.ToSingle(range.Left);  
                      
  
                    Microsoft.Office.Interop.Excel.Pictures pict = (Microsoft.Office.Interop.Excel.Pictures)xlWorkSheet.Pictures(Type.Missing);  
                    if (filename.IndexOf(".") > 0)  
                    {  
                        if (System.IO.File.Exists(filename))  
                        {  
                            //pict.Insert(filename, Type.Missing);//显示原图   重叠在一起  
                            xlWorkSheet.Shapes.AddPicture(filename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, PicLeft, PicTop, PicWidth, PicHeight);//指定位置显示小图  
                        }  
                    }  
                }  
            #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);  
                }  
                finally  
                {  

                }  
            }  

            #endregion  
        #endregion

        以上方法即可实现导出图片。



       

你可能感兴趣的:(C#,文件,excel)