C#实现xlsx文件导出
本文最后更新于 757 天前,其中的信息可能已经有所发展或是发生改变。

model 实体

namespace Test.Models.Book
{
    public class BookModel
    {
        public int BookId { get; set; }
        public string FileName { get; set; }
    }
}

controller 控制器层

using Test.BLL.Book;
using Test.Models.Book;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Test.Controllers.Book
{
    [Route("api/book/[controller]/[action]")]
    [ApiController]

    public class Book : Controller
    {
        [HttpPost]
        public ActionResult ExportBookData([FromBody] BookModel model)   // 下载文件
        {
            BookDal bll = new BookDal();
            DataSet ds =  bll.ExportBookData(model.BookId);
            Dictionary<string, List<ExcelColumn>> dc = new Dictionary<string, List<ExcelColumn>>();
            ds.Tables[0].TableName = "sheet1";
            List<ExcelColumn> listColumns = bll.ExportBookColumns(); // xlsx文件的表头
            dc.Add("sheet1", listColumns);
            MemoryStream ms = new ExcelUtils().GetStreamByData(ds, dc);
            byte[] buffer = ms.ToArray();  //转化为byte格式存储
            MemoryStream ms1 = new MemoryStream(buffer);
            return File(ms1, System.Net.Mime.MediaTypeNames.Application.Octet, model.FileName);
        }
    }
}

bll 业务逻辑层

using Test.DAL.Book;
using Test.Models.Book;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;

namespace Test.BLL.Book
{
    public class Book
    {
        BookDal bll = new BookDal();

        public DataSet ExportBookData(int BookId) // 导出的数据
        {
            return bll.ExportBookData(BookId);
        }

        public List<ExcelColumn> ExportBookColumns() // 导出的表头
        {
            return bll.ExportBookColumns();
        }
    }
}

dal 数据操作层

using Test.BLL;
using Test.Models.Book;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;

namespace Test.DAL.Book
{
    public class Book
    {
        public DataSet ExportBookData(int BookId)
        {
            IDbConnection sqlconn = SqlUtil.SqlConnection(ConfigUtil.GetSysSettings().Book);
            try
            {
                string sql = @"SELECT * FROM book WHERE BookId = @BookId"
                sqlconn.Open();
                SqlCommand sqlcomm = new SqlCommand();
                sqlcomm.CommandType = CommandType.Text;
                sqlcomm.Connection = sqlconn;
                sqlcomm.CommandText = sql;
                sqlcomm.Parameters.Add(new SqlParameter("@BookId", BookId));
                SqlDataAdapter sqlda = new SqlDataAdapter(sqlcomm);
                DataSet ds = new DataSet();
                sqlda.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("查询失败");
            }
            finally
            {
                sqlconn.Close();
            }
        }

        public List<ExcelColumn> ExportBookColumns()
        {
            string[] columns =
            {
                "BookId"
                ,"BookName"
                ,"BookDetail"
                ,"BookDate"
                ,"SaleNumber"
                ,"BookPrice"
                ,"SaleCity"
            };
            List<ExcelColumn> listColumns = new List<ExcelColumn>();
            foreach (var column in columns)
            {
                string headName = column;
                switch (column)
                {
                    case "BookId":
                        headName = "BookId";
                        break;
                    case "BookName":
                        headName = "BookName";
                        break;
                    case "BookDetail":
                        headName = "BookDetail";
                        break;
                    case "BookDate":
                        headName = "BookDate";
                        break;
                    case "SaleNumber":
                        headName = "SaleNumber";
                        break;
                    case "BookPrice":
                        headName = "BookPrice";
                        break;
                    case "SaleCity":
                        headName = "SaleCity";
                        break;
                }
                listColumns.Add(new ExcelColumn(headName, column, 200));
            }
            return listColumns;
        }
    }
}
本文链接:https://likepoems.com/articles/cshap-implements-xlsx-file-export/
转载说明:本站文章若无特别说明,皆为原创,转载请注明来源:likepoems,谢谢!^^
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇