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