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);
}
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 == )
{
return null;
} int vRowIndex = ;
HSSFWorkbook vHssfworkbook = new HSSFWorkbook();
ISheet vSheet1 = vHssfworkbook.CreateSheet();//.GetSheetAt(0);
IRow rowFirst = vSheet1.CreateRow(vRowIndex);
rowFirst.CreateCell().SetCellValue("组名");
rowFirst.CreateCell().SetCellValue("代订人 ");
rowFirst.CreateCell().SetCellValue("被代订人 ");
rowFirst.CreateCell().SetCellValue("Email"); //设置sheet的属性
for (int colCount = ; colCount < ; colCount++)
{
vSheet1.SetColumnWidth(colCount, * );
} 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().SetCellValue(vRow["Group_Name"].AsString());
if(!string.IsNullOrWhiteSpace(vUser_ID))//被代订人
{
row.CreateCell().SetCellValue(vRow["User_Name"].AsString());
}
if(!string.IsNullOrWhiteSpace(vAgent_User_Id))//代订人
{
row.CreateCell().SetCellValue(vRow["User_Name"].AsString());
}
row.CreateCell().SetCellValue(vRow["User_Email"].AsString());
vRowIndex++;
}
if (vRowIndex == )
{
return null;
}
vHssfworkbook.Write(file);
return file;
}
}
}