Siverlight 导出Excel (经测试通过 Vs2010 ,silverlight5 )
网上搜了下,很多代码都有各种问题,自己抽时间整理了一下这个导出
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"));
} }
}
Siverlight 导出Excel (经测试通过 Vs2010 ,silverlight5 )的更多相关文章
- php两种导出excel的方法
所需要的:jquery库,phpexcel插件,页面导出excel效果测试文件explode.php,excel导出功能实现文件exp.php和explode_excel.php,文件相关内容在此文下 ...
- 前端导出Excel兼容写法
今天整理出在Web前端导出Excel的写法,写了一个工具类,对各个浏览器进行了兼容. 首先,导出的数据来源可能有两种: 1. 页面的HTML内容(一般是table) 2. 纯数据 PS:不同的数据源, ...
- Oracle导出excel
oracle导出excel(非csv)的方法有两种,1.使用sqlplus spool,2.使用包体 现将网上相关代码整理后贴出以备不时之需: 使用sqlplus: 使用sqlplus需要两个文件: ...
- Java导出excel
一.介绍 常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际的开发中,很多时候需要实现导入.导出Excel的应用. ...
- Python导出Excel为Lua/Json/Xml实例教程(一):初识Python
Python导出Excel为Lua/Json/Xml实例教程(一):初识Python 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出 ...
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- PHP导入导出excel表格图片(转)
写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了.呵呵.(COM方式不讲了,这种可读的太多了,我也写过利用wp ...
- MVC 导出Excel 的其中一方法(View导出excel)
场景:mvc下导出excel 思路:使用View导出excel 步骤: 1.导出标签添加事件 $("#export_A").click(function(){ //省略代码.... ...
- chrome浏览器js 导出excel
<table id="table"> <tr> <th>ID</th> <th>姓名</th> <th ...
随机推荐
- sql-将字符串按指定字符分割插入指定表中
CREATE PROC SPLIT( @STR VARCHAR(MAX), --截取字符串 ), --截取字符 ) --存放表名 ) AS BEGIN ) IF(OBJECT_ID(@TABLE) I ...
- Python进阶(三)--global和类属性
global关键字 一句话概括为:告诉python解释器,global声明的变量为全局作用域内定义的变量.解释器就会到全局作用域内寻找global定义的变量. python的类属性 类属性相当于其他O ...
- Linux C相关基础
系统求助 man 函数名 man 2 函数名 - 表示函数是系统调用函数 man 3 函数名 - 表示函数是C的库函数 eg:man fread man 2 w ...
- 退役&&搬家
牡丹江与鞍山两站作为最后的结束站.一银一铜就此结束了~ 此博客用来怀念ACM就此保留并不添加任何其它与其无关内容. ------------------------------------------ ...
- VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION 这篇论文
由Andrew Zisserman 教授主导的 VGG 的 ILSVRC 的大赛中的卷积神经网络取得了很好的成绩,这篇文章详细说明了网络相关事宜. 文章主要干了点什么事呢?它就是在在用卷积神经网络下, ...
- Redis教程(三) list类型
一.概述: redis的list类型其实就是一个每个子元素都是string类型的双向链表.所以[lr]push和[lr]pop命令的算法时间复杂度都是O(1) 另外list会记录链表的长度.所以ll ...
- Git 安装
安装参考资料: http://lzw.me/a/msysgit-tortoisegit-win-git.html http://blog.csdn.net/qwiwuqo/article/detail ...
- STM32 串口固件库中定义的几个中断标志位什么意思?
在stm32f10x_usart.h中以上几个宏,很没有规律,诈一看还真不知道为什么会这么定义,其实通过代码就很容易明白: D7~D5:代表中断标志位对应的中断使能位在 CR1.CR2还是CR3寄存器 ...
- 实验三——for 语句及分支结构else-if
1.本节课学习到的知识点:在本次课中,我学习了for语句的使用,认识了for语句的执行流,明确了三种表达式的意义.以及最常用的实现多分支的else-if语句. 2.实验过程中遇到的问题及解决方法:在本 ...
- OpenBSD内核之引导PBR
OpenBSD引导的第二部PBR,也是活动分区的一个扇区的代码,由第一步的MBR加载到0x7C00处,manpage里详细的讲解了过程和大致实现 biosboot(8) (http://man.ope ...