C# .NET GridView数据导出Execl带图片一起

C# .NET 平台
把using Insus.NET;放到 Bin后,
在后台引用:using Insus.NET;

.NET 前端,
  <asp:TemplateField HeaderText="图片" ItemStyle-Width="50px" ItemStyle-Height="60px" >
                                            <ItemTemplate>    
                                               <img  src='<%#GetUrl(Eval("WorkshopNo"),Eval("Ln")) %>' style="width:50px;height:60px" alt="请上传图片"/>                                                 
                                            </ItemTemplate>
                                            <ItemStyle Width="60px" Height="60px" />
                                        </asp:TemplateField>        

记住要使用html 写法,不然如果图片名包含中文会显示解码过的路径的,显示解码过的路径Insus是识别不了的,如上面写法


Insus 也要使用全地址的写法,https也不能识别的,所以我也处理过图片地址的

//读取记录在数据库的指定路径
    protected string GetUrl(object No,object key)
    {
        string str = "";
        string urlKey = "";
        bll = new OrderProgressBll();
        if (No == null || No.ToString().Length == 0 || key.ToString().Trim().Length == 0 || key == null) { }else        
        urlKey = No.ToString().Trim() + "_" + key.ToString().Trim();

        List<string> images = bll.GetRnImagesByRnNo_s((urlKey), FunName);
        if (images != null)
        {
            if(images.Count >0)
                str = @"/Rnfiles/" + images[0].Trim();
            if (Request.Url.ToString().ToUpper().IndexOf("YSUN") > 0)
            {
                if (Request.Url.ToString().IndexOf("9988") < 1) //9988是端口号,S是表示缩略图的图片名
                {
                    str = Request.Url.ToString().ToUpper().Replace("S", "").Split('/')[0] + "//" + Request.Url.ToString().Split('/')[2] + ":9988/" + str;
                }
                else
                {
                    str = Request.Url.ToString().ToUpper().Replace("S", "").Split('/')[0] + "//" + Request.Url.ToString().Split('/')[2] + "/" + str;
                }
            }
            else
            {
                str = Request.Url.ToString().ToUpper().Replace("S", "").Split('/')[0] + "//" + Request.Url.ToString().Split('/')[2] + "/" + str;
            }
        }
        return str;
    }

在导出的时候我是这样子写的,GridView分页先展开,导出成EXECL 后再设分页

    /// <summary>
    /// 订单进度导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_export_Click(object sender, EventArgs e)
    {
        if (IsMasterView)
        {
            PrintStatus = 5;
            InsusExportToExcel objToexecl = new InsusExportToExcel();
            

              gvMasterList.Visible = false;
              gvMasterList.AllowPaging = false;
              SetColVisible(mainEditColIndex, false, gvMasterList);
               BindgvMaster(0);
              objToexecl.ExportToExcel(gvMasterList, "主计划" + DateTime.Now.ToShortDateString());
             //GftBase.GridViewToExcel(Response, DateTime.Now.ToShortDateString().ToString()+ "OrderPO", gvMasterList);          
              SetColVisible(mainEditColIndex, true, gvMasterList);
              gvMasterList.AllowPaging = true;
              gvMasterList.Visible = false;
              PrintStatus = -1;
        } 

    }

导出一图片有大有小,最好在上传的时候定义生好一个副图缩略图,我是用以下方便生成的(数据库记录了主图路径,缩略图只记录命名规则即可)

    /**/
    /// <summary>
    /// 生成缩略图
    /// </summary>
    /// <param name="originalImagePath">源图路径(物理路径)</param>
    /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
    /// <param name="width">缩略图宽度</param>
    /// <param name="height">缩略图高度</param>
    /// <param name="mode">生成缩略图的方式</param>    
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

        int towidth = width;
        int toheight = height;

        int x = 0;
        int y = 0;
        int ow = originalImage.Width;
        int oh = originalImage.Height;

        switch (mode)
        {
            case "HW"://指定高宽缩放(可能变形)                
                break;
            case "W"://指定宽,高按比例                    
                toheight = originalImage.Height * width / originalImage.Width;
                break;
            case "H"://指定高,宽按比例
                towidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut"://指定高宽裁减(不变形)                
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }
                break;
            default:
                break;
        }

        //新建一个bmp图片
        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

        //新建一个画板
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

        //设置高质量插值法
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //设置高质量,低速度呈现平滑程度
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //清空画布并以透明背景色填充
        g.Clear(System.Drawing.Color.Transparent);

        //在指定位置并且按指定大小绘制原图片的指定部分
        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);

        try
        {
            //以jpg格式保存缩略图
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {            
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

    /**/
    /// <summary>
    /// 在图片上增加文字水印
    /// </summary>
    /// <param name="Path">原服务器图片路径</param>
    /// <param name="Path_sy">生成的带文字水印的图片路径</param>
    protected void AddShuiYinWord(string Path, string Path_sy)
    {
        string addText = "测试水印";
        System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(image, 0, 0, image.Width, image.Height);
        System.Drawing.Font f = new System.Drawing.Font("Verdana", 16);
        System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);

        g.DrawString(addText, f, b, 15, 15);
        g.Dispose();

        image.Save(Path_sy);
        image.Dispose();
    }
    /**/
    /// <summary>
    /// 在图片上生成图片水印
    /// </summary>
    /// <param name="Path">原服务器图片路径</param>
    /// <param name="Path_syp">生成的带图片水印的图片路径</param>
    /// <param name="Path_sypf">水印图片路径</param>
    protected void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
        g.Dispose();

        image.Save(Path_syp);
        image.Dispose();
    }


    //附件或图片上传
    /// <summary>
    /// 文件上传至 D:\RDIMS\RnFiles 路径
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        try
        {    
            if (Fu_selFile.HasFile)
            {
                Attachment attm = new Attachment(-1);
                DocumentLink link = new DocumentLink(-1, -1);
                attm.Dept = SysUser.GetDept(Login.Login_id);
                attm.DocMType = SysTypeItems.GetSysTypeItemKey("文档管理分类", "其它资料");
                attm.ForHyperLink = true;

                link.LinkModule = FunName;
                link.LinkModuleNo = RnNo + "_" + RnMKey;
                link.LinkKey = RnMKey;

                Upload_Bll ulBll = new Upload_Bll();
                string relPath = ulBll.CreateFolder(link.LinkModule, link.LinkModuleNo, attm.ForHyperLink);
                string absPath = ulBll.GetfilePath(Fu_selFile.FileName, relPath, attm.ForHyperLink);
                string s_FileName = ulBll.GetfilePath("s_"+Fu_selFile.FileName, relPath, attm.ForHyperLink);//路图加_s表示缩略图
                if (Fu_selFile.FileName.ToUpper().IndexOf(".JPG") > 1 || Fu_selFile.FileName.ToUpper().IndexOf(".JPEG") > 1 || Fu_selFile.FileName.ToUpper().IndexOf(".PNG") > 1 || Fu_selFile.FileName.ToUpper().IndexOf(".GIF") > 1 || Fu_selFile.FileName.ToUpper().IndexOf(".BMP") > 1) { }else
                    throw new ApplicationException("你上传的不是图片,请上传图片格式文件(jpeg/jpg/gif/bmp)");                
                if ((Fu_selFile.FileContent.Length / 1024) > 700)
                    throw new ApplicationException("图片大小不能大于700KB,请使用QQ截图重新截取图片再上传");
               
                if (File.Exists(absPath))
                {
                    MsgShow("文件已存在,上传失败。请选择其它文件");
                    return;                  
                }
                Fu_selFile.SaveAs(absPath);                
                if (!File.Exists(s_FileName))
                {
                  ///webFilePath 服务器图片路径 webFilePath_s 缩略图路径
                  MakeThumbnail(absPath, s_FileName,50,60, "HW"); // 生成缩略图方法
                }

                try
                {
                    attm.FileLength = Fu_selFile.FileContent.Length / 1024;
                    attm.FileType = Upload_Bll.GetFileType(Path.GetExtension(Fu_selFile.FileName));
                    attm.Path = relPath;
                    attm.FileName = Fu_selFile.FileName;
                    attm.UpLoad_By = Login.Login_id;
                    attm.UpLoad_date = DateTime.Now;
                    attm.Update_Date = DateTime.Now;
                    attm.Update_By = Login.Login_id;
                    attm.Description = "";
                    link.DocConfirm = false;
                    attm.company = Login.CompanyCode;
                    attm.Add(link);
                }
                catch
                {
                    if (File.Exists(absPath))
                    {
                        File.Delete(absPath);
                    }
                    throw;
                }

                MsgShow("上传成功");
                BindPictures();
                bindfileInfo();               
                //SetImagesVisible();
                

            }
            else
            {
                MsgShow("请先选择上传的文件");
            }
        }
        catch (ImsException ix)
        {
            GftBase.ShowImsMessageDialog(this.Page, ix.GetMessage(Login.Lang), ImsMessageIcon.None);
        }
        catch (Exception ex)
        {
            GftBase.ShowImsMessageDialog(this.Page, ex.Message, ImsMessageIcon.None);
        }

    }

导出来后Execl

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那小x的传说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值