• 生成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
  • 相关阅读:
    解决安装IIS时提示找不到zClientm.exe文件的问题
    在函数体内开辟动态内存时,函数形参选择指向指针的指针的原理解析
    const用法之修饰指向常量的指针
    辅助记忆“map”使用细节的经典例题
    “变量名”和“函数名”典型选记
    宁静致远
    多参数“模板类”的使用启发
    cplusplus和MSDN的优势分工
    理解关联容器“map”的关键点
    字符串或文件处理的一个可选流程
  • 原文地址:https://www.cnblogs.com/xuxu-dragon/p/3569534.html
Copyright © 2020-2023  润新知