zoukankan      html  css  js  c++  java
  • C#实现打印(反射)

    前端:

    function printTask(obj) {
        var rowData = $("#tb").DataTable().row($(obj).parents('tr')[0]).data();
        var url = "/Material/PrintContract";
        var inputs = '<input type="hidden" name="conid" value="' + rowData.WengvNj + '" />';
        $('<form action="' + url + '" method="post" target="_blank">' + inputs + '</form>').appendTo('body').submit().remove();
    }
    

      后台:

     public FileResult PrintContract(string conid)
    {
      MemoryStream ms = new FileHelper().PrintContractNew(mesContract, 
                    detailsEntity,
                    pnEntity.FirstOrDefault(),
                    cuInfo, 
                    filiale,
                    cuInuser,
                    sysUser,
                    dtReturn);
                return File(ms, "application/pdf");
    }
    

      

    public MemoryStream PrintContractNew(MesContract mesContract,
                IList<MesContractDetails> mesContractDetails,
                MesPn pnEntity, 
                CuInfo cuInfo,
                DataFiliale filiale,
                IList<CuInuser> cuInusers,
                SysUser sysUser,
                DataTable dtReturn)
            {
                string filePath = "";
    
                //根据合同类型需要走两个分支,采购根据供应商调用不同的模板
                string contractType = mesContract.MesType;
                
                if (contractType == "采购")
                {
                    string supplierName = mesContract.MesCuid;
                    if (supplierName == "SUP501") //商飞公司
                        filePath = HttpContext.Current.Server.MapPath("~/Files/Modal/ShanFeiProcurementContract.docx");
                    else    //采购的其他公司
                        filePath = HttpContext.Current.Server.MapPath("~/Files/Modal/ProcurementContract.docx");
                }
                else  //送修和索赔合同
                    filePath = HttpContext.Current.Server.MapPath("~/Files/Modal/SendRepair.docx");
                 
                //判断文件是否存在
                if (!File.Exists(filePath)) return null;
                
                Document document = new Document(filePath);
    
    
                if (contractType == "采购")
                {
                    if (mesContractDetails != null || mesContractDetails.Count > 0)
                    {
                        //  TODO  代码瞎写,采购中每项都来自,自己的选项中,
                        foreach (var item in mesContractDetails)
                            item.MesCurrency = mesContract.MesCurrency;
    
                        PrintPurchse(document, mesContractDetails);
    
                        //TODO 给税率赋值,瞎写,这个税率等于总税率
                        mesContract.WengvJdjpjxrq = mesContractDetails[0].WengvZtpbBlfr;
                    }
                }
                else
                    PrintUnPurchse(document, mesContractDetails, dtReturn);
    
                string emergencyLevel = GetSortedDictionaryValue(mesContract.MesEmer, GetEmergencyLevelType());//获取紧急程度
                mesContract.WengvJslz = emergencyLevel;
    
                string tranSportType = GetSortedDictionaryValue(mesContract.MesTransport, GetTranSportType());//运输方式
                mesContract.WengvYxhvbzzdg = tranSportType;
    
                string payName = GetByEnglisConverToChine(dtPay, mesContract.MesPaymentmet, "BM_CODE", "BM_ABBR");//付款方式
                mesContract.WengvUgfunxeyrh = payName;
    
                string currencyName = GetByEnglisConverToChine(dtCurrency, mesContract.MesCurrency, "RMB_CODE", "RMB_CNAME"); //币种
                mesContract.WengvHayznxnk = currencyName;
    
                string moneyCapital = ConvertToChinese(mesContract.MesSum.ToString());//小写转大写
                mesContract.MesMoneyCapital = moneyCapital;
    
    
                ReKey(document.Range, mesContract); //合同信息
    
                ReKey(document.Range, cuInfo);//客户公司信息
                ReKey(document.Range, filiale);//打印甲方公司
    
                CuInuser cuInuser = cuInusers.FirstOrDefault();
                if (cuInuser == null)
                    cuInuser = new CuInuser();
                ReKey(document.Range, cuInuser);//客户公司的人员信息
    
                if (sysUser == null)
                    sysUser = new SysUser();
                
                ReKey(document.Range, sysUser);//用户信息
    
                return GetMeoryStreamByFileStream(filePath, document);
            }
    

      

    private static void ReKey(Range range, object model)
            {
                if (model == null) return;
                Type t = model.GetType();
                PropertyInfo[] pi = t.GetProperties();
                foreach (PropertyInfo p in pi)
                {
                    var v = p.GetValue(model, null);
                    string va = "";
                    if (v != null)
                    {
                        if (v.GetType() == typeof(DateTime) || v.GetType() == typeof(Nullable<DateTime>))
                        {
                            va = DateTime.Parse(v.ToString()).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            va = v.ToString();
                        }
                    }
                    FindReplaceOptions op = new FindReplaceOptions();
                    op.FindWholeWordsOnly = false;
    
                    range.Replace("$" + p.Name + "$", va, op);
                    //range.Replace("$" + p.Name + "$", va);
                }
            }
    

      

     private static MemoryStream GetMeoryStreamByFileStream(string filePath, Document doc)
            {
                if (!File.Exists(filePath)) return null;
                using (FileStream stream = File.OpenRead(filePath))
                {
                    if (stream == null) return null;
                    MemoryStream ms = new MemoryStream();
                    doc.Save(ms, SaveFormat.Pdf);
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    return ms;
                }
            }
    

      生成table:

    private Table PrintMesContractDetail(Document document, IList<MesContractDetails> mesContractDetails)
            {
                try
                {
                    Table tb = (Table)document.GetChild(NodeType.Table, 1, true);
                    int i = 1;
                    foreach (var item in mesContractDetails)
                    {
                        //序号、件号、名称、数量、单位、 状态、不含税单价、含税单价、含税总价、币种、交货期、证书、备注
                        string[] colunValues = new string[] {  //TODO 其他列用不用判断
                             i.ToString(),//序号
                             item.MesPno, //件号
                             item.MesProductname,//名称
                             item.MesCount, //数量
                             GetByEnglisConverToChine(dtUnit,item.MesUnit,"BM_CODE","BM_CNAME"),//单位
                             "NE",//状态
                             item.MesNottxaprice.ToString(),//不含税单价
                             item.MesTxaprice.ToString(),//含税单价
                             item.MesTxaprice.ToString(),//含税单价
                             GetByEnglisConverToChine(dtCurrency,item.MesCurrency,"RMB_CODE","RMB_CNAME"),//币种
                             item.MesDeliverydate.ToString(),//交货期
                             item.MesCertification= item.MesCertification==null?"":item.MesCertification//证书
                         };
                        i++;
                        var tr = CreateRow(12, colunValues, document);
                        tb.Rows.Add(tr);
                    }
                    return tb;
                }
                catch (Exception ex)
                {
                    LogWriter.Write(LOG_CATEGORY.WIN_UI, LOG_LEVEL.ERROR, "PrintMesContractDetail:" + ex.ToString());
                    return null;
                }
            }
    

      生成table:

    private static Row CreateRow(int columnCount, string[] columnValues, Document doc)
            {
                Row r2 = new Row(doc);
                for (int i = 0; i < columnCount; i++)
                {
                    if (columnValues.Length > i)
                    {
                        var cell = CreateCell(columnValues[i], doc);
                        r2.Cells.Add(cell);
                    }
                    else
                    {
                        var cell = CreateCell("", doc);
                        r2.Cells.Add(cell);
                    }
                }
                return r2;
            }
    

      把不需要的代码去掉就行.

  • 相关阅读:
    HDU-4609 3-idiots FFT
    HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
    HDU-1007 Quoit Design 平面最近点对
    POJ-3714 Raid 平面最近点对
    HDU-4631 Sad Love Story 平面最近点对
    HDU-4630 No Pain No Game 树状数组+离线操作
    HDU-4628 Pieces 搜索 | DP
    HDU-4627 The Unsolvable Problem 简单数学
    HDU-4638 Group 树状数组+离线
    HDU-4635 Strongly connected 强连通,缩点
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/12188127.html
Copyright © 2011-2022 走看看