Use Edraw Office Viewer Component or Word Viewer Component to get the Document interface, then you can do it by the office automation.
document.OA1.CreateNew(“Word.Document”);
var objWord = document.OA1.ActiveDocument;
var range = objWord.Range(0,0);
var WTable = objWord.Tables.Add(range, 3,3);
WTable.Cell(1,1).Range.Font.Name = “Times New Roman”;
WTable.Cell(1,1).Range.Text = “examples 1”;
WTable.Cell(1,2).Range.Font.Size = 18;
WTable.Cell(1,2).Range.Bold = true;
WTable.Cell(1,2).Range.Font.Italic = true;
WTable.Cell(1,2).Range.Text = “examples 2”;
WTable.Cell(2,1).Range.ParagraphFormat.Alignment = 1; // 0= Left, 1=Center, 2=Right
WTable.Cell(2,1).Range.Font.Name = “Arial”;
WTable.Cell(2,1).Range.Font.Size = 12;
WTable.Cell(2,1).Range.Bold = false;
WTable.Cell(2,1).Range.ParagraphFormat.Alignment = 2;
WTable.Cell(3,3).Range.Font.Name = “Times New Roman”;
WTable.Cell(3,3).Range.Font.Size = 14;
WTable.Cell(3,3).Range.Bold = true;
WTable.Cell(3,3).Range.Font.Underline = true;
WTable.Cell(3,3).Range.ParagraphFormat.Alignment = 0;
WTable.Cell(3,2).Range.Text = “examples 3”
One Response to “How to insert HTML table into Word doc using Javascript?”
Leave a Reply
You must be logged in to post a comment.
June 5th, 2009 at 7:21 am
To change the font color:
WTable.Cell(1,2).Range.Font.Color = 5287936;
Change the border style and border width:
document.OA1.Open(“d:\\test.doc”);
var objWord = document.OA1.ActiveDocument;
var app = document.OA1.Application;
app.Selection.GoTo(3,1,3,””);
var range = app.Selection.Range;//objWord.Range(0,0);
var WTable = objWord.Tables.Add(range, 3,3);
WTable.Cell(1,1).Range.Font.Name = “Times New Roman”;
WTable.Cell(1,1).Range.Text = “ThaiCreate.Com 1”;
WTable.Cell(1,2).Range.Font.Size = 18;
WTable.Cell(1,2).Range.Bold = true;
WTable.Cell(1,2).Range.Font.Italic = true;
WTable.Cell(1,2).Range.Font.Color = 5287936;
WTable.Cell(1,2).Range.Text = “ThaiCreate.Com 2”;
WTable.Cell(2,1).Range.ParagraphFormat.Alignment = 1; // 0= Left, 1=Center, 2=Right
WTable.Cell(2,1).Range.Font.Name = “Arial”;
WTable.Cell(2,1).Range.Font.Size = 12;
WTable.Cell(2,1).Range.Bold = false;
WTable.Cell(2,1).Range.ParagraphFormat.Alignment = 2;
WTable.Cell(3,3).Range.Font.Name = “Times New Roman”;
WTable.Cell(3,3).Range.Font.Size = 14;
WTable.Cell(3,3).Range.Bold = true;
WTable.Cell(3,3).Range.Font.Underline = true;
WTable.Cell(3,3).Range.ParagraphFormat.Alignment = 0;
WTable.Cell(3,2).Range.Text = “ThaiCreate.Com 3”;
app.Options.DefaultBorderLineStyle = 1;
app.Options.DefaultBorderLineWidth = 8;
WTable.Borders(-1).LineStyle = 1;
WTable.Borders(-2).LineStyle = 1;
WTable.Borders(-4).LineStyle = 1;
WTable.Borders(-6).LineStyle = 1;
WTable.Borders(-3).LineStyle = 1;
WTable.Borders(-5).LineStyle = 1;
WTable.Borders(-5).Color = 5287936;