public override void Create(object sender, EventArgs e) { List <string> file = new List <string>(); IList <string> stringList = this.VerbaliSelezionati(); BaseVerbale baseVerbale = new BaseVerbale(); if (this.ViewState["idverbale"] != null && stringList.Count > 0) { int num = int.Parse(this.ViewState["idverbale"].ToString()); string item = string.Empty; using (new ComandoEntities()) { using (IEnumerator <string> enumerator = stringList.GetEnumerator()) { while (enumerator.MoveNext()) { item = Helper.FillDocument(enumerator.Current, this.CreaDettaglio((long)num), null); file.Add(item); } } Helper.DownloadFile(this, file, base.GetType()); return; } } this.Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>ShowVerbali()</script>"); }
public static string FillDocument(string path, BaseVerbale b, Application word) { string str = "Verbale_" + DateTime.Now.Ticks.ToString(); b.directory = DateTime.Now.ToString("dd-MM-yyyy") + "//"; string destFileName = ConfigurationManager.AppSettings["PathNuovi"] + b.directory + str + ".doc"; if (!Directory.Exists(ConfigurationManager.AppSettings["PathNuovi"] + b.directory)) { Directory.CreateDirectory(ConfigurationManager.AppSettings["PathNuovi"] + b.directory); } File.Copy(Directory.GetFiles(ConfigurationManager.AppSettings["PathTemplates"], path, SearchOption.AllDirectories).FirstOrDefault <string>(), destFileName, true); //word.DisplayAlerts = WdAlertLevel.wdAlertsNone; //Document document = (Document) Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00020906-0000-0000-C000-000000000046"))); //object fileName = destFileName; //document = word.Documents.Open(fileName); //document.Activate(); //var enumerator = document.FormFields.GetEnumerator(); // using (enumerator) { //while (enumerator.MoveNext()) //{ // FormField field = (FormField) enumerator.Current; // Console.WriteLine(field.Name); // if (b.Fields.Any<KeyValuePair<string, string>>(x => x.Key == field.Name)) // { // field.Range.Text = (from x in b.Fields // where x.Key == field.Name // select x.Value).FirstOrDefault<string>(); // } //} } string fileName = destFileName; var document = File.ReadAllText(fileName); var regExPatter = @"(?<=<%)(.*)(?=%>)"; Regex r = new Regex(regExPatter, RegexOptions.IgnoreCase); // Match the regular expression pattern against a text string. var newValue = string.Empty; foreach (Match ItemMatch in r.Matches(document)) { if (b.Fields.Any <KeyValuePair <string, string> >(x => x.Key == ItemMatch.Value)) { newValue = (from x in b.Fields where x.Key == ItemMatch.Value select x.Value).FirstOrDefault <string>(); } document = document.Replace("<%" + ItemMatch.Value + "%>", newValue); } (ConfigurationManager.AppSettings["PathNuovi"] + "VerbaleNuovo.doc").Replace("VerbaleNuovo", "VerbaleNuovo" + DateTime.Now.Ticks); object obj17 = destFileName; File.WriteAllText(fileName, document); //document.Close(); return(destFileName); }