using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using QMFrameWork.WebUI.Attribute; using QMAPP.FJC.Entity.Produce; using QMAPP.Common.Web.Controllers; using QMAPP.ServicesAgent; using QMAPP.FJC.Entity.ProductPetrospect; using QMAPP.Entity; using QMFrameWork.WebUI.DataSource; using QMFrameWork.Common.Serialization; using QMAPP.FJC.Web.Models.ProductPetrospect; using System.Data; using Common.AssemblyUtil; using System.IO; using System.Diagnostics; using iTextSharp; using iTextSharp.text; using iTextSharp.text.pdf; using QMAPP.FJC.Entity.Operation; using QMAPP.FJC.Entity; using QMAPP.FJC.Entity.Basic; namespace QMAPP.FJC.Web.Controllers { /// /// 模块编号:M5-1 /// 作 用:产品档案控制层 /// 作 者:王丹丹 /// 编写日期:2015年06月08日 /// public class ProductArchivesController : QController { #region 初始化页面 public ActionResult Index() { return View(); } [HttpPost] [HandleException] public ActionResult Index(bool? callBack) { ProductArchivesModel seachModel = new ProductArchivesModel(); ServiceAgent wcfAgent = this.GetServiceAgent(); //ProductArchives condition = new ProductArchives(); DataResult dataResult = new DataResult(); //List list = new List(); try { //获取查询对象 seachModel = GetModel(); #region 获取缓存值 if (callBack != null) { TryGetSelectBuffer(out seachModel); } else { //保存搜索条件 SetSelectBuffer(seachModel); } #endregion #region wcf服务统一接口 dataResult = wcfAgent.InvokeServiceFunction>("ProductBLL_GetProductInfoByAnyBarcode", seachModel.ERMISCODE); if(dataResult.IsSuccess) { seachModel.ProductInfo = dataResult.Result; Session.Add("ProductInfo", seachModel.ProductInfo); } else { SetMessage(dataResult.Msg); } #endregion //return Json(new { Msg = dataResult.Msg, IsSuccess = dataResult.IsSuccess.ToString(), Result = list }); return View(seachModel); } catch (Exception ex) { throw ex; } } [HandleException] public ActionResult LoadPartList(string productCode) { if (string.IsNullOrWhiteSpace(productCode)) { return View("PartList"); } ServiceAgent wcfAgent = this.GetServiceAgent(); //ProductArchives condition = new ProductArchives(); //List list = new List(); try { #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>("ProductBLL_GetPartList", productCode); #endregion //return Json(new { Msg = dataResult.Msg, IsSuccess = dataResult.IsSuccess.ToString(), Result = list }); Session.Add("PartList", dataResult); return View("PartList", dataResult); } catch (Exception ex) { throw ex; } } [HandleException] public ActionResult LoadShipment(string productCode) { if (string.IsNullOrWhiteSpace(productCode)) { return View("Shipment"); } ServiceAgent wcfAgent = this.GetServiceAgent(); try { #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction("ProductBLL_GetShipmentInfo", productCode); #endregion Session.Add("Shipment", dataResult); return View("Shipment", dataResult); } catch (Exception ex) { throw ex; } } [HandleException] public ActionResult LoadProcessRecord(string productCode) { if (string.IsNullOrWhiteSpace(productCode)) { return View("ProcessRecord"); } ServiceAgent wcfAgent = this.GetServiceAgent(); try { #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction>("MainOperationBLL_GetOperationRecords", productCode); #endregion return View("ProcessRecord", dataResult); } catch (Exception ex) { throw ex; } } [HttpPost] [HandleException] public ActionResult LoadParameter(string productcode, string machinecode, string tables, string fileds) { ServiceAgent wcfAgent = this.GetServiceAgent(); try { var condition = new ProcessParameter { PARATABLENAME =tables, Fileds = fileds, MACHINECODDE = machinecode, ProductCode = productcode }; #region wcf服务统一接口 var dataResult = wcfAgent.InvokeServiceFunction("ProcessParameterBLL_GetParameter", condition); #endregion return Content(dataResult); } catch (Exception ex) { throw ex; } } #endregion #region 获取列表 /// /// 获取列表 /// /// 是否回调 /// 列表 [HandleException] public ActionResult GetExportParameter(bool? callBack) { ProductArchivesModel seachModel = new ProductArchivesModel(); ServiceAgent wcfAgent = this.GetServiceAgent(); ProductArchives condition = new ProductArchives(); DataResult dataResult = new DataResult(); List list = new List(); try { //获取查询对象 seachModel = GetModel(); #region 获取缓存值 if (callBack != null) { TryGetSelectBuffer(out seachModel); } else { //保存搜索条件 SetSelectBuffer(seachModel); } #endregion condition = CopyToModel(seachModel); #region wcf服务统一接口 //dataResult = wcfAgent.InvokeServiceFunction>(QMAPP.ServicesAgent.ProductPetrospect.ProductArchivesBLL_GetProductParameter.ToString(), condition); var dl = wcfAgent.InvokeServiceFunction("ProductArchivesBLL_GetProductParameterNew1", condition); //dl = dl.OrderBy(x => x.MEMO).ToList(); //list = EntityFactory.GetEntities(dataResult.Result); #endregion //return Json(new { Msg = dataResult.Msg, IsSuccess = dataResult.IsSuccess.ToString(), Result = list }); return Content(dl); } catch (Exception ex) { throw ex; } } #endregion #region 导出pdf /// /// 导出excel /// /// 结果 [HttpPost] public ActionResult ExportExcel() { ProductArchivesModel seachModel = null; ProductArchives condition = null; DataResult exportDs = new DataResult(); ServiceAgent wcfAgent = this.GetServiceAgent(); string savePathName = Request["savePath"]; string ermiscode = Request["ermiscode"]; string maincode = Request["maincode"]; string sourcePath =""; try { //获取查询对象 seachModel = GetModel(); TryGetSelectBuffer(out seachModel); condition = CopyToModel(seachModel); condition.ERMISCODE = ermiscode; condition.MAINCODE = maincode; //获取加工参数数据 exportDs = wcfAgent.InvokeServiceFunction>(QMAPP.ServicesAgent.ProductPetrospect.ProductArchivesBLL_GetExportData.ToString(), condition); if (exportDs.IsSuccess == false) { SetMessage(exportDs.Msg); return View("Index"); } //获取本体信息 Main main = wcfAgent.InvokeServiceFunction
(QMAPP.ServicesAgent.ProductPetrospect.ProductArchivesBLL_GetMainInfo.ToString(), condition); //获取加工记录信息 //MainOperation mainOperation = wcfAgent.InvokeServiceFunction(QMAPP.ServicesAgent.ProductPetrospect.ProductArchivesBLL_GetMainOperationInfo.ToString(), condition); MainOperation mainOperation = new MainOperation(); //生成pdf文件到“App_Data/ProductArchives.pdf”模板中 ExportPDF(exportDs.Result, main, mainOperation); #region 下载文件到客户端 //将生成的pdf文件“App_Data/ProductArchives.pdf”下载到客户端 //pdf文件模板路径 sourcePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ProductArchives.pdf"; //保存的文件及路径 savePathName ="c:\\"+ savePathName + ".pdf"; //保存的文件路径 string savePath = savePathName.Substring(0, savePathName.LastIndexOf("\\")); if (!System.IO.Directory.Exists(savePath)) { // 目录不存在,建立目录 System.IO.Directory.CreateDirectory(savePath); } //判断同名文件是否存在 if (System.IO.File.Exists(savePathName)) { // 如果存在 exportDs.Msg = "同名文件已存在!"; exportDs.IsSuccess = false; return View("Index"); } #endregion exportDs.IsSuccess = true; } catch (Exception ex) { exportDs.IsSuccess = false; exportDs.Msg = ex.Message; throw exportDs.Ex; } return File(sourcePath, "Application/pdf", string.Format("{0}.pdf", string.IsNullOrEmpty(maincode) ? ermiscode : maincode)); } #endregion #region 生成PDF文件到“App_Data/ProductArchives.pdf”模板中 /// /// 生成PDF文件到“App_Data/ProductArchives.pdf”模板中 /// /// 加工参数 /// 本体 /// 加工记录 public void ExportPDF( DataSet ds, Main main, MainOperation mainOperation) { string txtheader = "产品档案信息"; //创建一个iTextSharp.text.Document 对象的实例: Document document = new Document(PageSize.A4);//纸张大小设置 //导出模板路径 string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //为该Document 创建一个Writer 实例: PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path + "ProductArchives.pdf", FileMode.Create)); //设置文档的属性 document.AddAuthor("XXX");//作者 document.AddCreator("XXX,江森B9管理系统!"); document.AddKeywords("PDF,存档," + txtheader); document.AddSubject(txtheader + "PDF文档导出存档"); document.AddTitle(txtheader);//标题 #region 添加页脚 //设置页脚的字体 BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //添加页脚 iTextSharp.text.Font FontFooter = new iTextSharp.text.Font(bfChinese, 10); HeaderFooter footer = new HeaderFooter(new Phrase(" 第 ", FontFooter), new Phrase(" 页", FontFooter)); footer.IsNumbered(); footer.Border = Rectangle.NO_BORDER; footer.Alignment = Rectangle.ALIGN_RIGHT; document.Footer = footer; #endregion try { //开启Document document.Open(); #region 设置字体 //第四步,为当前Document 添加内容:做了三项1:段落 2:列表 3:表格 Font fontChinese = new iTextSharp.text.Font(bfChinese, 10); Font fontHeadInfo = new iTextSharp.text.Font(bfChinese, 13); Font fontbz = new iTextSharp.text.Font(bfChinese, 13, iTextSharp.text.Font.BOLD); Font fontfl = new iTextSharp.text.Font(bfChinese, 17, iTextSharp.text.Font.BOLD); Font font = new iTextSharp.text.Font(bfChinese, 20, iTextSharp.text.Font.BOLD); #endregion #region 表头信息 //创建表单 PdfPTable tableTitle = new PdfPTable(8); PdfPCell cellhead = new PdfPCell(tableTitle); cellhead.PaddingBottom = 0; cellhead.PaddingTop = 10; tableTitle.SplitLate = false; //图片 string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; Image image = Image.GetInstance(new Uri(str + "Content/Img/pdfyemei.bmp")); PdfPCell cellimg = new PdfPCell(image, true); cellimg.Colspan = 8; cellimg.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellimg); PdfPCell cellTitle1 = new PdfPCell(); cellTitle1.AddElement(new Phrase("Traceability Report", fontbz)); cellTitle1.Colspan = 8; cellTitle1.VerticalAlignment = Element.ALIGN_LEFT; cellTitle1.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle1); PdfPCell cellTitle2 = new PdfPCell(); cellTitle2.AddElement(new Phrase("Plant:", fontHeadInfo)); cellTitle2.VerticalAlignment = Element.ALIGN_LEFT; cellTitle2.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle2); PdfPCell cellTitle3 = new PdfPCell(); cellTitle3.AddElement(new Phrase("江森", fontHeadInfo)); cellTitle3.VerticalAlignment = Element.ALIGN_LEFT; cellTitle3.BorderColor = new Color(255, 255, 255); cellTitle3.Colspan = 2; tableTitle.AddCell(cellTitle3); PdfPCell cellTitle4 = new PdfPCell(); cellTitle4.AddElement(new Phrase("Product Code:", fontHeadInfo)); cellTitle4.VerticalAlignment = Element.ALIGN_LEFT; cellTitle4.Colspan = 2; cellTitle4.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle4); PdfPCell cellTitle5 = new PdfPCell(); cellTitle5.AddElement(new Phrase(main.MAINCODE, fontHeadInfo)); cellTitle5.VerticalAlignment = Element.ALIGN_LEFT; cellTitle5.Colspan = 3; cellTitle5.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle5); PdfPCell cellTitle6 = new PdfPCell(); cellTitle6.AddElement(new Phrase("Line:", fontHeadInfo)); cellTitle6.VerticalAlignment = Element.ALIGN_LEFT; cellTitle6.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle6); PdfPCell cellTitle7 = new PdfPCell(); cellTitle7.AddElement(new Phrase("B9", fontHeadInfo)); cellTitle7.VerticalAlignment = Element.ALIGN_LEFT; cellTitle7.Colspan = 7; cellTitle7.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle7); #region 分割线(第5、6行) //分割线 PdfPCell cellTitle10 = new PdfPCell(); cellTitle10.AddElement(new Phrase("", fontHeadInfo)); cellTitle10.Colspan = 8; cellTitle10.BorderWidthBottom = 3f; cellTitle10.Border = Rectangle.BOTTOM_BORDER; tableTitle.AddCell(cellTitle10); PdfPCell cellInfoKong = new PdfPCell(); cellInfoKong.AddElement(new Phrase("", fontHeadInfo)); cellInfoKong.VerticalAlignment = Element.ALIGN_RIGHT; cellInfoKong.Colspan = 8; cellInfoKong.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfoKong); #endregion // 告诉程序这行是表头,这样页数大于1时程序会自动为你加上表头。 tableTitle.HeaderRows = 6; #endregion #region 零件信息 //零件信息 #region 产品类别 PdfPCell cellInfo1 = new PdfPCell(); cellInfo1.AddElement(new Phrase("Product Type:", fontHeadInfo)); cellInfo1.VerticalAlignment = Element.ALIGN_RIGHT; cellInfo1.Colspan = 3; cellInfo1.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo1); PdfPCell cellInfo2 = new PdfPCell(); cellInfo2.AddElement(new Phrase("Dash Board", fontHeadInfo)); cellInfo2.VerticalAlignment = Element.ALIGN_LEFT; cellInfo2.Colspan = 5; cellInfo2.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo2); #endregion #region 产品状态(是否合格) PdfPCell cellInfo3 = new PdfPCell(); cellInfo3.AddElement(new Phrase("Product Status:", fontHeadInfo)); cellInfo3.VerticalAlignment = Element.ALIGN_RIGHT; cellInfo3.Colspan = 3; cellInfo3.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo3); PdfPCell cellInfo4 = new PdfPCell(); if (main.STATUS == EnumGeter.PRODUCTSTAUTS.QUALIFIED.GetHashCode().ToString()) { cellInfo4.AddElement(new Phrase("OK", fontHeadInfo)); } else { cellInfo4.AddElement(new Phrase("NO", fontHeadInfo)); } cellInfo4.VerticalAlignment = Element.ALIGN_LEFT; cellInfo4.Colspan = 5; cellInfo4.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo4); #endregion #region 产品加工开始时间 PdfPCell cellInfo5 = new PdfPCell(); cellInfo5.AddElement(new Phrase("Production Start:", fontHeadInfo)); cellInfo5.VerticalAlignment = Element.ALIGN_RIGHT; cellInfo5.Colspan = 3; cellInfo5.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo5); PdfPCell cellInfo6 = new PdfPCell(); cellInfo6.AddElement(new Phrase(main.CREATEDATE.ToString("yyyy.MM.dd hh:mm:ss"), fontHeadInfo)); cellInfo6.VerticalAlignment = Element.ALIGN_LEFT; cellInfo6.Colspan = 5; cellInfo6.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo6); #endregion #region 产品加工完成时间 PdfPCell cellInfo7 = new PdfPCell(); cellInfo7.AddElement(new Phrase("Production End:", fontHeadInfo)); cellInfo7.VerticalAlignment = Element.ALIGN_RIGHT; cellInfo7.Colspan = 3; cellInfo7.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo7); PdfPCell cellInfo8 = new PdfPCell(); cellInfo8.AddElement(new Phrase(main.COMPLETETIME.ToString("yyyy.MM.dd hh:mm:ss"), fontHeadInfo)); cellInfo8.VerticalAlignment = Element.ALIGN_LEFT; cellInfo8.Colspan = 5; cellInfo8.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo8); #endregion #region 产品加工时长 //计算产品加工时长(产品加工完成时间-产品加工开始时间) TimeSpan TimeCount = main.COMPLETETIME - main.CREATEDATE; int days = TimeCount.Days; int hours = TimeCount.Hours; int minites = TimeCount.Minutes; int secound = TimeCount.Seconds; string timeCount = days + "days" + hours + ":" + minites + ":" + secound; PdfPCell cellInfo9 = new PdfPCell(); cellInfo9.AddElement(new Phrase("Duration:", fontHeadInfo)); cellInfo9.VerticalAlignment = Element.ALIGN_RIGHT; cellInfo9.Colspan = 3; cellInfo9.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo9); PdfPCell cellInfo10 = new PdfPCell(); if (main.COMPLETETIME != DateTime.MinValue && main.CREATEDATE != DateTime.MinValue) { cellInfo10.AddElement(new Phrase(timeCount, fontHeadInfo)); } else { cellInfo10.AddElement(new Phrase("", fontHeadInfo)); } cellInfo10.VerticalAlignment = Element.ALIGN_LEFT; cellInfo10.Colspan = 5; cellInfo10.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo10); #endregion #region 产品加工状态 PdfPCell cellInfo11 = new PdfPCell(); cellInfo11.AddElement(new Phrase("Production Status:", fontHeadInfo)); cellInfo11.VerticalAlignment = Element.ALIGN_RIGHT; cellInfo11.Colspan = 3; cellInfo11.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo11); PdfPCell cellInfo12 = new PdfPCell(); if (main.COMPLETEFLAG == EnumGeter.COMPLETEFLAG.COMPLETED.GetHashCode().ToString()) { cellInfo12.AddElement(new Phrase("finished", fontHeadInfo)); } else { cellInfo12.AddElement(new Phrase("Processing", fontHeadInfo)); } cellInfo12.VerticalAlignment = Element.ALIGN_LEFT; cellInfo12.Colspan = 5; cellInfo12.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfo12); #endregion //插入空行 PdfPCell cellInfoKong3 = new PdfPCell(); cellInfoKong3.AddElement(new Phrase("", fontHeadInfo)); cellInfoKong3.VerticalAlignment = Element.ALIGN_RIGHT; cellInfoKong3.Colspan = 8; cellInfoKong3.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfoKong3); //表名 PdfPCell cellTitle11 = new PdfPCell(); cellTitle11.AddElement(new Phrase(txtheader, fontbz)); cellTitle11.VerticalAlignment = Element.ALIGN_LEFT; cellTitle11.Colspan = 8; cellTitle11.Border = Rectangle.TOP_BORDER; tableTitle.AddCell(cellTitle11); //插入空行 PdfPCell cellInfoKong2 = new PdfPCell(); cellInfoKong2.AddElement(new Phrase("", fontHeadInfo)); cellInfoKong2.VerticalAlignment = Element.ALIGN_RIGHT; cellInfoKong2.BorderColor = new Color(255, 255, 255); cellInfoKong2.Colspan = 8; tableTitle.AddCell(cellInfoKong2); #endregion #region 添加数据 //添加数据标题 for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { PdfPCell cellTitle = new PdfPCell(); cellTitle.AddElement(new Phrase(ds.Tables[0].Columns[j].ColumnName, fontbz)); cellTitle.Colspan = 4; cellTitle.BackgroundColor = new Color(220, 220, 220); tableTitle.AddCell(cellTitle); } #region 添加数据 // 添加数据 string Str = ""; string[] StrSplit; //遍历原gridview的数据行 foreach (DataTable dt in ds.Tables) { for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { PdfPCell cell1 = new PdfPCell(); Str = dt.Rows[i][j].ToString(); //處理   Str = Str.Replace(" ", " "); cell1.AddElement(new Phrase(Str, fontChinese)); if (i == 0 && j == 0) { cell1.Border = Rectangle.LEFT_BORDER | Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER; cell1.BackgroundColor = new Color(239, 239, 239); } if (i == 0 && j == 1) { cell1.Border = Rectangle.RIGHT_BORDER | Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER; cell1.BackgroundColor = new Color(239, 239, 239); } cell1.Colspan = 4; tableTitle.AddCell(cell1); StrSplit = Str.Split(' '); for (int k = 0; k < StrSplit.Length; k++) cell1.AddElement(new Phrase(StrSplit[k], fontChinese)); } } } #endregion PdfPCell cellfoot = new PdfPCell(); cellfoot.AddElement(new Phrase("", fontHeadInfo)); cellfoot.Colspan = 8; cellfoot.Border = Rectangle.TOP_BORDER; tableTitle.AddCell(cellfoot); document.Add(tableTitle); #endregion //关闭Document document.Close(); } catch (Exception ex) { throw ex; } } #endregion #region Q5导出pdf /// /// /// /// public ActionResult ExportExcelNew() { ProductArchivesModel seachModel = null; ProductArchives condition = null; DataResult> exportDs = new DataResult>(); ServiceAgent wcfAgent = this.GetServiceAgent(); string savePathName = Request["savePath"]; string ermiscode = Request["ermiscode"]; string maincode = Request["maincode"]; string sourcePath = ""; try { //获取查询对象 seachModel = GetModel(); TryGetSelectBuffer(out seachModel); condition = CopyToModel(seachModel); condition.ERMISCODE = ermiscode; condition.MAINCODE = maincode; //获取加工参数数据 exportDs = wcfAgent.InvokeServiceFunction>>("ProductArchivesBLL_GetExportDataNew", condition); if (exportDs.IsSuccess == false) { SetMessage(exportDs.Msg); return View("Index"); } //生成pdf文件到“App_Data/ProductArchives.pdf”模板中 ExportPDFNew(exportDs.Result); #region 下载文件到客户端 //将生成的pdf文件“App_Data/ProductArchives.pdf”下载到客户端 //pdf文件模板路径 sourcePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ProductArchives.pdf"; //保存的文件及路径 savePathName = "c:\\" + savePathName + ".pdf"; //保存的文件路径 string savePath = savePathName.Substring(0, savePathName.LastIndexOf("\\")); if (!System.IO.Directory.Exists(savePath)) { // 目录不存在,建立目录 System.IO.Directory.CreateDirectory(savePath); } //判断同名文件是否存在 if (System.IO.File.Exists(savePathName)) { // 如果存在 exportDs.Msg = "同名文件已存在!"; exportDs.IsSuccess = false; return View("Index"); } #endregion exportDs.IsSuccess = true; } catch (Exception ex) { exportDs.IsSuccess = false; exportDs.Msg = ex.Message; throw exportDs.Ex; } return File(sourcePath, "Application/pdf", string.Format("{0}.pdf", string.IsNullOrEmpty(maincode) ? ermiscode : maincode)); } #endregion #region Q5生成PDF文件到“App_Data/ProductArchives.pdf”模板中 /// /// /// /// public void ExportPDFNew(List ds) { string txtheader = "产品档案信息"; //创建一个iTextSharp.text.Document 对象的实例: Document document = new Document(PageSize.A4);//纸张大小设置 //导出模板路径 string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //为该Document 创建一个Writer 实例: PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path + "ProductArchives.pdf", FileMode.Create)); //设置文档的属性 document.AddAuthor("XXX");//作者 document.AddCreator("XXX,富维安道拓数字化工厂!"); document.AddKeywords("PDF,存档," + txtheader); document.AddSubject(txtheader + "PDF文档导出存档"); document.AddTitle(txtheader);//标题 #region 添加页脚 //设置页脚的字体 BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //添加页脚 iTextSharp.text.Font FontFooter = new iTextSharp.text.Font(bfChinese, 10); HeaderFooter footer = new HeaderFooter(new Phrase(" 第 ", FontFooter), new Phrase(" 页", FontFooter)); footer.IsNumbered(); footer.Border = Rectangle.NO_BORDER; footer.Alignment = Rectangle.ALIGN_RIGHT; document.Footer = footer; #endregion try { //开启Document document.Open(); #region 设置字体 //第四步,为当前Document 添加内容:做了三项1:段落 2:列表 3:表格 Font fontChinese = new iTextSharp.text.Font(bfChinese, 10); Font fontHeadInfo = new iTextSharp.text.Font(bfChinese, 13); Font fontbz = new iTextSharp.text.Font(bfChinese, 13, iTextSharp.text.Font.BOLD); Font fontfl = new iTextSharp.text.Font(bfChinese, 17, iTextSharp.text.Font.BOLD); Font font = new iTextSharp.text.Font(bfChinese, 20, iTextSharp.text.Font.BOLD); #endregion #region 表头信息 //创建表单 PdfPTable tableTitle = new PdfPTable(12); PdfPCell cellhead = new PdfPCell(tableTitle); cellhead.PaddingBottom = 0; cellhead.PaddingTop = 10; tableTitle.SplitLate = false; //图片 string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; Image image = Image.GetInstance(new Uri(str + "Content/Img/pdfyemei.bmp")); PdfPCell cellimg = new PdfPCell(image, true); cellimg.Colspan = 12; cellimg.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellimg); PdfPCell cellTitle1 = new PdfPCell(); cellTitle1.AddElement(new Phrase("Traceability Report", fontbz)); cellTitle1.Colspan = 12; cellTitle1.VerticalAlignment = Element.ALIGN_LEFT; cellTitle1.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle1); PdfPCell cellTitle2 = new PdfPCell(); cellTitle2.AddElement(new Phrase("Plant:", fontHeadInfo)); cellTitle2.VerticalAlignment = Element.ALIGN_LEFT; cellTitle2.BorderColor = new Color(255, 255, 255); cellTitle2.Colspan = 2; tableTitle.AddCell(cellTitle2); PdfPCell cellTitle3 = new PdfPCell(); cellTitle3.AddElement(new Phrase("富维安道拓成都工厂", fontHeadInfo)); cellTitle3.VerticalAlignment = Element.ALIGN_LEFT; cellTitle3.BorderColor = new Color(255, 255, 255); cellTitle3.Colspan = 4; tableTitle.AddCell(cellTitle3); //PdfPCell cellTitle4 = new PdfPCell(); //cellTitle4.AddElement(new Phrase("Product Code:", fontHeadInfo)); //cellTitle4.VerticalAlignment = Element.ALIGN_LEFT; //cellTitle4.Colspan = 2; //cellTitle4.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellTitle4); //PdfPCell cellTitle5 = new PdfPCell(); //cellTitle5.AddElement(new Phrase("Product Code:", fontHeadInfo)); //cellTitle5.VerticalAlignment = Element.ALIGN_LEFT; //cellTitle5.Colspan = 3; //cellTitle5.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellTitle5); PdfPCell cellTitle6 = new PdfPCell(); cellTitle6.AddElement(new Phrase("Line:", fontHeadInfo)); cellTitle6.VerticalAlignment = Element.ALIGN_LEFT; cellTitle6.BorderColor = new Color(255, 255, 255); cellTitle6.Colspan = 2; tableTitle.AddCell(cellTitle6); PdfPCell cellTitle7 = new PdfPCell(); cellTitle7.AddElement(new Phrase(" ", fontHeadInfo)); cellTitle7.VerticalAlignment = Element.ALIGN_LEFT; cellTitle7.Colspan = 4; cellTitle7.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellTitle7); #region 分割线(第5、6行) //分割线 PdfPCell cellTitle10 = new PdfPCell(); cellTitle10.AddElement(new Phrase("", fontHeadInfo)); cellTitle10.Colspan = 12; cellTitle10.BorderWidthBottom = 3f; cellTitle10.Border = Rectangle.BOTTOM_BORDER; tableTitle.AddCell(cellTitle10); PdfPCell cellInfoKong = new PdfPCell(); cellInfoKong.AddElement(new Phrase("", fontHeadInfo)); cellInfoKong.VerticalAlignment = Element.ALIGN_RIGHT; cellInfoKong.Colspan = 12; cellInfoKong.BorderColor = new Color(255, 255, 255); tableTitle.AddCell(cellInfoKong); #endregion // 告诉程序这行是表头,这样页数大于1时程序会自动为你加上表头。 tableTitle.HeaderRows = 6; #endregion #region 零件信息 ////零件信息 //#region 产品类别 //PdfPCell cellInfo1 = new PdfPCell(); //cellInfo1.AddElement(new Phrase("Product Type:", fontHeadInfo)); //cellInfo1.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfo1.Colspan = 3; //cellInfo1.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo1); //PdfPCell cellInfo2 = new PdfPCell(); //cellInfo2.AddElement(new Phrase("Dash Board", fontHeadInfo)); //cellInfo2.VerticalAlignment = Element.ALIGN_LEFT; //cellInfo2.Colspan = 5; //cellInfo2.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo2); //#endregion //#region 产品状态(是否合格) //PdfPCell cellInfo3 = new PdfPCell(); //cellInfo3.AddElement(new Phrase("Product Status:", fontHeadInfo)); //cellInfo3.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfo3.Colspan = 3; //cellInfo3.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo3); //PdfPCell cellInfo4 = new PdfPCell(); //cellInfo4.AddElement(new Phrase("OK", fontHeadInfo)); //cellInfo4.VerticalAlignment = Element.ALIGN_LEFT; //cellInfo4.Colspan = 5; //cellInfo4.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo4); //#endregion //#region 产品加工开始时间 //PdfPCell cellInfo5 = new PdfPCell(); //cellInfo5.AddElement(new Phrase("Production Start:", fontHeadInfo)); //cellInfo5.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfo5.Colspan = 3; //cellInfo5.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo5); //PdfPCell cellInfo6 = new PdfPCell(); //cellInfo6.AddElement(new Phrase("Production Start:", fontHeadInfo)); //cellInfo6.VerticalAlignment = Element.ALIGN_LEFT; //cellInfo6.Colspan = 5; //cellInfo6.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo6); //#endregion //#region 产品加工完成时间 //PdfPCell cellInfo7 = new PdfPCell(); //cellInfo7.AddElement(new Phrase("Production End:", fontHeadInfo)); //cellInfo7.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfo7.Colspan = 3; //cellInfo7.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo7); //PdfPCell cellInfo8 = new PdfPCell(); //cellInfo8.AddElement(new Phrase("Production End:", fontHeadInfo)); //cellInfo8.VerticalAlignment = Element.ALIGN_LEFT; //cellInfo8.Colspan = 5; //cellInfo8.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo8); //#endregion //#region 产品加工时长 ////计算产品加工时长(产品加工完成时间-产品加工开始时间) ////TimeSpan TimeCount = main.COMPLETETIME - main.CREATEDATE; ////int days = TimeCount.Days; ////int hours = TimeCount.Hours; ////int minites = TimeCount.Minutes; ////int secound = TimeCount.Seconds; ////string timeCount = days + "days" + hours + ":" + minites + ":" + secound; //PdfPCell cellInfo9 = new PdfPCell(); //cellInfo9.AddElement(new Phrase("Duration:", fontHeadInfo)); //cellInfo9.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfo9.Colspan = 3; //cellInfo9.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo9); //PdfPCell cellInfo10 = new PdfPCell(); //cellInfo10.AddElement(new Phrase("", fontHeadInfo)); //cellInfo10.VerticalAlignment = Element.ALIGN_LEFT; //cellInfo10.Colspan = 5; //cellInfo10.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo10); //#endregion //#region 产品加工状态 //PdfPCell cellInfo11 = new PdfPCell(); //cellInfo11.AddElement(new Phrase("Production Status:", fontHeadInfo)); //cellInfo11.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfo11.Colspan = 3; //cellInfo11.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo11); //PdfPCell cellInfo12 = new PdfPCell(); //cellInfo12.AddElement(new Phrase("finished", fontHeadInfo)); //cellInfo12.VerticalAlignment = Element.ALIGN_LEFT; //cellInfo12.Colspan = 5; //cellInfo12.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfo12); //#endregion ////插入空行 //PdfPCell cellInfoKong3 = new PdfPCell(); //cellInfoKong3.AddElement(new Phrase("", fontHeadInfo)); //cellInfoKong3.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfoKong3.Colspan = 12; //cellInfoKong3.BorderColor = new Color(255, 255, 255); //tableTitle.AddCell(cellInfoKong3); ////表名 //PdfPCell cellTitle11 = new PdfPCell(); //cellTitle11.AddElement(new Phrase(txtheader, fontbz)); //cellTitle11.VerticalAlignment = Element.ALIGN_LEFT; //cellTitle11.Colspan = 12; //cellTitle11.Border = Rectangle.TOP_BORDER; //tableTitle.AddCell(cellTitle11); ////插入空行 //PdfPCell cellInfoKong2 = new PdfPCell(); //cellInfoKong2.AddElement(new Phrase("", fontHeadInfo)); //cellInfoKong2.VerticalAlignment = Element.ALIGN_RIGHT; //cellInfoKong2.BorderColor = new Color(255, 255, 255); //cellInfoKong2.Colspan = 12; //tableTitle.AddCell(cellInfoKong2); #endregion #region 添加数据 //添加数据标题 //for (int j = 0; j < ds.Tables[0].Columns.Count; j++) //{ // PdfPCell cellTitle = new PdfPCell(); // cellTitle.AddElement(new Phrase(ds.Tables[0].Columns[j].ColumnName, fontbz)); // cellTitle.Colspan = 4; // cellTitle.BackgroundColor = new Color(220, 220, 220); // tableTitle.AddCell(cellTitle); //} PdfPCell cellTitlet1 = new PdfPCell(); cellTitlet1.AddElement(new Phrase("设备编号", fontbz)); cellTitlet1.Colspan = 4; cellTitlet1.BackgroundColor = new Color(220, 220, 220); tableTitle.AddCell(cellTitlet1); PdfPCell cellTitlet2 = new PdfPCell(); cellTitlet2.AddElement(new Phrase("加工参数名称", fontbz)); cellTitlet2.Colspan = 4; cellTitlet2.BackgroundColor = new Color(220, 220, 220); tableTitle.AddCell(cellTitlet2); PdfPCell cellTitlet3 = new PdfPCell(); cellTitlet3.AddElement(new Phrase("加工参数值", fontbz)); cellTitlet3.Colspan = 4; cellTitlet3.BackgroundColor = new Color(220, 220, 220); tableTitle.AddCell(cellTitlet3); #region 添加数据 // 添加数据 string Str = ""; string[] StrSplit; //遍历原gridview的数据行 foreach (var dt in ds) { PdfPCell cellData1 = new PdfPCell(); cellData1.AddElement(new Phrase(dt.MACHINECODDE, fontChinese)); cellData1.Colspan = 4; cellData1.BackgroundColor = new Color(239, 239, 239); tableTitle.AddCell(cellData1); PdfPCell cellData2 = new PdfPCell(); cellData2.AddElement(new Phrase(dt.PARADESCRIBE, fontChinese)); cellData2.Colspan = 4; cellData2.BackgroundColor = new Color(239, 239, 239); tableTitle.AddCell(cellData2); PdfPCell cellData3 = new PdfPCell(); cellData3.AddElement(new Phrase(dt.MEMO, fontChinese)); cellData3.Colspan = 4; cellData3.BackgroundColor = new Color(239, 239, 239); tableTitle.AddCell(cellData3); } #endregion PdfPCell cellfoot = new PdfPCell(); cellfoot.AddElement(new Phrase("", fontHeadInfo)); cellfoot.Colspan = 12; cellfoot.Border = Rectangle.TOP_BORDER; tableTitle.AddCell(cellfoot); document.Add(tableTitle); #endregion //关闭Document document.Close(); } catch (Exception ex) { throw ex; } } #endregion } }