How to save exported datagridview in a well organised way-C#

  • Thread starter Thread starter The Techie here
  • Start date Start date
T

The Techie here

Guest
All exported Datagridview contenet is getting merged in a single place (getting merged with each other) is there way to save them in a well organised way (recently exported table below the previously exported table) ?

How to avoid overlapping?

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 System.IO;

namespace Save_DataGridView_As_Word_Doc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public string filename;
public string filepath;

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("1");
dt.Columns.Add("2");
dt.Columns.Add("3");
dt.Columns.Add("4");
dt.Columns.Add("5");
dt.Columns.Add("6");
dt.Columns.Add("7");
dt.Columns.Add("8");
dt.Columns.Add("9");
dt.Columns.Add("10");
dt.Columns.Add("11");
dt.Columns.Add("12");
dt.Columns.Add("13");
dt.Columns.Add("14");
dt.Columns.Add("15");
dt.Columns.Add("16");
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });
dt.Rows.Add(new object[] { "1", 2, "3", "4", "5", 6, "7", "8", "9", 10, "11", "12", "13", 14, "15", "16" });

dataGridView1.DataSource = dt;
}

private void button1_Click(object sender, EventArgs e)
{
save_datagridview(dataGridView1, filename);
}

public void save_datagridview(DataGridView DGV, string filename)
{
string time = DateTime.Now.ToString("HH:mm:ss");
string date = DateTime.Today.ToShortDateString();
filename = filepath + @"D:\datagridview" + ".docx";

var application = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();

try
{
var originalDocument = application.Documents.Open(filename);
}
catch
{

}

if (DGV.Rows.Count != 0)
{
int RowCount = DGV.Rows.Count;
int ColumnCount = DGV.Columns.Count;
Object[,] DataArray = new object[RowCount + 1, ColumnCount + 1];
int r = 0; for (int c = 0; c <= ColumnCount - 1; c++)
{
for (r = 0; r <= RowCount - 1; r++)
{
DataArray[r, c] = DGV.Rows[r].Cells[c].Value;
}
}

application.ActiveDocument.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape;
dynamic orange = application.ActiveDocument.Content.Application.Selection.Range;
string otemp = "";
for (r = 0; r <= RowCount - 1; r++)
{
for (int c = 0; c <= ColumnCount - 1; c++)
{
otemp = otemp + DataArray[r, c] + "\t";
}
}

orange.Text = otemp;
object Separator = Microsoft.Office.Interop.Word.WdTableFieldSeparator.wdSeparateByTabs;
object ApplyBorders = true;
object AutoFit = true;
object AutoFitBehavior = Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent;
orange.ConvertToTable(ref Separator, ref RowCount, ref ColumnCount, Type.Missing, Type.Missing, ref ApplyBorders, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, ref AutoFit, ref AutoFitBehavior, Type.Missing);

orange.Select();
application.ActiveDocument.Application.Selection.Tables[1].Select();
application.ActiveDocument.Application.Selection.Tables[1].Rows.AllowBreakAcrossPages = 0;
application.ActiveDocument.Application.Selection.Tables[1].Rows.Alignment = 0;
application.ActiveDocument.Application.Selection.Tables[1].Rows[1].Select();
application.ActiveDocument.Application.Selection.InsertRowsAbove(1);
application.ActiveDocument.Application.Selection.Tables[1].Rows[1].Select();

application.ActiveDocument.Application.Selection.Tables[1].Rows[1].Range.Bold = 1;
application.ActiveDocument.Application.Selection.Tables[1].Range.Font.Name = "Tahoma";
application.ActiveDocument.Application.Selection.Tables[1].Rows[1].Range.Font.Size = 14;

for (int c = 0; c <= ColumnCount - 1; c++)
{
application.ActiveDocument.Application.Selection.Tables[1].Cell(1, c + 1).Range.Text = dataGridView1.Columns[c].HeaderText;
}
application.ActiveDocument.Application.Selection.Tables[1].set_Style("Grid Table 4 - Accent 5");
application.ActiveDocument.Application.Selection.Tables[1].Rows[1].Select();
application.ActiveDocument.Application.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

foreach (Microsoft.Office.Interop.Word.Section section in application.ActiveDocument.Application.ActiveDocument.Sections)
{
Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);

headerRange.Text = "XYZ";
headerRange.Font.Size = 18;
headerRange.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
}

//object start = 0, end = 0;
//Microsoft.Office.Interop.Word.Range rng1 = application.ActiveDocument.Range(ref start, ref end);
//Microsoft.Office.Interop.Word.Range rng2 = application.ActiveDocument.Range(ref start, ref end);
//rng1.SetRange(rng1.End, rng1.End);
//rng1.Text = "\t\t\t\t\t\t xyz\t :\t xyz ";
//rng2.SetRange(rng2.End, rng2.End);
//rng2.Text = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t zyz\t :\t xyz ";

application.ActiveDocument.Save();
application.Quit();
MessageBox.Show("Document created successfully !");

}
}


}
}

Continue reading...
 
Back
Top