Code mẫu

1. Common const public static class TemplateDicisionKeyword { //const for template VietNam public const string Company_Name_Vn_Capital = "#Company_Name_Vn_Capital#"; public const string day = "#day#"; public const string month = "#month#"; public const string year = "#year#"; public const string Company_Name_Vn = "#Company_Name_Vn#"; public const string Company_Certificate_No = "#Company_Certificate_No#"; public const string Company_Certificate_Issuer_Vn = "#Company_Certificate_Issuer_Vn#"; public const string Company_CertificateDate = "#Company_CertificateDate#"; public const string Title_Vn = "#Title_Vn#"; public const string Traveller_name = "#Traveller_name#"; public const string Country = "#Country#"; public const string TravelStartDate = "#TravelStartDate#"; public const string TravelEndDate = "#TravelEndDate#"; //const for template English public const string Date_EN = "#Date_EN#"; public const string Company_Name_En_Capital = "#Company_Name_En_Capital#"; public const string Destination_Country = "#Destination_Country#"; public const string Company_Name_En = "#Company_Name_En#"; public const string Company_CertificateDate_EN = "#Company_CertificateDate_EN#"; public const string Company_Certificate_Issuer_En = "#Company_Certificate_Issuer_En#"; public const string title = "#title#"; public const string Travel_Start_Date = "#Travel_Start_Date#"; public const string Travel_End_Date = "#Travel_End_Date#"; } 2. Replace public static string ExportDecision(string langDicision, int idTrip, string traveller) { string htmlBody = ""; PersonalInformationEntity employeeInfo = ServiceOnsiteController.GetEmployeeInfo(traveller); System.Data.SqlClient.SqlParameter[] param = new System.Data.SqlClient.SqlParameter[2]; param[0] = new System.Data.SqlClient.SqlParameter("@idTrip", idTrip); param[1] = new System.Data.SqlClient.SqlParameter("@LegalCode", employeeInfo.LegalCompany); DataSet dtDecisionInfo = SQLProvider.SelectMultiDataSet("Proc_GetLegalInfoDecision", param); DataTable dtLegalInfo = dtDecisionInfo.Tables[0]; DataTable dtTripInfo = dtDecisionInfo.Tables[1]; //get country System.Data.SqlClient.SqlParameter paramKey = new System.Data.SqlClient.SqlParameter(); paramKey = new System.Data.SqlClient.SqlParameter("@key", dtTripInfo.Rows[0][2].ToString()); DataTable dtCoutry = SQLProvider.Select("GetCountryByKey", paramKey); if (langDicision.Equals("EN")) { htmlBody = TravelController.GetTemplateContentByKeyword(Constants.TemplateKeyword.DecisionTemplateEN); //replace template EN //replace conpany name htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Name_En_Capital, dtLegalInfo.Rows[0][3].ToString().ToUpper()); //replace date create Dicicsion DateTime dateNow = DateTime.Now; htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Date_EN, String.Format("{0: MMMM d, yyyy}", dateNow)); //replace legal htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Name_En, dtLegalInfo.Rows[0][3].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Certificate_No, dtLegalInfo.Rows[0][4].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Certificate_Issuer_En, dtLegalInfo.Rows[0][5].ToString()); DateTime CertificateDate = Convert.ToDateTime(dtLegalInfo.Rows[0][6].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_CertificateDate_EN, String.Format("{0: d MMMM yyyy}", CertificateDate)); //view name of employee if (employeeInfo.Gender == "Male") { htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.title, "Mr. "); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Traveller_name, employeeInfo.FullName); } else if (employeeInfo.Gender == "Female") { htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.title, "Ms "); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Traveller_name, employeeInfo.FullName); } else { htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.title, ""); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Traveller_name, employeeInfo.FullName); } //replace trip info DateTime StartDate = Convert.ToDateTime(dtTripInfo.Rows[0][0].ToString()); DateTime EndDate = Convert.ToDateTime(dtTripInfo.Rows[0][1].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Destination_Country, dtCoutry.Rows[0][0].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Travel_Start_Date, String.Format("{0: d MMMM yyyy}", StartDate)); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Travel_End_Date, String.Format("{0: d MMMM yyyy}", EndDate)); } else { htmlBody = TravelController.GetTemplateContentByKeyword(Constants.TemplateKeyword.DecisionTemplateVN); //replace template vietnam //replace conpany name htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Name_Vn_Capital, dtLegalInfo.Rows[0][2].ToString().ToUpper()); //replace date create Dicicsion DateTime dateNow = DateTime.Now; htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.day, dateNow.Day.ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.month, dateNow.Month.ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.year, dateNow.Year.ToString()); //replace legal htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Name_Vn, dtLegalInfo.Rows[0][2].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Certificate_No, dtLegalInfo.Rows[0][4].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_Certificate_Issuer_Vn, dtLegalInfo.Rows[0][5].ToString()); DateTime CertificateDate = Convert.ToDateTime(dtLegalInfo.Rows[0][6].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Company_CertificateDate, String.Format("{0:MM/dd/yyyy}", CertificateDate)); //view name of employee if (employeeInfo.Gender == "Male") { htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Title_Vn, "Ông "); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Traveller_name, employeeInfo.FullName); } else if (employeeInfo.Gender == "Female") { htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Title_Vn, "Bà "); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Traveller_name, employeeInfo.FullName); } else { htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Title_Vn, ""); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Traveller_name, employeeInfo.FullName); } //replace trip info DateTime StartDate = Convert.ToDateTime(dtTripInfo.Rows[0][0].ToString()); DateTime EndDate = Convert.ToDateTime(dtTripInfo.Rows[0][1].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.Country, dtCoutry.Rows[0][0].ToString()); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.TravelStartDate, String.Format("{0:MM/dd/yyyy}", StartDate)); htmlBody = htmlBody.Replace(Constants.TemplateDicisionKeyword.TravelEndDate, String.Format("{0:MM/dd/yyyy}", EndDate)); } HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.ContentType = "application/vnd.ms-word"; HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=ContentDocument.doc"); HttpContext.Current.Response.Write(htmlBody.ToString()); HttpContext.Current.Response.End(); HttpContext.Current.Response.Flush(); return ""; }