生成Excel直接以流或字节形式发给客户端,无需在服务生成一个实体文件。

    public ActionResult ExportAgentBooking(string Company_Id, string Company_Name)//Altman.Web.BCDAdmin.Models.CompanyInfo argCompanyInfo)

        {

            if (string.IsNullOrWhiteSpace(Company_Id) || string.IsNullOrWhiteSpace(Company_Name))

            {

                return View();

            }



            Dictionary<string, object> vDic = new Dictionary<string, object>();

            vDic.Add("argCompanyId", Company_Id);//argCompanyInfo.Company_Id);

            vDic.Add("argStatus", (int)Altman.Web.Common.Utility.State.Active);

            vDic.Add("argGroupType", (int)Altman.Web.Common.Utility.GroupType.AgentBookingGroup);



            ClientResponse vUserInGroup = ExcuteService("Altman.Services.BCDAdmin.Group", "GetAgentBookingUser", vDic);

            DataTable vDT = vUserInGroup["DtUsers"] as DataTable;



            Common.ExcelHelper vExcelHelper = new ExcelHelper();

            MemoryStream vStream = vExcelHelper.ExeportAgentBookingData(vDT);

            if (vStream == null)

            {

                return View();  

            }

            string vFileName = string.Concat(Company_Name, ".xls");

            return File(vStream.ToArray(), CONTENTTYPE, vFileName);

        }
View Code


EXCEL处理类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



using System.IO;

using NPOI.HSSF.UserModel;

using NPOI.SS.UserModel;

using System.Data;

namespace Altman.Web.BCDAdmin.Common

{



    public class ExcelHelper

    {

        public MemoryStream ExeportAgentBookingData(DataTable vDt)

        {

            vDt.DefaultView.Sort = "Group_Name desc,Agent_User_Id desc";

            vDt = vDt.DefaultView.ToTable();

            MemoryStream file = new MemoryStream();

            if (vDt == null || vDt.Rows.Count == 0)

            {

                return null; 

            }



            int vRowIndex = 0;

            HSSFWorkbook vHssfworkbook = new HSSFWorkbook();

            ISheet vSheet1 = vHssfworkbook.CreateSheet();//.GetSheetAt(0);

            IRow rowFirst = vSheet1.CreateRow(vRowIndex);

            rowFirst.CreateCell(0).SetCellValue("组名");

            rowFirst.CreateCell(1).SetCellValue("代订人 ");

            rowFirst.CreateCell(2).SetCellValue("被代订人 ");

            rowFirst.CreateCell(3).SetCellValue("Email");

            

            //设置sheet的属性

            for (int colCount = 0; colCount < 4; colCount++)

            {

                vSheet1.SetColumnWidth(colCount, 30* 256);

            }



                vRowIndex++;

            foreach(DataRow vRow in vDt.Rows)

            {

                IRow row = vSheet1.CreateRow(vRowIndex);

                string vUser_ID = vRow["User_ID"].AsString();

                string vAgent_User_Id = vRow["Agent_User_Id"].AsString();

                if (string.IsNullOrWhiteSpace(vUser_ID) && string.IsNullOrWhiteSpace(vAgent_User_Id))

                {

                    continue;

                }

                row.CreateCell(0).SetCellValue(vRow["Group_Name"].AsString());

                if(!string.IsNullOrWhiteSpace(vUser_ID))//被代订人 

                {

                    row.CreateCell(2).SetCellValue(vRow["User_Name"].AsString());

                }

                if(!string.IsNullOrWhiteSpace(vAgent_User_Id))//代订人 

                {

                    row.CreateCell(1).SetCellValue(vRow["User_Name"].AsString());

                }

                row.CreateCell(3).SetCellValue(vRow["User_Email"].AsString());

                vRowIndex++;

            }

            if (vRowIndex == 1)

            {

                return null;

            }

            vHssfworkbook.Write(file);

            return file;

        }

    }

}
View Code

 

你可能感兴趣的:(Excel)