Click here to Skip to main content
15,917,060 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
:omg: i m waitting u r rply. plzzzzzz hurrry....
Posted

XML
dlg = new SaveFileDialog();
                   dlg.Filter = "Microsoft Excel (*.xls)|*.xls";
                   dlg.DefaultExt = "xls";
                   //dlg.Filter = "Microsoft Excel (*.doc)|*.doc";
                   //dlg.DefaultExt = "doc";
                   if (dlg.ShowDialog().Value)
                   {
                       // forming stringbuilder table
                       strBuilder.Append("<table>");
                       // Starts adding grid headers to excel
                       strBuilder.Append("<tr style='background:#7f7f7f;height:25px;'>");
                       foreach (DataGridColumn item in dtgGrid.Columns)
                       {
                           if (item.Visibility == Visibility.Visible)
                           {
                               if (item is DataGridBoundColumn)
                               {
                                   strBuilder.Append("<td><b>" + item.Header.ToString() + "</b></td><td style='width:1px;background:#000000;'></td>");
                               }
                               // checking for button control in datagridtemplatecolumn then eleminating
                               else if (item is DataGridTemplateColumn)
                               {
                                   foreach (object data in dtgGrid.ItemsSource)
                                   {
                                       FrameworkElement element = dtgGrid.Columns[GetcolumnIndex(item.Header.ToString(), dtgGrid)].GetCellContent(data) as FrameworkElement;
                                       if (element is TextBlock)
                                       {
                                           if ((element as TextBlock).Text != null)
                                               strBuilder.Append("<td><b>" + item.Header.ToString() + "</b></td><td style='width:1px;background:#000000;'></td>");
                                       }
                                       else if (element is Label)
                                       {
                                           if ((element as Label).Content != null)
                                               strBuilder.Append("<td><b>" + item.Header.ToString() + "</b></td><td style='width:1px;background:#000000;'></td>");
                                       }
                                       break;
                                   }
                               }
                           }
                       }
                       strBuilder.Append("</tr>");
                       strBuilder.Append("<tr style='height:1px;background:#000000;'></td></tr>");
                       // EOF adding grid headers to excel

                       // starts adding grid data to excel
                       // Looping every row of datagrid
                       foreach (object data in dtgGrid.ItemsSource)
                       {
                           strBuilder.Append("<tr style='background:#d8d8d8;'>");
                           // Looping every column of each row datagrid
                           foreach (DataGridColumn col in dtgGrid.Columns)
                           {
                               if (col.Visibility == Visibility.Visible)
                               {
                                   if (col is DataGridBoundColumn)
                                   {
                                       binding = (col as DataGridBoundColumn).Binding;
                                       colPath = binding.Path.Path;
                                       propInfo = data.GetType().GetProperty(colPath);
                                       if (propInfo != null)
                                       {
                                           strBuilder.Append("<td style='font:11px arial;color:black;'>" + propInfo.GetValue(data, null).ToString() + "</td><td style='width:1px;background:#000000;'></td>");
                                       }
                                   }
                                   else
                                   {
                                       FrameworkElement element = dtgGrid.Columns[GetcolumnIndex(col.Header.ToString(), dtgGrid)].GetCellContent(data) as FrameworkElement;
                                       if (element != null)
                                       {
                                           if (element is TextBlock)
                                           {
                                               if ((element as TextBlock).Text != null)
                                                   strBuilder.Append("<td style='font:11px arial;color:black;'>" + (element as TextBlock).Text + " </td><td style='width:1px;background:#000000;'></td>");
                                           }
                                           else if (element is Label)
                                           {
                                               if ((element as Label).Content != null)
                                                   strBuilder.Append("<td style='font:11px arial;color:black;'>" + (element as Label).Content + " </td><td style='width:1px;background:#000000;'></td>");
                                           }
                                           //else if (element is Button)
                                           //{
                                           //    if ((element as Button).Content != null)
                                           //        strBuilder.Append("<td style='font:11px arial;color:black;'>" + (element as Button).Content + " </td><td style='width:1px;background:#000000;'></td>");
                                           //}
                                       }
                                   }
                               }
                           }
                           strBuilder.Append("</tr>");
                           // ends Looping every column of datagrid row &
                           // adding line break
                           strBuilder.Append("<tr style='height:1px;background:#000000;'></td></tr>");
                       }
                       // EOF adding grid data to excel
                       strBuilder.Append("</table>");

                       using (Stream stream = dlg.OpenFile())
                       {
                           using (StreamWriter writer = new StreamWriter(stream))
                           {
                               writer.Write(strBuilder);
                               // closing streamwriter from writing content to a file
                               writer.Close();
                           }
                           // closing stream to release resource
                           stream.Close();
                           MessageBox.Show("Report is exported successfully");
                       }
                   }
 
Share this answer
 
Comments
Sandeep Mewara 4-Oct-10 13:27pm    
Why to pop such an old question?
Not many people are going to respond if you demand them to hurry, nor will they respond if it seems like you are asking them to do all of your work. You must put in some effort.

If you really want help, you need to provide us with more information about your project and what problems you are having. Perhaps include some of your code. At the least you should tell us what language you are programming in.

Now, in response to your question it sounds like you are going to have to do a tie in with Microsoft Office. You could start by researching CodeProject articles that deal with Excel[^], Word[^], and PDFs[^].

Hope this helps you get started.
 
Share this answer
 
v2
I have web-based sales projection system that displays sales reports to the user in a asp:GridView. The user have the option to save it out and open in MS Excel. This is my method to accomplish the task. You can easily modify for MS Word also:
C#
private void ExportGridView()
{
    string attachment = "attachment; filename=SalesReport.xls";
    Response.ClearContent();
    Response.Charset = "";
    EnableViewState = false;
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    using (HtmlForm frm = new HtmlForm())
    {
        Controls.Add(frm);
        frm.Controls.Add(gvSalesReport);
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                frm.RenderControl(htw);
            Response.Write(sw.ToString());
        }
    }
    Response.End();
}
 
Share this answer
 
v2
i search a solution for winforms please evrybody
 
Share this answer
 

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