代码改变世界

导出数据到Excel&Word

2013-12-11 11:15  taozsay  阅读(105)  评论(0)    收藏  举报

 

//枚举转换

public EnumVictor<EducationalBackgrounds> EducationalBackgroundsEnum = new EnumVictor<EducationalBackgrounds>();

public EnumVictor<ApplicationStage> AppStage = new EnumVictor<ApplicationStage>();

 

//调用api返回List<MobileStationInfo>对象
this.Collection = this.RequestApi();

//指定输出格式为何种格式
Response.ContentType = "application/vnd.ms-excel";//application/msword;

//文件名称
const string fileName = "students.xls";//students.doc;

//指定文件下载的名称
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);//(多加注意)
//指定字符编括码格式(避免出现乱码)
Response.ContentEncoding = Encoding.GetEncoding("gb2312");

//定义标题
const string columns = "序号\t姓名\t电话号码\t电子邮箱\t所在省份\t所在城市\t所在地区\t申请阶段\t意向专业\t当前教育阶段\t留学国家\t客户来源\t提交日期\n";
Response.Write(columns);
//定义内容
var builder = new StringBuilder();

for (var i = 0; i < this.Collection.Count; i++)
{
var line = String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\t{10}\t{11}\t{12}",
i + 1,
this.Collection[i].Name,
this.Collection[i].MobileNumber,
this.Collection[i].Email,
this.Collection[i].Province,
this.Collection[i].City,
this.Collection[i].District,
this.AppStage.GetBitFieldDescriptionValue((ApplicationStage)Convert.ToInt32(this.Collection[i].AppStage)),
this.Collection[i].MajorIntention,
this.EducationalBackgroundsEnum.GetBitFieldDescriptionValue((EducationalBackgrounds)Convert.ToInt32(this.Collection[i].EducationalBackground)),
this.Collection[i].AbroadCountry,
this.Collection[i].Source,
this.Collection[i].CreationTime);
builder.AppendLine(line);
}

Response.Write(builder);
Response.End();