惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
月光博客
月光博客
AI
AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
WordPress大学
WordPress大学
GbyAI
GbyAI
L
Lohrmann on Cybersecurity
O
OpenAI News
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Vercel News
Vercel News
The Hacker News
The Hacker News
Y
Y Combinator Blog
Latest news
Latest news
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cisco Blogs
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题

博客园 - 奚彧

打印代码片段 部署silverlight ria 程序 备忘 刚学 silverlight 问题遇到一堆 2010 微软难道抛弃了ado.net data service openfire 安装 关于DataSet.merge 问题的答案。 关于merge .net常用代码 (收藏) - 奚彧 - 博客园 碰到一个巨郁闷的情况! - 奚彧 - 博客园 关于连接池问题 删除确认按钮 - 奚彧 - 博客园 水晶报表自动插入空白行 也谈如何缩小SQL SERVER日志文件 新年第一帖,祝大家新年快乐! vs2005什么时候能出正式版 《WEB打印的相关技术分析》 web打印2 web打印 权限设置问题!(比较着急)
Siverlight 导出Excel (经测试通过 Vs2010 ,silverlight5 )
奚彧 · 2013-10-28 · via 博客园 - 奚彧

网上搜了下,很多代码都有各种问题,自己抽时间整理了一下这个导出

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Data;
using System.Reflection;

namespace MySlSyj
{
    public static class dataGridkz
    {
        public static string ExportDataGrid(this DataGrid grid, bool withHeaders)
        {
            string colPath;
            System.Reflection.PropertyInfo propInfo;
            System.Windows.Data.Binding binding;
            System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();          

            List<string> headers = new List<string>();
            List<string> GetValue = new List<string>();
            foreach (var cl in grid.Columns)
            {
                headers.Add(FormatCSVField(cl.Header.ToString()));

            }
          
            strBuilder
            .Append(String.Join("", headers.ToArray()))
            .Append("\t\n");


            ////
            //int i = 0;
            string AA = "";
            foreach (Object data in grid.ItemsSource)
            {
                var csvRow = new List<string>();
               
                foreach (DataGridColumn col in grid.Columns)
                {
                    string strValue = "";
                   
                    Binding objBinding = null;
                    if (col is DataGridBoundColumn)

                        objBinding = (col as DataGridBoundColumn).Binding;

                    if (col is DataGridTemplateColumn)
                    {
                        //这是一个模板列
                        DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent();
                        FrameworkElement oFE = (FrameworkElement)objDO;
                        FieldInfo oFI = oFE.GetType().GetField("TextProperty");
                        if (oFI != null)
                        {
                            if (oFI.GetValue(null) != null)
                            {
                                if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)) != null)
                                    objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)).ParentBinding;
                            }
                        }
                    }
                    if (objBinding != null)
                    {
                        if (objBinding.Path.Path != "")
                        {
                            PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path);
                            if (pi != null) strValue = pi.GetValue(data, null).ToString();
                        }
                        if (objBinding.Converter != null)
                        {
                            if (strValue != "")
                                strValue = objBinding.Converter.Convert(strValue, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
                            else
                                strValue = objBinding.Converter.Convert(data, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString();
                        }
                    }
                  
                   GetValue.Add(FormatCSVField(strValue));                
                }
              
              strBuilder.Append(String.Join("", GetValue.ToArray())).Append("\t\n");
              GetValue.Clear();           
            }
            
            return strBuilder.ToString();
        }


        private static string FormatCSVField(string data)
        {
            
            return String.Format("\t{0}", data.Replace("\"", "\t\n"));
        }

    }
}