private void microsoftWordToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string date1 = dateTimePicker1.Value.ToString("yyyy-MM-dd");
string date2 = dateTimePicker2.Value.ToString("yyyy-MM-dd");
string bg8 = date1;
string bg9 = date2;
Object oMissing = System.Reflection.Missing.Value;
OBJECTS OF FALSE AND TRUE
Object oTrue = true;
Object oFalse = false;
CREATING OBJECTS OF WORD AND DOCUMENT
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
MAKING THE APPLICATION VISIBLE
oWord.Visible = true;
ADDING A NEW DOCUMENT TO THE APPLICATION
oWordDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
LoadDataGridView();
Object start1 = 0;
Object end1 = 1;
Microsoft.Office.Interop.Word.Range rng1 = oWordDoc.Range(ref start1, ref end1);
rng1.InsertAfter(" Date Range: " + bg8 + " to " + bg9 + "");
rng1.Font.Name = "Verdana";
rng1.Font.Size = 12;
rng1.InsertParagraphAfter();
rng1.SetRange(rng1.End, rng1.End);
rng1.Select();
Object start2 = 0;
Object end2 = 1;
Microsoft.Office.Interop.Word.Range rng2 = oWordDoc.Range(ref start2, ref end2);
rng2.InsertAfter(" Report Type: Report");
rng2.Font.Name = "Verdana";
rng2.Font.Size = 12;
rng2.InsertParagraphAfter();
rng2.SetRange(rng2.End, rng2.End);
rng2.Select();
Object start3 = 0;
Object end3 = 0;
Microsoft.Office.Interop.Word.Range rng3 = oWordDoc.Range(ref start1, ref end1);
rng3.InlineShapes.AddPicture(@"C://Users//Public//Pictures//Sample Pictures//11111111.jpg", ref oMissing, ref oMissing, ref oMissing);
rng3.SetRange(rng3.End, rng3.End);
rng3.Select();
Object start = oWordDoc.Content.End - 1;
Object end = oWordDoc.Content.End;
Microsoft.Office.Interop.Word.Range rng = oWordDoc.Range(ref start, ref end);
ADD TABLE
Microsoft.Office.Interop.Word.Table tbl = oWordDoc.Tables.Add(rng, Dt.Rows.Count, Dt.Columns.Count, ref oMissing, ref oMissing);
END ADD TABLE
Object defaultTableBehavior = Type.Missing;
Object autoFitBehavior = Type.Missing;
object missing = System.Type.Missing;
Microsoft.Office.Interop.Word.Row newRow = oWordDoc.Tables[1].Rows.Add(ref missing);
newRow.Range.Font.Bold = 0;
newRow.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
tbl.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
tbl.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
tbl.Rows.Borders.InsideColor = Microsoft.Office.Interop.Word.WdColor.wdColorBlueGray;
tbl.Rows.Shading.ForegroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
tbl.Rows.Shading.BackgroundPatternColor = Microsoft.Office.Interop.Word.WdColor.wdColorPlum;
string filename = string.Empty;
saveFileDialog1.InitialDirectory = @"C:\";
saveFileDialog1.Title = "Save Word Files";
saveFileDialog1.CheckFileExists = true;
saveFileDialog1.CheckPathExists = true;
saveFileDialog1.DefaultExt = "docx";
saveFileDialog1.Filter = "Word files (*.doc) |*.doc|Word files (*.docx) |*.docx|All files (*.*) |*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
filename = saveFileDialog1.FileName;
for (int i = 0; i <= Dt.Rows.Count; i++)
{
if (i == Dt.Rows.Count)
{
oWordDoc.SaveAs(filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
MessageBox.Show("The file " + saveFileDialog1.FileName + " has been exported successfully.");
}
else
{
storing header part in Word
for (int i2 = 1; i2 < dataGridView1.Columns.Count + 1; i2++)
{
tbl.Cell(1, i2).Range.Text = dataGridView1.Columns[i2 - 1].HeaderText;
}
storing each row and column value to Word document
for (int j = 0; j < Dt.Columns.Count; j++)
{
tbl.Cell(i + 2, j + 1).Range.Text = Dt.Rows[i][j].ToString();
}
}
}
}
StyleDataGridView();
oWord.Selection.TypeParagraph();
}
catch
{
}
}