Export DataTable To MS Word in C#

Last post 06-24-2008 1:16 AM by ramsivaa. 8 replies.

Sort Posts:

  • Export DataTable To MS Word in C#

    01-30-2007, 11:40 AM
    • Member
      30 point Member
    • arnabdt
    • Member since 01-29-2007, 5:48 AM
    • India
    • Posts 16

    Hello All,

    I have a DataTable with data now I want to export those data to a MS Word document(Report) in a table.I figure out the following code....but can't able to add data to the word table.Is there anyone who can help me?

    Word.ApplicationClass wa =

    new Word.ApplicationClass();

    wa.Visible=

    true;

    Word.DocumentClass wd =

    new Word.DocumentClass();

    wd.Activate();

    Object start = Type.Missing;

    Object end = Type.Missing;

    Word.Range rng = wd.Range(

    ref start, ref end);

    Object defaultTableBehavior = Type.Missing;

    Object autoFitBehavior = Type.Missing;

    wd.Tables.Add(rng, 1, 3,

    ref defaultTableBehavior, ref autoFitBehavior);

    The above code only create a word table I how can I insert data from a datatable in this word table?

    Thanks in advance

  • Re: Export DataTable To MS Word in C#

    01-30-2007, 2:09 PM
    • Member
      30 point Member
    • arnabdt
    • Member since 01-29-2007, 5:48 AM
    • India
    • Posts 16

    I just figure out the issue and its working now....Here is the code

    private

    void button3_Click(object sender, System.EventArgs e)

    {

    Word.DocumentClass wd =

    new Word.DocumentClass();

    wd.Activate();

    //ADD TEXT

    //wd.Content.Text="<table></table>";

    //END ADD TEXT

    Object start = Type.Missing;

    Object end = Type.Missing;

    Word.Range rng = wd.Range(

    ref start, ref end);

    Object defaultTableBehavior = Type.Missing;

    Object autoFitBehavior = Type.Missing;

    object missing = System.Type.Missing;

    //ADD TABLE

    Word.Table tbl = wd.Tables.Add

    (rng,1, 5,

    ref missing, ref missing);

    //END ADD TABLE

    //SET HEADER

    SetHeadings(tbl.Cell(1, 1), "Permit Type");

    SetHeadings(tbl.Cell(1, 2), "Completeness Determination");

    SetHeadings(tbl.Cell(1, 3), "Draft Permit");

    SetHeadings(tbl.Cell(1, 4), "Final Decision");

    SetHeadings(tbl.Cell(1, 5), "Other Target");

    //END SET HEADER

    //ADD ROW

    for(int i=0;i<fds.Rows.Count;i++)

    {

    Word.Row newRow = wd.Tables[1].Rows.Add(

    ref missing);

    newRow.Range.Font.Bold = 0;

    newRow.Range.ParagraphFormat.Alignment =

    Word.WdParagraphAlignment.wdAlignParagraphLeft;

     

    newRow.Cells[1].Range.Text = fds.Rows[i][0].ToString();

    newRow.Cells[2].Range.Text = fds.Rows[i][1].ToString();

    newRow.Cells[3].Range.Text = fds.Rows[i][2].ToString();

    newRow.Cells[4].Range.Text = fds.Rows[i][3].ToString();;

    newRow.Cells[5].Range.Text = fds.Rows[i][4].ToString();

    }

     

    //END ROW

     

     

    }

    //ADD HEADER

    static void SetHeadings(Word.Cell tblCell, string text)

    {

    tblCell.Range.Text = text;

    tblCell.Range.Font.Bold = 1;

    tblCell.Range.ParagraphFormat.Alignment =

    Word.WdParagraphAlignment.wdAlignParagraphCenter;

    }

    //ADD HEADER
  • Re: Export DataTable To MS Word in C#

    01-30-2007, 2:09 PM
    • Member
      30 point Member
    • arnabdt
    • Member since 01-29-2007, 5:48 AM
    • India
    • Posts 16

    I just figure out the issue and its working now....Here is the code

    private

    void button3_Click(object sender, System.EventArgs e)

    {

    Word.DocumentClass wd =

    new Word.DocumentClass();

    wd.Activate();

    //ADD TEXT

    //wd.Content.Text="<table></table>";

    //END ADD TEXT

    Object start = Type.Missing;

    Object end = Type.Missing;

    Word.Range rng = wd.Range(

    ref start, ref end);

    Object defaultTableBehavior = Type.Missing;

    Object autoFitBehavior = Type.Missing;

    object missing = System.Type.Missing;

    //ADD TABLE

    Word.Table tbl = wd.Tables.Add

    (rng,1, 5,

    ref missing, ref missing);

    //END ADD TABLE

    //SET HEADER

    SetHeadings(tbl.Cell(1, 1), "Permit Type");

    SetHeadings(tbl.Cell(1, 2), "Completeness Determination");

    SetHeadings(tbl.Cell(1, 3), "Draft Permit");

    SetHeadings(tbl.Cell(1, 4), "Final Decision");

    SetHeadings(tbl.Cell(1, 5), "Other Target");

    //END SET HEADER

    //ADD ROW

    for(int i=0;i<fds.Rows.Count;i++)

    {

    Word.Row newRow = wd.Tables[1].Rows.Add(

    ref missing);

    newRow.Range.Font.Bold = 0;

    newRow.Range.ParagraphFormat.Alignment =

    Word.WdParagraphAlignment.wdAlignParagraphLeft;

     

    newRow.Cells[1].Range.Text = fds.Rows[i][0].ToString();

    newRow.Cells[2].Range.Text = fds.Rows[i][1].ToString();

    newRow.Cells[3].Range.Text = fds.Rows[i][2].ToString();

    newRow.Cells[4].Range.Text = fds.Rows[i][3].ToString();;

    newRow.Cells[5].Range.Text = fds.Rows[i][4].ToString();

    }

     

    //END ROW

     

     

    }

    //ADD HEADER

    static void SetHeadings(Word.Cell tblCell, string text)

    {

    tblCell.Range.Text = text;

    tblCell.Range.Font.Bold = 1;

    tblCell.Range.ParagraphFormat.Alignment =

    Word.WdParagraphAlignment.wdAlignParagraphCenter;

    }

    //ADD HEADER
  • Re: Export DataTable To MS Word in C#

    10-09-2007, 3:04 AM
    • Member
      2 point Member
    • ajay_s
    • Member since 05-28-2007, 9:59 AM
    • Posts 2

    hi All Iam try this code but gettting error at

    Dim wd As New Word.DocumentClass()

    error is -- Word.DocumentClass() is not defined.

    Is there any dll t be included or wat is the sol's

    enviornment ---Asp.net, vb.net

    reply soon

    with regards

    ajay

  • Do you have the Office 2007 PIA?

    06-04-2008, 5:36 PM
    • Member
      6 point Member
    • xibian
    • Member since 06-04-2008, 9:17 PM
    • USA / Ecuador
    • Posts 3

    Hi, My name is Diego Toala from Ecuador,South America , if you wanna use the Word.DocumentClass() you need to add a reference to the MSOffice 2007 PIA (Primary Interop Assembly) in your project , you can find all the office reference in the Redistributable MSOffice 2007 PIA installer package and you can download the installer package from here http://www.microsoft.com/downloads/details.aspx?FamilyID=59daebaa-bed4-4282-a28c-b864d8bfa513&displaylang=en , install the package and you get the Microsoft.Office.Interop.Word assembly...

     Open the Visual Studio 2005, Open your Project , then right click in the "References" folder, Choose "Add Reference", Choose the "CoM" tab, then choose the "Microsoft Office 12 Object Library" option,,, now you can use all the word interop classes in the Namespace Microsoft.Office.Interop.Word .

    Check the assembly, type  Microsoft.Office.Interop.Word.DocumentCl... and the intellisense make it all for you.

     Here is a code sample:

    Microsoft.Office.Interop.Word.ApplicationClass wa = new Microsoft.Office.Interop.Word.ApplicationClass();

    wa.Visible = true;Microsoft.Office.Interop.Word.DocumentClass wd = new Microsoft.Office.Interop.Word.DocumentClass();

    wd.Activate();

    Object start = Type.Missing;

    Object end = Type.Missing;

    Microsoft.Office.Interop.Word.Range wr = wd.Range(ref start, ref end);

    Object defaultTableBehavior = Type.Missing;

    Object autoFitBehavior = Type.Missing;

    Object missing = Type.Missing;

    Microsoft.Office.Interop.Word.Table wtable= wd.Tables.Add(wr, 1,1,ref missing, ref missing);

    SetHeadings(wtable.Cell(1, 0), "Ejemplo - Example");

     

    put in the load method of your form, see you, i hope this little articule , fits your needs.

    Diego Toala

     

    Filed under:
  • Re: Do you have the Office 2007 PIA?

    06-04-2008, 6:47 PM
    • Member
      87 point Member
    • JacquesThomas
    • Member since 05-23-2007, 7:09 AM
    • South Africa
    • Posts 75

    Does anyone perhaps know of good resources to start using PIA? I want to generate Word documents that clients can save.

    Also would the web server that hosts the website need to have PIA's installed or could i copy the dll to my bin folder instead of GAC?

    ----------------------------------------
    I never saved anything for the swim back.
    quote: Gattaca (1997)
  • Links

    06-05-2008, 11:07 AM
    • Member
      6 point Member
    • xibian
    • Member since 06-04-2008, 9:17 PM
    • USA / Ecuador
    • Posts 3
  • Re: Export DataTable To MS Word in C#

    06-05-2008, 12:20 PM
    • Contributor
      2,956 point Contributor
    • cioina
    • Member since 06-02-2008, 1:00 PM
    • Posts 649

    arnabdt:

    Hello All,

    I have a DataTable with data now I want to export those data to a MS Word document(Report) in a table.I figure out the following code....but can't able to add data to the word table.Is there anyone who can help me?

    Word.ApplicationClass wa =

    new Word.ApplicationClass();

    wa.Visible=

    true;

    Word.DocumentClass wd =

    new Word.DocumentClass();

    wd.Activate();

    Object start = Type.Missing;

    Object end = Type.Missing;

    Word.Range rng = wd.Range(

    ref start, ref end);

    Object defaultTableBehavior = Type.Missing;

    Object autoFitBehavior = Type.Missing;

    wd.Tables.Add(rng, 1, 3,

     

    ref defaultTableBehavior, ref autoFitBehavior);

    The above code only create a word table I how can I insert data from a datatable in this word table?

    Thanks in advance

    Please remark that Microsoft does not recoment to use ActiveX/OLE controls of Word /Excel/e.t.c.  on servers.

    Alexei Cioina.

    http://www.californiadreamhomesandland.com

    Alexei Cioina
  • Re: Export DataTable To MS Word in C#

    06-24-2008, 1:16 AM
    • Member
      14 point Member
    • ramsivaa
    • Member since 05-13-2008, 7:45 AM
    • Posts 35

     Hai...

    pls tell me how to add header and footer in ms-word using C#.

    thanxs..

     

Page 1 of 1 (9 items)