导出Excel
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; namespace App_Helper { public abstract class ExportBase { protected int ColumnNum; protected string separator = "\t"; protected string contentType = "text/plain"; protected string fileExt = ".txt"; protected ExportHeader header; protected ExportBody body; public ExportHeader Header { get { return header; } set { header = value; } } public ExportBody Body { get { return body; } set { body = value; } } public string ContentType { get { return contentType; } set { contentType = value; } } public ExportBase(int columnNum) { ColumnNum = columnNum; header = new ExportHeader(columnNum); body = new ExportBody(columnNum); } public bool SaveToFile(string fileName, Encoding encoding) { try { File.WriteAllText(fileName, Output(), encoding); return true; } catch { return false; } } public string Output() { StringBuilder result = new StringBuilder(); result.Append(header.Output(separator)); result.Append(Environment.NewLine); result.Append(body.Output(separator)); return result.ToString(); } public string BuildFileName(string fileName) { fileName = fileName.Split('.')[0]; return fileName + fileExt; } } public class ExportContent { private List<string> content; //public ExportContent(int num) { content = new List<string>(num); } //去除初始可存储数据限制,使得能够导出大数据源 public ExportContent(int num) { content = new List<string>(); } public void SetContent(List<string> value) { content = value; } public void SetContent(params string[] value) { content.Clear(); content.AddRange(value); } public string Output(string separator) { return string.Join(separator, content.ToArray()); } /// <summary> /// 直接输出列表数据 /// </summary> /// <returns></returns> public List<string> Output() { return content; } } public class ExportHeader { private ExportContent content; public ExportHeader(int num) { content = new ExportContent(num); } public void SetHeader(List<string> value) { content.SetContent(value); } public void SetHeader(params string[] value) { content.SetContent(value); } public string Output(string seperate) { return content.Output(seperate); } /// <summary> /// 直接输出Header列表 /// </summary> /// <returns></returns> public List<string> Output() { return content.Output(); } } public class ExportBody { private int ColumnNum; private List<ExportContent> content; public ExportBody(int num) { ColumnNum = num; content = new List<ExportContent>(); } public void AddBody(List<String> value) { ExportContent item = new ExportContent(ColumnNum); item.SetContent(value); content.Add(item); } public void AddBody(params string[] value) { ExportContent item = new ExportContent(int.MaxValue); item.SetContent(value); content.Add(item); } public string Output(string seperate) { StringBuilder result = new StringBuilder(); int count = content.Count(); for (int i = 0; i < count; i++) { ExportContent item = content[i]; result.Append(item.Output(seperate)); if (i < count) result.Append(Environment.NewLine); } return result.ToString(); } /// <summary> /// 直接输出列表内容 /// </summary> /// <param name="seperate"></param> /// <returns></returns> public List<ExportContent> Output() { return content; } } } -------------------------------------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace App_Helper { public class Excel_File : ExportBase { public Excel_File(int num) : base(num) { separator = "\t"; contentType = "application/vnd.ms-excel"; fileExt = ".xls"; } public void Write(System.Web.HttpResponseBase Response, string filename) { Response.Clear(); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/vnd.ms-excel"; Response.Write(Output()); } } public class Txt_File : ExportBase { public Txt_File(int num) : base(num) { separator = "\t"; contentType = "text/plain"; fileExt = ".txt"; } } public class CSV_File : ExportBase { public CSV_File(int num) : base(num) { separator = ","; contentType = "application/vnd.ms-excel"; fileExt = ".csv"; } } } -------------------------------------------------------------------------调用 public ActionResult Exportexl(string StatDate, int Platform = 0) { Excel_File file = GetData(StatDate, Platform); ResponseHelper.DownloadFile(Response, file.BuildFileName("库存滞销"), file); return View(); } //获取导出数据 public Excel_File GetData(string StatDate, int Platform = 0) { Excel_File file = new Excel_File(12); file.Header.SetHeader( "组别", "组别", "组别", "组别", "组别", "组别", "组别", "组别", "组别", "组别", "组别", "组别" ); foreach (Fact_sellercube_tb_statunsalablestore_result_Process_Mage areaitem in AreaList) { file.Body.AddBody( areaitem.Area, areaitem.Team, areaitem.Groups, areaitem.NowStoreTotal.DecimalToString("F2"), areaitem.amount60.DecimalToString("F2"), areaitem.rate60.DecimalToString("F2"), areaitem.rateold60.DecimalToString("F2"), (areaitem.rate60 - areaitem.rateold60).DecimalToString("F2"), areaitem.amount180.DecimalToString("F2"), areaitem.rate180.DecimalToString("F2"), areaitem.rateold180.DecimalToString("F2"), (areaitem.rate180 - areaitem.rateold180).DecimalToString("F2") ); } }