博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
导出Excel
阅读量:5054 次
发布时间:2019-06-12

本文共 5802 字,大约阅读时间需要 19 分钟。

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
content; //public ExportContent(int num) { content = new List
(num); } //去除初始可存储数据限制,使得能够导出大数据源 public ExportContent(int num) { content = new List
(); } public void SetContent(List
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()); } ///
/// 直接输出列表数据 /// ///
public List
Output() { return content; } } public class ExportHeader { private ExportContent content; public ExportHeader(int num) { content = new ExportContent(num); } public void SetHeader(List
value) { content.SetContent(value); } public void SetHeader(params string[] value) { content.SetContent(value); } public string Output(string seperate) { return content.Output(seperate); } ///
/// 直接输出Header列表 /// ///
public List
Output() { return content.Output(); } } public class ExportBody { private int ColumnNum; private List
content; public ExportBody(int num) { ColumnNum = num; content = new List
(); } public void AddBody(List
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(); } ///
/// 直接输出列表内容 /// ///
///
public List
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") ); } }

 

转载于:https://www.cnblogs.com/chxl800/p/5850462.html

你可能感兴趣的文章
token简单的使用流程。
查看>>
django创建项目流程
查看>>
Vue 框架-01- 入门篇 图文教程
查看>>
多变量微积分笔记24——空间线积分
查看>>
poi操作oracle数据库导出excel文件
查看>>
(转)Intent的基本使用方法总结
查看>>
Windows Phone开发(24):启动器与选择器之发送短信
查看>>
JS截取字符串常用方法
查看>>
Google非官方的Text To Speech和Speech Recognition的API
查看>>
stdext - A C++ STL Extensions Libary
查看>>
进程间通信系列 之 socket套接字及其实例
查看>>
天气预报插件
查看>>
Unity 游戏框架搭建 (十三) 无需继承的单例的模板
查看>>
模块与包
查看>>
apache服务器中设置目录不可访问
查看>>
嵌入式Linux驱动学习之路(十)字符设备驱动-my_led
查看>>
【NOIP模拟】密码
查看>>
java容器---------手工实现Linkedlist 链表
查看>>
three.js 性能优化的几种方法
查看>>
《梦断代码》读书笔记(三)
查看>>