数据导出

        private DataSet ListToDataSet(IList<Asset> list)
        {
            DataSet mResult = new DataSet();
            if (list != null)
            {
                var newList = from l in list
                              select new
                              {
                                  l.Id,
                                  Type = l.AssetTypeId == null ? null : l.AssetTypeId.AssetTypeName,
                                  PurchaseDate = l.PurchaseDate == null ? "" : l.PurchaseDate.ToShortDateString(),
                                  Name = l.AssetName,
                                  Status = l.Status == null ? null : l.Status.StatusName,
                                  EmployeeName = l.EmployeeName,
                                  DepartmentName = l.Department == null ? null : l.Department.DepartmentName,
                                  CPU = l.CPU,
                                  Model = l.ModelId == null ? null : l.ModelId.ModelName,
                                  WarrantyStart = l.WarrantyStartDate != null ? l.WarrantyStartDate.ToShortDateString() : "",
                                  WarrantyEnd = l.WarrantyEndDate != null ? l.WarrantyEndDate.ToShortDateString() : "",
                                  Location = l.Location == null ? null : l.Location.LocationName,
                                  RAM = l.MEMTypeId == null ? null : l.MEMTypeId.MEMTypeName,
                                  HD = l.HDTypeId == null ? null : l.HDTypeId.HDTypeName,
                                  Brand = l.Brand == null ? null : l.Brand.BrandName,
                                  l.ServiceTag,
                                  l.Scrap,
                                  l.Remark
                              };

                IList aList = newList.ToList();


                DataTable mDataTable = new DataTable();
                if (aList.Count > 0)
                {
                    PropertyInfo[] mPropertys = aList[0].GetType().GetProperties();
                    foreach (PropertyInfo mPi in mPropertys)
                    {
                        if (!mPi.PropertyType.ToString().Contains("String"))
                        {
                            mDataTable.Columns.Add(mPi.Name, typeof(string));
                        }
                        else
                        {
                            mDataTable.Columns.Add(mPi.Name, mPi.PropertyType);
                        }
                    }

                    for (int i = 0; i < aList.Count; i++)
                    {
                        ArrayList mTempList = new ArrayList();
                        foreach (PropertyInfo mPi in mPropertys)
                        {
                            object mObj = mPi.GetValue(aList[i], null);
                            mTempList.Add(mObj);
                        }
                        object[] mArray = mTempList.ToArray();
                        mDataTable.LoadDataRow(mArray, true);
                    }
                }
                mResult.Tables.Add(mDataTable);
            }
            return mResult;
        }

        public ActionResult ExportToExcel()
        {
            //根据传过来的参数查询要导出的数据
            //listAsset = AssetService.SelectAll();
            //DataSet dataInfo = IListToDataSet();

            var listAsset = GetListBySearch("", "");
            var dataInfo = ListToDataSet(listAsset);

            System.Data.DataTable dtData = dataInfo.Tables[0];
            System.Web.UI.WebControls.DataGrid dgExport = null;
            // 当前对话  
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            // IO用于导出并返回excel文件  
            StringWriter strWriter = null;
            HtmlTextWriter htmlWriter = null;
            string filename = "comp_" + DateTime.Now.ToLongTimeString();
            byte[] str = null;

            if (dtData != null)
            {
                // 设置编码和附件格式  
                //curContext.Response.ContentType = "application/vnd.ms-excel";
                //curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                //curContext.Response.Charset = "gb2312";

                dgExport = new System.Web.UI.WebControls.DataGrid();
                dgExport.DataSource = dtData.DefaultView;
                dgExport.AllowPaging = false;
                dgExport.DataBind();

                Response.ClearContent();
                Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xls");
                Response.ContentType = "application/excel";

                strWriter = new StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                dgExport.RenderControl(htmlWriter);
                //// 返回客户端  
                str = System.Text.Encoding.UTF8.GetBytes(strWriter.ToString());
            }
            //var mPath = Server.MapPath("~/Temp/") + "1.xls";
            //return File(mPath, "application/excel", "1.xls");

            return File(str, "application/excel", filename + ".xls");
        }

 

posted @ 2013-01-25 15:52  xust  阅读(151)  评论(0)    收藏  举报