T
The Techie here
Guest
How can we export the entire datagridview into the word document using office word interp DLL in C# iam able to achieve this using Spire dll, as it is a free version so it has lot of limitaions to overcome these constraints iam tryng to implement similar logic using office word interop dll.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.License;
using Spire.Doc.Fields;
namespace Export_Datagridview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string filename;
public string Reportfile_path;
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("x", typeof(int)), new DataColumn("y", typeof(string)), new DataColumn("z", typeof(string)),new DataColumn("x1", typeof(int)), new DataColumn("y1", typeof(string)), new DataColumn("z1", typeof(string)) });
dt.Rows.Add(1, "1", "1","1", "1","1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
this.dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
export_datagridview();
}
private void export_datagridview()
{
string time = DateTime.Now.ToString("HH:mm:ss");
string date = DateTime.Today.ToShortDateString();
filename = Reportfile_path + "sample" + ".doc";
Document document = new Document();
try
{
document.LoadFromFile(filename, FileFormat.Doc);
}
catch
{
}
int xx = 0, yy = 0, section_number = 0;
Section section = new Section(document);
Paragraph paragraph = section.AddParagraph();
paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
yy = document.Sections.Count;
if (yy == 0)
{
section_number = yy;
section = document.AddSection();
section = document.Sections[section_number];
}
else
{
section_number = yy - 1;
section = document.Sections[section_number];
}
xx = section.Tables.Count;
if (xx == 5)
{
section_number++;
section = document.AddSection();
section = document.Sections[section_number];
}
else
{
section = document.Sections[section_number];
}
paragraph = section.AddParagraph();
paragraph.AppendText("\t\t SOMETHING");
paragraph = section.AddParagraph();
paragraph = section.AddParagraph();
paragraph.AppendText("Something\t\t:\tsomething");
paragraph = section.AddParagraph();
paragraph.AppendText("something\t\t:\tsomething");
Add_Table(dataGridView1, filename, section);
document.SaveToFile(filename, FileFormat.Doc);
}
private void Add_Table(DataGridView dGV, string filename, Section section)
{
Spire.Doc.Table table = section.AddTable();
table.ResetCells(dGV.RowCount, dGV.ColumnCount);
table.ResetCells(dGV.RowCount + 1, dGV.ColumnCount);
// first row
TableRow row = table.Rows[0];
row.IsHeader = true;
row.Height = 22;
row.HeightType = TableRowHeightType.Exactly;
row.RowFormat.BackColor = Color.Gray;
for (int i = 0; i < dGV.ColumnCount; i++)
{
row.Cells.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p = row.Cells.AddParagraph();
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange txtRange = p.AppendText(Convert.ToString(dGV.Columns.HeaderText));
txtRange.CharacterFormat.Bold = true;
}
for (int r = 0; r < dGV.RowCount; r++)
{
TableRow dataRow = table.Rows[r + 1];
dataRow.Height = 22;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
for (int c = 0; c < dGV.ColumnCount; c++)
{
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
row.Cells[c].Width = 80;
dataRow.Cells[c].Width = 80;
dataRow.Cells[c].AddParagraph().AppendText(Convert.ToString(dGV.Rows[r].Cells[c].Value));
}
}
}
}
}
Continue reading...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.License;
using Spire.Doc.Fields;
namespace Export_Datagridview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string filename;
public string Reportfile_path;
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("x", typeof(int)), new DataColumn("y", typeof(string)), new DataColumn("z", typeof(string)),new DataColumn("x1", typeof(int)), new DataColumn("y1", typeof(string)), new DataColumn("z1", typeof(string)) });
dt.Rows.Add(1, "1", "1","1", "1","1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
dt.Rows.Add(1, "1", "1", "1", "1", "1");
dt.Rows.Add(2, "2", "2", "2", "2", "2");
dt.Rows.Add(3, "3", "3", "3", "3", "3");
dt.Rows.Add(4, "4", "4", "4", "4", "4");
this.dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
export_datagridview();
}
private void export_datagridview()
{
string time = DateTime.Now.ToString("HH:mm:ss");
string date = DateTime.Today.ToShortDateString();
filename = Reportfile_path + "sample" + ".doc";
Document document = new Document();
try
{
document.LoadFromFile(filename, FileFormat.Doc);
}
catch
{
}
int xx = 0, yy = 0, section_number = 0;
Section section = new Section(document);
Paragraph paragraph = section.AddParagraph();
paragraph = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
yy = document.Sections.Count;
if (yy == 0)
{
section_number = yy;
section = document.AddSection();
section = document.Sections[section_number];
}
else
{
section_number = yy - 1;
section = document.Sections[section_number];
}
xx = section.Tables.Count;
if (xx == 5)
{
section_number++;
section = document.AddSection();
section = document.Sections[section_number];
}
else
{
section = document.Sections[section_number];
}
paragraph = section.AddParagraph();
paragraph.AppendText("\t\t SOMETHING");
paragraph = section.AddParagraph();
paragraph = section.AddParagraph();
paragraph.AppendText("Something\t\t:\tsomething");
paragraph = section.AddParagraph();
paragraph.AppendText("something\t\t:\tsomething");
Add_Table(dataGridView1, filename, section);
document.SaveToFile(filename, FileFormat.Doc);
}
private void Add_Table(DataGridView dGV, string filename, Section section)
{
Spire.Doc.Table table = section.AddTable();
table.ResetCells(dGV.RowCount, dGV.ColumnCount);
table.ResetCells(dGV.RowCount + 1, dGV.ColumnCount);
// first row
TableRow row = table.Rows[0];
row.IsHeader = true;
row.Height = 22;
row.HeightType = TableRowHeightType.Exactly;
row.RowFormat.BackColor = Color.Gray;
for (int i = 0; i < dGV.ColumnCount; i++)
{
row.Cells.CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph p = row.Cells.AddParagraph();
p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
TextRange txtRange = p.AppendText(Convert.ToString(dGV.Columns.HeaderText));
txtRange.CharacterFormat.Bold = true;
}
for (int r = 0; r < dGV.RowCount; r++)
{
TableRow dataRow = table.Rows[r + 1];
dataRow.Height = 22;
dataRow.HeightType = TableRowHeightType.Exactly;
dataRow.RowFormat.BackColor = Color.Empty;
for (int c = 0; c < dGV.ColumnCount; c++)
{
dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
row.Cells[c].Width = 80;
dataRow.Cells[c].Width = 80;
dataRow.Cells[c].AddParagraph().AppendText(Convert.ToString(dGV.Rows[r].Cells[c].Value));
}
}
}
}
}
Continue reading...