我如何在ms-word,excel,pdf中导出datagridview [英] how i can export datagridview in ms-word,excel,pdf

查看:72
本文介绍了我如何在ms-word,excel,pdf中导出datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:omg:我正在等你. ...

:omg: i m waitting u r rply. plzzzzzz hurrry....

推荐答案

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");
                       }
                   }


如果您要求他们赶时间,没有多少人会做出回应,如果您要他们做所有的工作,他们也不会做出回应.您必须付出一些努力.

如果您确实需要帮助,则需要向我们提供有关您的项目以及遇到的问题的更多信息.也许包括一些您的代码.至少您应该告诉我们您使用的是哪种语言.

现在,在回答您的问题时,听起来您将不得不与Microsoft Office配合使用.您可以从研究与 Excel [ ^ ], PDF文件 [ ^ ].

希望这对您有所帮助.
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.


我有一个基于Web的销售预测系统,可以在asp:GridView中向用户显示销售报告.用户可以选择将其保存并在MS Excel中打开.这是我完成任务的方法.您还可以轻松地为MS Word进行修改:
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:
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();
}


这篇关于我如何在ms-word,excel,pdf中导出datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆