前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#实现xlsx文件导出

C#实现xlsx文件导出

作者头像
likepoems
发布2023-03-04 17:18:41
5610
发布2023-03-04 17:18:41
举报
文章被收录于专栏:学习笔记分享

本文最后更新于 128 天前,其中的信息可能已经有所发展或是发生改变。

model 实体

代码语言:javascript
复制
namespace Test.Models.Book
{
    public class BookModel
    {
        public int BookId { get; set; }
        public string FileName { get; set; }
    }
}

controller 控制器层

代码语言:javascript
复制
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 业务逻辑层

代码语言:javascript
复制
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 数据操作层

代码语言:javascript
复制
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;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-8-17 1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • model 实体
  • controller 控制器层
  • bll 业务逻辑层
  • dal 数据操作层
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档