using crystalreport generate PDF

DataTable dt = new DataTable();
                dt.TableName = "DataTable1";
                dt.Columns.Add("id");
                dt.Columns.Add("DESCRIPTION");
                dt.Columns.Add("QUANTITY");
                dt.Columns.Add("UNITPRICE");
                dt.Columns.Add("HandWorkPrice");

                DataTable dtOrder = new DataTable();
                dtOrder.TableName = "Order";
                dtOrder.Columns.Add("id");
                dtOrder.Columns.Add("ReceiveDate");
                dtOrder.Columns.Add("PRODUCT");
                dtOrder.Columns.Add("MachineSN");
                dtOrder.Columns.Add("TroubleDescription");
                dtOrder.Columns.Add("Description2");
                dtOrder.Columns.Add("Diagnose");
                dtOrder.Columns.Add("SUBTOTAL");
                dtOrder.Columns.Add("Inspection");
                dtOrder.Columns.Add("TOTAL");
                dtOrder.Columns.Add("To");
                dtOrder.Columns.Add("Date");
                dtOrder.Columns.Add("Telephone");
                dtOrder.Columns.Add("QuotationNo");
                dtOrder.Columns.Add("MOBILE");
                dtOrder.Columns.Add("ENGINEER");
                dtOrder.Columns.Add("Email");
                dtOrder.Columns.Add("SchoolAddr");
                dtOrder.Columns.Add("SchoolPhone");
                dtOrder.Columns.Add("SchoolEmail");

                DataSet ds = new DataSet();
                ds.Tables.Add(dtOrder);
                ds.Tables.Add(dt);
                DataRelation dataRelation = new DataRelation("rel1", dtOrder.Columns[0], dt.Columns["id"]);

                string parts = "";
                listOrder = SPContext.Current.Web.Lists[Helper.Global.ListNames.OrderList];
                SPList accessoryList = SPContext.Current.Web.Lists[Helper.Global.ListNames.ComponentList];
                SPListItem item = listOrder.GetItemById(orderid);

                string queryStr = ASPsolution.Helper.Global.QueryString.Component;
                queryStr = string.Format(queryStr, orderid.ToString());
                SPQuery query = new SPQuery();
                query.Query = queryStr;
                SPListItemCollection accessaryItem = accessoryList.GetItems(query);
                List<ASPsolution.BLL.Entity.ASPOrder> order = new List<ASPsolution.BLL.Entity.ASPOrder>();
                //decimal totalPrice=0;
                for (int i = 0; i < accessaryItem.Count; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = orderid;
                    dr["DESCRIPTION"] = accessaryItem[i]["Description"];
                    dr["QUANTITY"] = accessaryItem[i]["Count"];
                    //totalPrice=
                    dr["UNITPRICE"] = Helper.Formater.FormateCurrency(OrderProvider.ComputeComponentCustomerPrice(Convert.ToDecimal(accessaryItem[i]["Price"].ToString()), int.Parse(accessaryItem[i]["Count"].ToString()), Convert.ToDecimal(accessaryItem[i]["HandWorkPrice"].ToString())));
                    dr["HandWorkPrice"] = accessaryItem[i]["HandWorkPrice"];
                    dt.Rows.Add(dr);
                    parts = parts + "," + Convert.ToString(dr["Description"]);
                }
                if (accessaryItem.Count == 0)
                {
                    DataRow dr = dt.NewRow();
                    dr["id"] = orderid;
                    dr["DESCRIPTION"] = "";
                    dr["QUANTITY"] = "";
                    dr["UNITPRICE"] = "";
                    dr["HandWorkPrice"] = "";
                    dt.Rows.Add(dr);
                }

                if (parts.StartsWith(","))
                {
                    parts = parts.Substring(1);
                }
                if (parts.EndsWith(","))
                {
                    parts = parts.Substring(0, parts.Length - 1);
                }

                DataRow dataRow = dtOrder.NewRow();
                dataRow["id"] = orderid;
                
                dataRow["PRODUCT"] = item["model"];
                dataRow["MachineSN"] = item["MachineSN"];
                dataRow["TroubleDescription"] = item["TroubleDescription"];
                dataRow["Diagnose"] = item["Diagnose"];

                decimal checkFee = 0m, customerFee = 0m, subTotal = 0m;
                checkFee = Convert.ToDecimal(item["CheckFee"]);
                customerFee = Convert.ToDecimal(item["Custom_TotalPrice"]);
                subTotal = customerFee - checkFee;
                dataRow["SUBTOTAL"] = Helper.Formater.FormateCurrency(subTotal);
                dataRow["Inspection"] = Helper.Formater.FormateCurrency(checkFee);
                dataRow["TOTAL"] = Helper.Formater.FormateCurrency(customerFee);
                dataRow["Description2"] = parts;
                dataRow["To"] = item["CustomName"];
                try
                {
                    dataRow["ReceiveDate"] = Convert.ToDateTime(item["receiveDate"]).ToString(Helper.Global.DateFormatReport);
                    dataRow["Date"] = Convert.ToDateTime(item["CreatedDate"]).ToString(Helper.Global.DateFormatReport);
                }
                catch(Exception ex)
                {
                    Helper.Logger.Log("Helper.ReportHelper.cs CreatePDFReport", ex.Message);
                }
                dataRow["Telephone"] = item["phone"];
                dataRow["QuotationNo"] = item["QuotationID"];
                dataRow["MOBILE"] = item["mobile"];

                SPFieldUser lookUpUser = listOrder.Fields.GetField("UserName"as SPFieldUser;
                SPFieldUserValue userValue = lookUpUser.GetFieldValue(item["UserName"].ToString()) as SPFieldUserValue;
                string userName = userValue.User.Name;
            

                dataRow["ENGINEER"] = userName;// item["UserName"];/////////////////////
                dataRow["Email"] = item["Email"];

                listMaintenanceCenter = SPContext.Current.Web.Lists[Helper.Global.ListNames.MaintenanceCenter];
                SPFieldLookup lookUp = listOrder.Fields.GetField("MaintenanceCenter"as SPFieldLookup;
                SPFieldLookupValue lookUpValue = lookUp.GetFieldValue(item["MaintenanceCenter"].ToString()) as SPFieldLookupValue;
                int maintenanceCenterId = int.Parse(lookUpValue.LookupId.ToString());
                listMaintenanceCenter = SPContext.Current.Web.Lists[Helper.Global.ListNames.MaintenanceCenter];
                SPListItem maintenanceCenter = listMaintenanceCenter.GetItemById(maintenanceCenterId);


                dataRow["SchoolAddr"] = Convert.ToString(maintenanceCenter["Address"]);
                dataRow["SchoolPhone"] = Convert.ToString(maintenanceCenter["Phone"]);
                dataRow["SchoolEmail"] = Convert.ToString(maintenanceCenter["Email"]);

                dtOrder.Rows.Add(dataRow);

                string path = HttpContext.Current.Server.MapPath("/tmp/" + orderid + ".pdf");
                ReportDocument repDoc = new ReportDocument();
                string path1 = Helper.DocumentHelper.GetDocumentPhysicalPath("CrystalReport2.rpt");
                repDoc.Load(path1);                
                repDoc.SetDataSource(ds);
                repDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, path);
                System.Threading.Thread.Sleep(500);
                return path;
posted @ 2012-06-04 11:31  zyip  阅读(239)  评论(0编辑  收藏  举报