zoukankan      html  css  js  c++  java
  • 生成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
  • 相关阅读:
    # 20155229 2016-2017-2 《Java程序设计》第七周学习总结
    20155229 实验一《Java开发环境的熟悉》实验报告
    20155229 2016-2017-2 《Java程序设计》第六周学习总结
    20155229 2016-2017-2 《Java程序设计》第五周学习总结
    20155229 2016-2017-2 《Java程序设计》第四周学习总结
    20155229 2016-2017-2 《Java程序设计》第三周学习总结
    20155229 2016-2017-2 《Java程序设计》第二周学习总结
    20155229 2016-2007-2 《Java程序设计》第一周学习总结
    20155229付钰涵-虚拟机安装及LINUX命令学习
    20155223 2016-2017-2 《Java程序设计》第10周学习总结
  • 原文地址:https://www.cnblogs.com/xuxu-dragon/p/3569534.html
Copyright © 2011-2022 走看看