• c# 数据导出成excel 方法总结 见标红部分


    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(0, 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 < 1)
                    {
                        return;
                    }
                    //取出第一个实体的所有Propertie
                    entityType = entitys[0].GetType();
                    entityProperties = entityType.GetProperties();
                    for (int i = 0; i < entityProperties.Length; i++)
                    {
                        sb.Append(entityProperties[i].Name);
                        sb.Append(",");
                    }
                    sb.Remove(sb.Length - 1, 1);
                    sb.Append("
    ");
                    //将所有entity添加到DataTable中
                    foreach (object entity in entitys)
                    {
                        //检查所有的的实体都为同一类型
                        if (entity.GetType() != entityType)
                        {
                            throw new Exception("要转换的集合元素类型不一致");
                        }
                        object[] entityValues = new object[entityProperties.Length];
                        for (int i = 0; i < entityProperties.Length; i++)
                        {
                            try
                            {
                                entityValues[i] = entityProperties[i].GetValue(entity, null);
                                sb.Append(""" + HttpContext.Current.Server.HtmlDecode(entityValues[i].ToString().Replace(""", """").
                                    Replace("
    ", Environment.NewLine).Replace("<BR>", Environment.NewLine)) + """);
                                sb.Append(",");
                            }
                            catch
                            {
                                entityValues[i] = string.Empty;
                                sb.Append(""" + entityValues[i].ToString() + """);
                                sb.Append(",");
                            }
                        }
                        sb.Remove(sb.Length - 1, 1);
                        sb.Append("
    ");
                    }
                    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();
                }
            }
  • 相关阅读:
    sql字符串函数(转)
    sqlserver 导入/导出Excel
    SelectSingleNode和SelectNodes区别
    iOS sqlite 的各种操作
    iOS 自定义的对象类型的解档和归档
    开放-封闭原则(OCP)开-闭原则 和 依赖倒转原则,单一职责原则
    iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区
    iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
    iOS中的事件传递和响应者链条
    iOS开发 首次启动显示用户引导,第二次启动直接进入App,UIScrollView,UIPageControl,NSUserDefaults
  • 原文地址:https://www.cnblogs.com/niaowo/p/4051048.html
Copyright © 2020-2023  润新知