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?
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;
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.
arnabdt
Member
40 Points
16 Posts
Export DataTable To MS Word in C#
Jan 30, 2007 03:40 PM|LINK
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
arnabdt
Member
40 Points
16 Posts
Re: Export DataTable To MS Word in C#
Jan 30, 2007 06:09 PM|LINK
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 TEXTObject 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 TABLEWord.Table tbl = wd.Tables.Add
(rng,1, 5,
ref missing, ref missing); //END ADD TABLE //SET HEADERSetHeadings(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 HEADERarnabdt
Member
40 Points
16 Posts
Re: Export DataTable To MS Word in C#
Jan 30, 2007 06:09 PM|LINK
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 TEXTObject 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 TABLEWord.Table tbl = wd.Tables.Add
(rng,1, 5,
ref missing, ref missing); //END ADD TABLE //SET HEADERSetHeadings(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 HEADERajay_s
Member
2 Points
2 Posts
Re: Export DataTable To MS Word in C#
Oct 09, 2007 07:04 AM|LINK
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
xibian
Member
6 Points
3 Posts
Do you have the Office 2007 PIA?
Jun 04, 2008 09:36 PM|LINK
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
Office 2007 PIA
JacquesThoma...
Member
87 Points
79 Posts
Re: Do you have the Office 2007 PIA?
Jun 04, 2008 10:47 PM|LINK
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)
xibian
Member
6 Points
3 Posts
Links
Jun 05, 2008 03:07 PM|LINK
Hi Jacques, review this links, for a full information about office's PIAs , i recommend the MSDN and CodeProject Websites.
http://msdn.microsoft.com/en-us/library/15s06t57.aspx
http://msdn.microsoft.com/en-us/library/kh3965hw.aspx
http://msdn.microsoft.com/en-us/library/76d2d007.aspx
http://msdn.microsoft.com/en-us/library/hy7c6z9k.aspx
http://www.codeproject.com
http://www.codeproject.com/info/search.aspx?artkw=Office+PIA
http://blogs.msdn.com/artleo/archive/2006/03/24/560418.aspx
the last one is the ArtLeo's WebLog , very good.
Enjoy.[:D]
{diego toala}
PIA OFFICE LINKS MSDN CODEPROJECT
cioina
Contributor
3728 Points
878 Posts
Re: Export DataTable To MS Word in C#
Jun 05, 2008 04:20 PM|LINK
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
ramsivaa
Member
14 Points
35 Posts
Re: Export DataTable To MS Word in C#
Jun 24, 2008 05:16 AM|LINK
Hai...
pls tell me how to add header and footer in ms-word using C#.
thanxs..
manuiet
Member
14 Points
37 Posts
Adding Header and footer To MS Word in C#
Dec 21, 2010 06:13 AM|LINK
object oTrue = true; object oFalse = false; object oMissing = System.Reflection.Missing.Value; object novalue=System.Reflection.Missing.Value; object missing=System.Reflection.Missing.Value; object fileName = "normal.dot"; object newTemplate=false; object docType=0; object isVisible=true; Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document aDoc= WordApp.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible); WordApp.Visible = true; aDoc.Activate(); string logoPath = Server.MapPath("~//Images//logo.JPG"); Microsoft.Office.Interop.Word.Shape logoCustom = null; WordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader; logoCustom = WordApp.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); logoCustom.Select(ref oMissing); logoCustom.Name = "CustomLogo"; logoCustom.Left = -47; logoCustom.Top = -16; WordApp.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter; WordApp.ActiveWindow.Selection.Font.Name = "Verdana"; WordApp.ActiveWindow.Selection.Font.Size=8; Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage; WordApp.ActiveWindow.Selection.TypeText("For Logica Internal Use Only"); WordApp.ActiveWindow.Selection.TypeText(" "); WordApp.ActiveWindow.Selection.TypeText("Page "); WordApp.ActiveWindow.Selection.Fields.Add(WordApp.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing); WordApp.ActiveWindow.Selection.TypeText(" "); WordApp.ActiveWindow.Selection.TypeText(DateTime.Today.ToString("MM/dd/yyyy"));Regards,
Manu
<asp.net>