In this post we will learn “How to Create Word Document Using C#”. Most of the time while working on project we need this, and I hope it will really help you. You can follow below instructions to create word doc using C# code.
For more such topics you can Search or Visit our C# Section , IIS Section & SQL Section too.
Important steps:
- As perquisite you must have installed MS office on your machine.
- Create a Console or Windows application and place a given code in it.
- Add a reference for “Microsoft.Office.Interop.Word“.
- You can find above DLL at location “C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll”
Code Sample:
//parm Header - it will write text with H1 tag in doc
//parm WholeData - it is the data which you want to write in doc file after writing header or title.
private void CreateDocFiles( string header,string WholeData)
{
try
{
//Create an instance for word app
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
//Set animation status for word application
winword.ShowAnimation = false;
//Set status for word application is to be visible or not.
winword.Visible = false;
//Create a missing variable for missing value
object missing = System.Reflection.Missing.Value;
//Create a new document
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
s //Add paragraph with Heading 1 style
Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
object styleHeading1 = "Heading 1";
para1.Range.set_Style(ref styleHeading1);
para1.Range.Text = header; // "Para 1 header text";
para1.Range.InsertParagraphAfter();
//document.Content.Text = Environment.NewLine + Environment.NewLine; // add new lines after writing header
//Add paragraph with Heading 2 style
Microsoft.Office.Interop.Word.Paragraph para2 = document.Content.Paragraphs.Add(ref missing);
para2.Range.Font.Size = 14;
para2.Range.Text = WholeData; // writing body of doc file
para2.Range.InsertParagraphAfter();
//Create a 1X1 table and insert some dummy record
Table firstTable = document.Tables.Add(para1.Range, 2, 1, ref missing, ref missing);
firstTable.Borders.Enable = 1;
foreach (Row row in firstTable.Rows)
{
foreach (Cell cell in row.Cells)
{
//Header row
if (cell.RowIndex == 1)
{
cell.Range.Text = "CodeConfig.in Sample Code" + cell.ColumnIndex.ToString();
cell.Range.Font.Bold = 1;
//other format properties goes here
cell.Range.Font.Name = "verdana";
cell.Range.Font.Size = 10;
//cell.Range.Font.ColorIndex = WdColorIndex.wdGray25;
cell.Shading.BackgroundPatternColor = WdColor.wdColorGray25;
//Center alignment for the Header cells
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}
//Data row
else
{
//cell.Range.Text = (cell.RowIndex - 2 + cell.ColumnIndex).ToString();
cell.Range.Text = "SQL or C# code sample will appear here";
}
}
}
//Save the document
//removing special chars which are not supported in file name
header = header.Replace(",", " ").Replace("-", " ").Replace("\\", " ").Replace("/", " ").Replace(":", " ").Replace("*", " ").Replace("?", " ").Replace("\"", " ").Replace("<", " ").Replace(">", " ").Replace("|", " ");
object filename = OutPutPath + "\\" + header + ".docx";
document.SaveAs2(ref filename);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
winword = null;
//MessageBox.Show("Document created successfully !");
}
catch (Exception ex)
{
string errormsg = ex.Message;
}
}
After executing this code, it will create the doc file with required content and save it at the mentioned location. File name will be whatever you pass in Header variable.
You can check more on Microsoft Learn and for more such topics you can Search or Visit our C# Section too.
Comments are closed.