Load text file into table - Documents for Word .NET

Posted by: remus.iftimie on 10 July 2020, 8:25 am EST

    • Post Options:
    • Link

    Posted 10 July 2020, 8:25 am EST

    Load a text file that have values separated by # character (similar to a CSV file) into a table. The text file contains about 50 columns and I want to load only 7 columns from it into the new table (that will have only 7 columns). And to save the output as a .docx file.

  • Posted 12 July 2020, 11:43 pm EST

    Hi Remus,

    You can use the code snippet given below to save the data from the text file into the array so that you save load it into the table:

    String input = File.ReadAllText(@"Data.txt");
    int i = 0, j = 0;
    var rows = input.Split('\n');
    string[,] result = new string[rows.Count(), 7];
    foreach (var row in rows)
     {
      j = 0;
      foreach (var col in row.Trim().Split('#'))
        {
          if (j < 7)
           {   
            result[i, j] = col.Trim(new char[] { '"'});
            j++;
           }
         else
           {
             break;
           }
        }
       i++;
     }
    

    Please go through the following link to learn how to create the table in the word file via code:

    https://www.grapecity.com/documents-api-word/docs/online/table.html

    If you need any other help, please let us know.

    Regards,

    Prabhat Sharma.

  • Posted 13 July 2020, 11:55 am EST

    Thank you for the solution !

  • Posted 13 July 2020, 11:56 am EST

    Thank you for the solution !

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels