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. C# 调用 Web Service

    Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...

  2. lisp构造表

    CONS 操作符 我们刚刚学习了如何拆分一个表,现在学习如何合并一个表. CONS 操作符就是做这件事情的. 假设有一个列表 (1 2 3) ,我们做一下 CAR 操作: (car '(1 2 3)) ...

  3. 什么是Elasticsearch

    一个采用Restfull API 标准的高扩展性和高可用性的实时数据分析的全文搜索工具 Elasticsearch 涉及到的一些概念: 1.Node(节点): 单个的装有Elasticsearch服务 ...

  4. Android Animations 视图动画使用详解!!!

    转自:http://www.open-open.com/lib/view/open1335777066015.html Android Animations 视图动画使用详解 一.动画类型 Andro ...

  5. BFS visit tree

    There are two ways to conduct BFS on tree. Solution 1 -- Given level Use recursion to find given lev ...

  6. Median of Two Sorted Arrays (找两个序列的中位数,O(log (m+n))限制) 【面试算法leetcode】

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  7. Linux下alias命令

    功能说明:设置指令的别名.语 法:alias[别名]=[指令名称]参 数 :若不加任何参数,则列出目前所有的别名设置.举    例 :ermao@lost-desktop:~$ alias       ...

  8. 机器学习 1、R语言

    R语言 R是用于统计分析.绘图的语言和操作环境.R是属于GNU系统的一个自由.免费.源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具. 特点介绍 •主要用于统计分析.绘图.数据挖掘 •R内置 ...

  9. rabbitmq-c初探

    RabbitMQ着实是个好东西,当然了也有对C语言client开发的支持.例子和文档少的可怜,只能去项目里去查看example来理解,简单整理了一些,以免走些弯路.主要是在版本对应上,这点就没Mave ...

  10. C#比较两个时间大小

    DateTime t1 = Convert.ToDateTime("2012-12-31 23:59:00");            DateTime t2 = Convert. ...