Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I am trying to create multiple .docx files (base on the number of rows in gridview) using 1 template only. It goes through but I am encountering a message saying that "Duplicate Headers received from server"
Here is my code:

C#
TextInfo txtInfo = new CultureInfo("en-US", false).TextInfo;
        string sourceFile = Server.MapPath("~/BankInstruction_CoveringLetter/BankInstruction_CoveringLetter.docx");
            foreach (GridViewRow gvRow in gvBankExport.Rows)
            {
                Label lblBankCode = (Label)gvRow.FindControl("lblBankCode");
                Label lblBankTotal = (Label)gvRow.FindControl("lblBankTotal");
                TextBox txtBankName = (TextBox)gvRow.FindControl("txtBankName");
                TextBox txtAddress = (TextBox)gvRow.FindControl("txtAddress");
                TextBox txtAcctNo = (TextBox)gvRow.FindControl("txtAcctNo");
                TextBox txtCurrencyCode = (TextBox)gvRow.FindControl("txtCurrencyCode");
                TextBox txtOfficerTitle = (TextBox)gvRow.FindControl("txtOfficerTitle");
                TextBox txtFirstName = (TextBox)gvRow.FindControl("txtFirstName");
                TextBox txtLastName = (TextBox)gvRow.FindControl("txtLastName");
                TextBox txtPosition = (TextBox)gvRow.FindControl("txtPosition");
                string signatory1 = drpSignatory1.SelectedItem.Text.Split('|')[0].ToString();
                string signatory1Pos = drpSignatory1.SelectedItem.Text.Split('|')[1].ToString();
                string signatory2 = drpSignatory2.SelectedItem.Text.Split('|')[0].ToString();
                string signatory2Pos = drpSignatory2.SelectedItem.Text.Split('|')[1].ToString();
                string bankcode = lblBankCode.Text;
                string bankname = txtBankName.Text;
                string address = txtAddress.Text;
                string acctno = txtAcctNo.Text;
                string curr = txtCurrencyCode.Text;
                string offtitle = txtOfficerTitle.Text;
                string lastname = txtLastName.Text;
                string firstname = txtFirstName.Text;
                string offposition = txtPosition.Text;
                string banktotal = lblBankTotal.Text;

                string destinationFile = Server.MapPath("~/BankInstruction_CoveringLetter/") + bankcode + "_BankInsCoverLetter" + DateTime.Now.ToString("yyyyMMdd") + ".docx";
                File.Copy(sourceFile, destinationFile, true);
                Dictionary<string, string> keyValues = new Dictionary<string, string>();
                string a = Convert.ToDecimal(Math.Truncate(Convert.ToDouble(banktotal))).ToString();
                string decimalPart = (Convert.ToDecimal(banktotal) - Math.Floor(Convert.ToDecimal(banktotal))).ToString().Replace("0.", "");
                string numberWordValue = txtInfo.ToTitleCase(NumberToWords(Convert.ToInt32(a)).Replace("-", " "));
                keyValues.Add("BankInstructionDate", DateTime.Now.ToString("MMMM dd, yyyy"));
                keyValues.Add("BANKFULLNAME", txtInfo.ToUpper(bankname));
                keyValues.Add("BANKFULLADDRESS", address);
                keyValues.Add("BankFullContactPerson", firstname + " " + lastname);
                keyValues.Add("BankFullContactPerPosition", offposition);
                keyValues.Add("BankOfficerTitle", offtitle);
                keyValues.Add("BankLastNameContactPerson", lastname);
                keyValues.Add("BANKFULLACCOUNTNO", acctno);
                keyValues.Add("TOTALAMOUNTINWORDS", numberWordValue);
                keyValues.Add("NUMBERCENTS", decimalPart);
                keyValues.Add("CURRENCYCODE", curr);
                keyValues.Add("TOTALAMOUNTINDIGITS", banktotal);
                keyValues.Add("Signatory1", signatory1);
                keyValues.Add("Sign1Position", signatory1Pos);
                keyValues.Add("Signatory2", signatory2);
                keyValues.Add("Sign3Positionsss", signatory2Pos); //NAGERROR KASI UNG ORIGINAL NA REPLACEMENT WORD, PERO ETO GUMANA, NOT SURE KUNG ANONG KAARTEHAN ULIT NG C#
                SearchAndReplace(destinationFile, keyValues);
                Process.Start(destinationFile);

                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(destinationFile));
                Response.WriteFile(destinationFile);

            }
            Response.End();


C#
public void SearchAndReplace(string document, Dictionary<string,string> toReplace)
   {
       using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
       {
           string docText = null;
           using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
           {
               docText = sr.ReadToEnd();
           }

           foreach (KeyValuePair<string, string> item in toReplace)
           {
               Regex regexText = new Regex(item.Key);
               docText = regexText.Replace(docText, item.Value);
           }

           using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
           {
               sw.Write(docText);

           }
       }

   }
Posted
Updated 14-Jan-16 16:43pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900