Beispiel #1
0
        public ActionResult Export()
        {
            List <CompanyInfo> list      = new Manager.EmployeeManager().GetCompanies(string.Empty);
            IExcel             excel     = ExcelFactory.CreateDefault();
            DataTable          dataTable = Converts.ListToDataTable <CompanyInfo>(list);

            dataTable.Columns["Name"].Caption           = "单位名称";
            dataTable.Columns["Code"].Caption           = "组织机构代码";
            dataTable.Columns["Representative"].Caption = "法人";
            dataTable.Columns["AccountBalance"].Caption = "账户余额";
            dataTable.Columns.Remove("CompanyId");
            dataTable.Columns.Remove("AccountId");
            dataTable.Columns.Remove("Positions");
            dataTable.Columns.Remove("Remark");
            DataSet dataSet = new DataSet();

            dataSet.Tables.Add(dataTable);
            POIStream stream = new POIStream();

            stream.AllowClose = false;
            excel.Write(stream, dataSet, ExcelExtendType.XLSX);
            stream.AllowClose = true;
            byte[] buffer = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(buffer, 0, buffer.Length);
            stream.Dispose();

            HttpResponse context = System.Web.HttpContext.Current.Response;

            try
            {
                context.ContentType = "application/ms-excel";
                context.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", HttpUtility.UrlEncode("单位信息", System.Text.Encoding.UTF8)));
                context.BinaryWrite(buffer);
                context.Flush();
                context.End();
            }
            catch (Exception ex)
            {
                context.ContentType = "text/plain";
                context.Write(ex.Message);
            }
            return(null);
        }