需求:根据PPT模板导出商品信息
需要用到的DLL:https://files.cnblogs.com/files/zhaoyl9/Aspose.Slides.zip
PPT模板:https://files.cnblogs.com/files/zhaoyl9/PPT%E6%A8%A1%E6%9D%BF.zip
导出效果:https://files.cnblogs.com/files/zhaoyl9/PPT%E5%95%86%E5%93%81%E6%96%87%E4%BB%B6.zip
#region 方案商品导出PPT /// <summary> /// 图片转二进制 /// </summary> /// <param name="img"></param> /// <returns></returns> public static byte[] GetByteImage(string imgPath) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgPath); byte[] bt = null; using (Stream stream = request.GetResponse().GetResponseStream()) { using (MemoryStream mstream = new MemoryStream()) { int count = 0; byte[] buffer = new byte[1024]; int readNum = 0; while ((readNum = stream.Read(buffer, 0, 1024)) > 0) { count = count + readNum; mstream.Write(buffer, 0, readNum); } mstream.Position = 0; using (BinaryReader br = new BinaryReader(mstream)) { bt = br.ReadBytes(count); } } } return bt; } /// <summary> /// 导出PPT /// </summary> /// <param name="tbScheme"></param> /// <param name="tbGoods"></param> /// <param name="exportType">2:单页1件商品;3:单页4件商品</param> public static void ExportPPTForSchemeGoods2(DataTable tbScheme, DataTable tbGoods) { string filePath = HttpContext.Current.Server.MapPath("/UpLoadFiles/UpLoadSelectGoods/选品导出工具PPT模板2.pptx"); Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(filePath); int index = 2; // 获取幻灯片:商品信息页 Aspose.Slides.ISlide slide = pres.Slides[3]; //从单张幻灯片中提取文本 foreach (DataRow item in tbGoods.Rows) { index++; //复制幻灯片 pres.Slides.InsertClone(index, slide); foreach (var shape in pres.Slides[index].Shapes) { #region 替换图片 if (shape is Aspose.Slides.PictureFrame) { Aspose.Slides.PictureFrame shapPicture = (Aspose.Slides.PictureFrame)shape; if (shape.AlternativeText == "商品主图") { string imgPath = item["imagePath"].ToString(); byte[] picBytes = GetByteImage(imgPath); Aspose.Slides.IPPImage imgx1 = pres.Images.AddImage(picBytes); shapPicture.PictureFormat.Picture.Image = imgx1; } if (shape.AlternativeText == "商品二维码") { ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter(); ZXing.Common.EncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions { //二维码放开这俩注释 DisableECI = true, CharacterSet = "UTF-8", PureBarcode = true, Width = Convert.ToInt32("400"), Height = Convert.ToInt32("400") }; writer.Format = ZXing.BarcodeFormat.QR_CODE; //这个是二维码 writer.Options = options; Bitmap bmp = writer.Write("http://m.yingkefuli.com/Shop/GiftProductDetail?ID=" + item["GoodsID"].ToString()); //内容不允许为空 System.IO.MemoryStream ms = new System.IO.MemoryStream(); try { bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Aspose.Slides.IPPImage imgx1 = pres.Images.AddImage(ms.ToArray()); shapPicture.PictureFormat.Picture.Image = imgx1; } finally { //显式释放资源 bmp.Dispose(); } } } #endregion #region 替换文字 if (shape is Aspose.Slides.AutoShape) { Aspose.Slides.IAutoShape autoShape = (Aspose.Slides.IAutoShape)shape; if (shape.AlternativeText == "方案名称") { autoShape.TextFrame.Text = tbScheme.Rows[0]["SchemeName"].ToString(); } if (shape.AlternativeText == "商品名称") { autoShape.TextFrame.Text = item["ProductName"].ToString(); } if (shape.AlternativeText == "市场价") { autoShape.TextFrame.Text = "¥" + item["MarketPrice"].ToString(); } if (shape.AlternativeText == "商品编号") { autoShape.TextFrame.Text = item["GoodsID"].ToString(); } if (shape.AlternativeText == "商品分类") { autoShape.TextFrame.Text = item["CategoryName"].ToString(); if (item["SubName"].ToString() != "") { autoShape.TextFrame.Text += "/" + item["SubName"].ToString(); } } if (shape.AlternativeText == "商品简介") { autoShape.TextFrame.Text = item["GoodsDescription"].ToString(); } } #endregion } } slide.Remove(); //获取最后一个幻灯片,替换联系人、联系电话 slide = pres.Slides[pres.Slides.Count - 1]; foreach (var shape in slide.Shapes) { if (shape is Aspose.Slides.AutoShape) { Aspose.Slides.IAutoShape autoShape = (Aspose.Slides.IAutoShape)shape; if (shape.AlternativeText == "联系人") { autoShape.TextFrame.Text = tbScheme.Rows[0]["Linkman"].ToString(); } if (shape.AlternativeText == "联系电话") { autoShape.TextFrame.Text = tbScheme.Rows[0]["LinkmanPhone"].ToString(); } } } var context = HttpContext.Current; string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pptx"; context.Response.Clear(); context.Response.ContentType = "application/vnd.ms-powerpoint"; // application/vnd.ms-powerpoint application/x-ppt context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); using (var ms = new MemoryStream()) { pres.Save(ms, Aspose.Slides.Export.SaveFormat.Pptx); context.Response.BinaryWrite(ms.ToArray()); context.ApplicationInstance.CompleteRequest(); } } public static void ExportPPTForSchemeGoods3(DataTable tbScheme, DataTable tbGoods) { string filePath = HttpContext.Current.Server.MapPath("/UpLoadFiles/UpLoadSelectGoods/选品导出工具PPT模板3.pptx"); Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(filePath); // 获取幻灯片:商品信息页 Aspose.Slides.ISlide slide = pres.Slides[3]; //需要复制的幻灯片总页数,每页4条商品信息 int totalPage = (tbGoods.Rows.Count + 4 - 1) / 4; for (int i = 0; i < totalPage; i++) { pres.Slides.InsertClone(i + 3, slide); } int num1 = 0; //记录商品信息行 //循环幻灯片页数 for (int i = 3; i < pres.Slides.Count; i++) { int rowIndex = 0; for (int j = num1; j < tbGoods.Rows.Count; j++) { rowIndex++; if (rowIndex > 4) { num1 = j; break; } foreach (var shape in pres.Slides[i].Shapes) { #region 替换图片 if (shape is Aspose.Slides.PictureFrame) { Aspose.Slides.PictureFrame shapPicture = (Aspose.Slides.PictureFrame)shape; if (shape.AlternativeText == rowIndex + "-商品主图") { string imgPath = tbGoods.Rows[j]["imagePath"].ToString(); byte[] picBytes = GetByteImage(imgPath); Aspose.Slides.IPPImage imgx1 = pres.Images.AddImage(picBytes); shapPicture.PictureFormat.Picture.Image = imgx1; } } #endregion #region 替换文字 if (shape is Aspose.Slides.AutoShape) { Aspose.Slides.IAutoShape autoShape = (Aspose.Slides.IAutoShape)shape; if (shape.AlternativeText == "方案名称") { autoShape.TextFrame.Text = tbScheme.Rows[0]["SchemeName"].ToString(); } if (shape.AlternativeText == rowIndex + "-商品编号+商品名称") { autoShape.TextFrame.Text = "【" + tbGoods.Rows[j]["GoodsID"].ToString() + "】" + tbGoods.Rows[j]["ProductName"].ToString(); } if (shape.AlternativeText == rowIndex + "-商品简介") { autoShape.TextFrame.Text = tbGoods.Rows[j]["GoodsDescription"].ToString(); } } #endregion if ((j + 1) == tbGoods.Rows.Count) { for (int k = (rowIndex + 1); k <= 4; k++) { #region 替换图片 if (shape is Aspose.Slides.PictureFrame) { Aspose.Slides.PictureFrame shapPicture = (Aspose.Slides.PictureFrame)shape; if (shape.AlternativeText == k + "-商品主图") { shape.Hidden = true; } } #endregion #region 替换文字 if (shape is Aspose.Slides.AutoShape) { Aspose.Slides.IAutoShape autoShape = (Aspose.Slides.IAutoShape)shape; if (shape.AlternativeText == k + "-商品编号+商品名称") { shape.Hidden = true; } if (shape.AlternativeText == k + "-商品简介") { shape.Hidden = true; } } #endregion } } } } } slide.Remove(); //删除商品信息模板页 //获取最后一个幻灯片,替换联系人、联系电话 slide = pres.Slides[pres.Slides.Count - 1]; foreach (var shape in slide.Shapes) { if (shape is Aspose.Slides.AutoShape) { Aspose.Slides.IAutoShape autoShape = (Aspose.Slides.IAutoShape)shape; if (shape.AlternativeText == "联系人") { autoShape.TextFrame.Text = tbScheme.Rows[0]["Linkman"].ToString(); } if (shape.AlternativeText == "联系电话") { autoShape.TextFrame.Text = tbScheme.Rows[0]["LinkmanPhone"].ToString(); } } } var context = HttpContext.Current; string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pptx"; context.Response.Clear(); context.Response.ContentType = "application/vnd.ms-powerpoint"; // application/vnd.ms-powerpoint application/x-ppt context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); using (var ms = new MemoryStream()) { pres.Save(ms, Aspose.Slides.Export.SaveFormat.Pptx); context.Response.BinaryWrite(ms.ToArray()); context.ApplicationInstance.CompleteRequest(); } } #endregion
一般处理程序调用:
if (flag == "ExportSchemeGoods") { #region 导出方案商品信息 #region 赋值 string SchemeCode = context.Request["SchemeCode"]; string GoodsID = context.Request["GoodsID"]; string GoodsName = context.Request["GoodsName"]; string MaxPrice = context.Request["MaxPrice"]; string MinPrice = context.Request["MinPrice"]; string MaxRate = context.Request["MaxRate"]; string MinRate = context.Request["MinRate"]; string GoodsStatus = context.Request["GoodsStatus"]; string SchemeBuyWay = context.Request["SchemeBuyWay"]; string ExportType = context.Request["ExportType"]; #endregion DataTable tbScheme = Ruby_BLL.SelectGoods.SelectGoodsScheme.GetSchemeByCode(SchemeCode); DataTable tbGoods = new DataTable(); ReturnObject ret = Ruby_BLL.SelectGoods.SelectGoodsScheme.GetSchemeGoodsDetail(SchemeCode, GoodsID, GoodsName, MaxPrice, MinPrice, MaxRate, MinRate, GoodsStatus, SchemeBuyWay, 0, 0, ExportType); if (ret.IsSuccessful) { if (ret.ResultSet.Tables[0] != null && ret.ResultSet.Tables[0].Rows.Count > 0) { tbGoods = ret.ResultSet.Tables[0]; if (ExportType == "0" || ExportType == "1") { byte[] data = NPOI_Tools.ExportExcelForSchemeGoods(tbScheme, tbGoods, "方案商品信息", ExportType); } if (ExportType == "2") { NPOI_Tools.ExportPPTForSchemeGoods2(tbScheme, tbGoods); } if (ExportType == "3") { NPOI_Tools.ExportPPTForSchemeGoods3(tbScheme, tbGoods); } } } #endregion }