public void ServiceOrderExport(string data)
{
StringBuilder sb = new StringBuilder();
Type entityType = null; ;
PropertyInfo[] entityProperties = null;
var input = data.DeserializeObject<structServiceOrder>();
using (var context = SRVDBHelper.DataContext)
{
sb.Remove(, sb.Length);
var results = context.Usp_SRV_CheckServiceOrder(input.ServiceOrderID, input.AcceptWay,
input.StatusCode, input.Description, input.OneLevelSortID, input.TwoLevelSortID,
input.ThreeLevelSortID, input.AInsNO, input.ACompanyName, input.ADepartmentID,
input.ASectionID, input.AName, input.CInsNO, input.CCompanyName, input.CDepartmentID,
input.CSectionID, input.CreatorName, input.HInsNO, input.HCompanyName, input.HDepartmentID,
input.HSectionID, input.HName, input.CreateDate1, input.CreateDate2, input.FinishDate1,
input.FinishDate2, input.OverDueStatus1,input.OverDueStatus2);
List<Usp_SRV_CheckServiceOrderResult> entitys = null;
if (input.HName !=null)
{
entitys = (from item in results
where item.处理人 != null
select item).ToList();
}
else
{
entitys = results.ToList();
}
//检查实体集合不能为空
if (entitys == null || entitys.Count < )
{
return;
}
//取出第一个实体的所有Propertie
entityType = entitys[].GetType();
entityProperties = entityType.GetProperties();
for (int i = ; i < entityProperties.Length; i++)
{
sb.Append(entityProperties[i].Name);
sb.Append(",");
}
sb.Remove(sb.Length - , );
sb.Append("\r\n");
//将所有entity添加到DataTable中
foreach (object entity in entitys)
{
//检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new Exception("要转换的集合元素类型不一致");
}
object[] entityValues = new object[entityProperties.Length];
for (int i = ; i < entityProperties.Length; i++)
{
try
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
sb.Append("\"" + HttpContext.Current.Server.HtmlDecode(entityValues[i].ToString().Replace("\"", "\"\"").
Replace("\n", Environment.NewLine).Replace("<BR>", Environment.NewLine)) + "\"");
sb.Append(",");
}
catch
{
entityValues[i] = string.Empty;
sb.Append("\"" + entityValues[i].ToString() + "\"");
sb.Append(",");
}
}
sb.Remove(sb.Length - , );
sb.Append("\r\n");
}
HttpResponse resp;
resp = System.Web.HttpContext.Current.Response;
resp.Charset = "GB2312";
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + ".csv");
resp.Write(sb);
resp.End();
}
}

c# 数据导出成excel 方法总结 见标红部分的更多相关文章

  1. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...

  2. Extjs — Grid数据导出成Excel

    最近因为项目问题,需要解决Extjs导出成Excel的问题. 下面简单描述这个问题解决的步骤如下: 1.先在js文件里写了一个button的handler事件,通过点击按钮,来实现调用ExportEx ...

  3. 使用SSM框架实现Sql数据导出成Excel表

    SSM框架实现SQL数据导出Excel 思路 首先在前端页面中添加一个导出功能的button,然后与后端controller进行交互. 接着在相应的controller中编写导出功能方法. 方法体: ...

  4. jquery.table2excel,将HTML的table标签数据导出成excel

    <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...

  5. 将数据导出成excel表

    /// <summary> /// 生成excel表 /// </summary> /// <param name="dt">数据表</p ...

  6. 有趣的Node爬虫,数据导出成Excel

    最近一直没更新了诶,因为学习Backbone好头痛,别问我为什么不继续AngularJs~因为2.0要出来了啊,妈蛋!好,言归正传,最近帮我的好基友扒数据,他说要一些股票债券的数据.我一听,那不就是要 ...

  7. 将DataTable中的数据导出成Excel

    public bool ExportFile(System.Data.DataTable dt){    SaveFileDialog sfd = new SaveFileDialog();    s ...

  8. mysql中数据导出成excel文件语句

    代码如下 复制代码 mysql>select * from xi_table into outfile ’d:test.xls’; 导出为txt文件:  代码如下 复制代码 select * f ...

  9. 史上最简单的在 Yii2.0 中将数据导出成 Excel

    在 vendor/yiisoft/yii2/helpers/ 创建一个 Excel.php <?php namespace yii\helpers;   class Excel{         ...

随机推荐

  1. 原创:应用串行NOR闪存提升内存处理能力

    在嵌入式系统中,NOR闪存一直以来仍然是较受青睐的非易失性内存,NOR器件的低延时特性可以接受代码执行和数据存储在一个单一的产品.虽然NAND记忆体已成为许多高密度应用的首选解决方案,但NOR仍然是低 ...

  2. Hbase深入学习(二) 安装hbase

    Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...

  3. < IOS > 论苹果数据持久化。

    最近做的音乐播放器,用了太多的数据存储.在各种APP中无可避免的要用到数据存储.在IOS中,给了很多办法进行数据持久化.但是万宗不离其变,都是要存储到本地中,IOS提供了沙盒机制,沙盒有多大呢???这 ...

  4. SQL语句优化汇总(上) 感动啊 学习 收藏了

    原文地址:http://topic.csdn.net/u/20080716/11/2317d040-48e7-42da-822e-040b4c55b46d.html MS   SQL   Server ...

  5. 配置VirtualBox Linux系统与Windows共享文件

    手动方式配置VirtualBox Linux系统与Windows共享文件 首先,要安装VirtualBox自带的扩展工具,这个ISO可以在虚拟主机安装目录下找到. 将这个文件Copy到你的Linux主 ...

  6. 一款超出你想象的代码审阅软件understand

    看源码人们一般会想到source insight这款软件可是这款软件目前只支持windows平台,那如果我想在Linux平台上审阅代码呢, 没关系还有一款强大的软件understand,这款软件能够生 ...

  7. 代码中实际运用memcached——java

    以下文章取自:http://jameswxx.iteye.com/blog/1168711 memcached的java客户端有好几种,http://code.google.com/p/memcach ...

  8. os x 10.10 測试版系统下载 swift语言学习资料下载

    http://pan.baidu.com/s/1eQ5oj1S               这是下载地址 ! 刚学完oc 就出了 swift!这----  只是还是非常高兴看了一点swith得东西感觉 ...

  9. myeclipse集成weblogicserver

    今天为了学一下JMS的东东, 不得不安装个weblogicserver, 下面是详细的安装步骤: 1. 首先去官网下载一个weblogic: 下载地址: http://download.oracle. ...

  10. yum安装epel库后,安装软件总是提示Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again(无法检索epel仓库)

    解决方法: vi /etc/yum.repos.d/epel.repo 将baseurl中的注释取消,将mirrorlist该列注释掉即可. 附图如下: