locked
replace text in Word when GridVeiw Dowload button clicked RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I am looking to replace text on a document that is opened from a GridView Download LinkButton. The Files listed in the GridView are stored in a folder on the server and accessed using the file path directory (as shown in the below code).

               if (!IsPostBack)
                {
                    string[] filePaths = Directory.GetFiles(Server.MapPath("~/Documents/Templates/"));
                    List<ListItem> files = new List<ListItem>();
                    foreach (string filePath in filePaths)
                    {
                        files.Add(new ListItem(Path.GetFileName(filePath), filePath));
                    }
                    GV.DataSource = files;
                    BV.DataBind();
                }

    Please see my code to download which runs when the "Download" button is clicked:

     protected void DownloadCrownfordForm(object sender, EventArgs e)
            {
                string filePath = (sender as LinkButton).CommandArgument;
                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
                Response.WriteFile(filePath);
                Response.End();
            }

    What I would like to do is when the Download LinkButton is clicked is to replace text in a Word documents (which will be "Ccc" for example)  with text populated within a textbox. inside my C# code page. Would this be possible?

    Many thanks in advance for any help, it is greatly appreciated.

    Jonny

    Tuesday, February 25, 2020 11:08 AM

Answers

All replies

  • User409696431 posted

    You are going to need to copy the document to a new instance, and edit it on the server to do a find/replace for your text, then serve that edited document to the user (and when done, delete that extra file).

    https://www.e-iceblue.com/Introduce/free-doc-component.html Spire.Doc for .Net  (Free).

    Available in Nuget: https://www.nuget.org/packages/FreeSpire.Doc/

    Instructions for document.Replace functionality: https://www.e-iceblue.com/Knowledgebase/Spire.Doc/Spire.Doc-Program-Guide/Text/NET-Word-Replace-String-in-Word-with-New-String-in-C-VB.NET.html

     protected void DownloadCrownfordForm(object sender, EventArgs e)
            {
                string filePath = (sender as LinkButton).CommandArgument;
    
    //Insert new code here to create a copy of filePath, call it filePath2, and edit it with Spire.Doc to replace your text
    
                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath2));
                Response.WriteFile(filePath2);
                Response.End();
    
    //Insert code here to clean up and remove the edited copy of the file at filePath2
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 25, 2020 9:05 PM
  • User-1901014284 posted

    Thank you very much Kathy, works perfectly!

    Kind regards

    Jonny

    Wednesday, February 26, 2020 3:06 PM
  • User-1901014284 posted

    Hi KathyW,

    Is there a way to use Spire.doc and Spire.xls within the same coding previously used? I have excel and word documents i would like to open. Would this be possible please?

    Kind regards

    Jonathan Lewis

    Friday, February 28, 2020 1:38 PM
  • User409696431 posted

    Try it.   You'd have to know which type of document you were using (check the file extension), put the approriate references for both Spire.Doc and Spire.Xls in your using statements, and use the appropriate commands to make whatever changes you want to make.  (E.g., for modifying data in an excel file, https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Find-and-Replace-Find-and-Replace-Data-in-Excel.html)

    Saturday, February 29, 2020 1:47 AM
  • User-1901014284 posted

    Thank you KathyW,

    When adding the using spire.xls; I receive the an error on the below line of code:

     document.SaveToFile("C:/Users/Administrator/source/repos/ClientPortal/ClientPortal/Documents/Crownford Template.docx", FileFormat.Docx);

    The error is on: 'FileFormat' is an ambiguous reference between 'Spire.Doc.FileFormat' and 'Spire.Xls.FileFormat'

    My apologies if this is a straight forward solution, I am new to Spire and still looking into this.

    Many thanks 

    Jonny

    Wednesday, March 4, 2020 10:35 AM
  • User409696431 posted

    This isn't specific to Spire.  When using any libraries that have the same reference names in them, you need to explicity specify what library you are using when you use the reference. 

    if  "'FileFormat' is an ambiguous reference between 'Spire.Doc.FileFormat' and 'Spire.Xls.FileFormat'", then use 'Spire.Doc.FileFormat' and 'Spire.Xls.FileFormat', depending on which one you are using, instead of FileFormat.

    Wednesday, March 4, 2020 2:22 PM
  • User-1901014284 posted

    Thank you again KathyW,

    I have been looking at using the Spire.Xls but can not seem to edit an existing document, it creates a new document everytime and adds the text to the correct cell but will not load a spreadsheet which already exists. Please see below my code:

                        workbook.LoadFromFile(@"C:\..\Environmental\Forms\" + Path.GetFileName(filePath), ExcelVersion.Version97to2003);
                        Worksheet worksheet = workbook.Worksheets[0];
    
                        try
                        {
                            CellRange range = worksheet.FindString("Ccc", false, false);
                            worksheet.Replace(range.Value, txtName.Text);
                        }
                        catch (System.Exception)
                        {
                            Console.WriteLine("The text doesn't exist the word you want to replace");
                        }
    
                        workbook.SaveToFile("Template.xls", ExcelVersion.Version97to2003);
                        System.Diagnostics.Process.Start(workbook.FileName);

    Any idea how I can change the cell in an existing excel document. The excel document is physically saved in the file path and opened on button click within a GridView.

    I have tried many other ways but always seems to be the same problem where a blank document is opened with the cell populated.

    Any help would be greatly appreciated.

    Kind regards

    Jonny
     

    Thursday, March 12, 2020 10:13 AM
  • User409696431 posted

    For details about using Spire, ask on their forum. https://www.e-iceblue.com/forum/

    Thursday, March 12, 2020 3:08 PM