Click here to Skip to main content
15,564,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

can any one help me with code

to export data in datagridview to a word file..
am developing a windows application.

am using word MS office 2007, OS is windows 7,
and VS version is Microsoft Visual Studio 2008....


debug in is x86

thanks in advance
looking forward for help...
Posted
Updated 30-Sep-11 22:11pm
v2

 
Share this answer
 
Comments
Ragi Gopi 1-Oct-11 3:35am    
got the ans....

i used the following code


private void button3_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 0;
int j = 0;

for (i = 0; i <= dgv1.RowCount - 1; i++)
{
for (j = 0; j <= dgv1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dgv1[j, i];
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}

xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);

MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");

}



but what to do display the header of each column
CodingLover 1-Oct-11 4:40am    
You can define the header row before store the data on rest of the rows.
Ragi Gopi 1-Oct-11 4:58am    
got it...
Ragi Gopi 1-Oct-11 3:40am    
transferred to exel..
i
CodingLover 1-Oct-11 4:40am    
Any reason related with technical stuff, that you change from Word to Excel.
C#
private void button3_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            int i = 0;
            int j = 0;

            for (i = 0; i <= dgv1.RowCount - 1; i++)
            {
                for (j = 0; j <= dgv1.ColumnCount - 1; j++)
                {
                    DataGridViewCell cell = dgv1[j, i];
                    xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
                }
            }

            xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls");
       
        }
 
Share this answer
 
v2
Comments
CodingLover 1-Oct-11 4:42am    
Edit: Code warp with relevant tags
Ragi Gopi 1-Oct-11 5:14am    
got the ans ...
but its not appearing in table format
C#
private void button5_Click(object sender, EventArgs e)
     {
         TextWriter tw = new StreamWriter("gridviewcontent.doc");

         for (int x = 0; x < dgv1.Columns.Count; x++)
         {
             tw.Write(dgv1.Columns[x].HeaderText);
             if (x != dgv1.Columns.Count - 1)
             {
                 tw.Write(", ");
             }

         }

         tw.WriteLine();

         //writing the data
         for (int x = 0; x < dgv1.Rows.Count - 1; x++)
         {
             for (int y = 0; y < dgv1.Columns.Count; y++)
             {
                 tw.Write(dgv1.Rows[x].Cells[y].Value);
                 if (y != dgv1.Columns.Count - 1)
                 {
                     tw.Write(", ");
                 }
             }
             tw.WriteLine();
         }
         tw.Close();
     }
 
Share this answer
 
hi,

it is very easy, when you click button export, fire this :

GridView1.MasterTableView.ExportToWord;

e.g:

C#
protected void btnExport_Click(object sender, EventArgs e)
{

    gvPaidtoSNA.ExportSettings.IgnorePaging = true;
    gvPaidtoSNA.ExportSettings.OpenInNewWindow = true;
    gvPaidtoSNA.MasterTableView.ExportToWord;

}
 
Share this answer
 
Comments
Ragi Gopi 1-Oct-11 5:45am    
Error 1 'System.Windows.Forms.DataGridView' does not contain a definition for 'ExportSettings' and no extension method 'ExportSettings' accepting a first argument of type 'System.Windows.Forms.DataGridView' could be found (are you missing a using directive or an assembly reference?) D:\ETMS\ETMS\ETMS\edit_fund_agen.cs 336 18 ETMS



got this error
costavo 1-Oct-11 5:53am    
oh, dude
i didn't pay attention that your app is a windows app
this solution is for web applications

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900