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. 中国版 Ubuntu Kylin 14.04 LTS 麒麟操作系统中文版发布下载 (Ubuntu天朝定制版)

    中国版 Ubuntu Kylin 14.04 LTS 麒麟操作系统中文版发布下载 (Ubuntu天朝定制版) http://www.iplaysoft.com/ubuntukylin.html

  2. Python 字符串相关操作

    # 1 * 重复输出字符串 print('hello'*2) # 2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表 print('helloworld'[2: ...

  3. 了解SQL注入攻击

    SQL注入:利用现有应用程序,将(恶意)的SQL命令注入到后台数据库引擎执行的能力,这是SQL注入的标准释义. 随着B/S模式被广泛的应用,用这种模式编写应用程序的程序员也越来越多,但由于开发人员的水 ...

  4. SHDP--Working With HBase (二)之HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成

    Phoenix:Phoenix将SQL查询语句转换成多个scan操作,并编排执行最终生成标准的JDBC结果集.   Spring将数据库访问的样式代码提取到JDBC模板类中,JDBC模板还承担了资源管 ...

  5. Android应用程序组件Content Provider应用实例

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6950440 文简要介绍了Android应用程序 ...

  6. FCKeditor

    FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器.它志于轻量化,不需要太复杂的安装步骤即可使用.     它可和PHP.JavaScript.ASP.ASP.NET.Col ...

  7. android——ObjectAnimator动画(一)

    直接贴上集中用法 package com.example.test; import com.example.test.views.CircleView; import android.animatio ...

  8. sql server数据库区分大小写设置

    数据库表中字段alter Table TableName 区分大小写 ALTER Column ColumnName VARCHAR(50) COLLATE Chinese_PRC_CS_AS不区分大 ...

  9. 【转】GATT Profile 简介

    网上关于讲解 BLE 的内容比较少,看到这篇文章写的非常详细 Introduction to Bluetooth Low Energy,作为 BLE 的入门时介绍是非常合适的.本文主要翻译了一下这篇文 ...

  10. hibernate:XXX is not mapped

    hibernate:XXX is not mapped  检查项目中是否将hbm.xml引入