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. android 中FragmentActivity中模拟返回键返回上一个Activity效果

    FragmentTransaction中先加入一个Fragment,这个Fragment就是当前要显示的Fragment, 当通过事件触发显示第二个Fragment时,在加入第二个Fragment并调 ...

  2. 解决ssh无密码登录不成功的问题

    把ssh设置为无密码登录很简单,只需两步: 1.在本地创建公钥和私钥: ssh-keygen -t rsa 2.然后把公钥上传到远程机器上: ssh-copy-id -i ~/.ssh/id_rsa. ...

  3. InternetExplorer 表单及用户名密码提交

    陆ftp或者其他类似需要输入密码的站点,可以在url中直接输入用户名密码,格式为: ftp://username:password@url 另外一种情况是,如果是表单提交的也可以通过url填写,如: ...

  4. IOS ViewController 生命周期

    加载过程 第一步 -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 第二步 -(void) ...

  5. m元素集合的n个元素子集

    理论: 假设有5个元素的集点,取出3个元素的可能子集如下:{1 2 3}.{1 2 4 }.{1 2 5}.{1 3 4}.{1 3 5}.{1 4 5}.{2 3 4}.{2 3 5}.{2 4 5 ...

  6. 解决红米等手机(移动端)无法触发touchend事件

    触屏事件的简单描述: js的触屏事件,主要有三个事件:touchstart,touchmove,touchend. 这三个事件最重要的属性是 pageX和 pageY,表示X坐标,Y坐标.touchs ...

  7. 一笔画问题(floyd+oular+dfs)

    一笔画问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 zyc从小就比较喜欢玩一些小游戏,其中就包括画一笔画,他想请你帮他写一个程序,判断一个图是否能够用一笔画下 ...

  8. myeclipse集成weblogicserver

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

  9. css设置

    box-size允许您以特定的方式定义匹配某个区域的特定元素. content-box(默认):宽度和高度分别应用到元素的内容框.在宽度和高度之外绘制元素的内边距和边框.border-box:为元素设 ...

  10. poj-2403-cup

    题目描述 The WHU ACM Team has a big cup, with which every member drinks water. Now, we know the volume o ...