博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# mvc如何生成excel
阅读量:6230 次
发布时间:2019-06-21

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

  我们知道mvc可以通过js生成excel文件,同样在后台c#中一样可以生成excel文件。

  首先我们要在后台中写如下方法:

  

[HttpGet] public FileResult excel_generation(string userid,string count,string order_id,string order_name,string rec_person,string height,string shoulder_width,string sleeve_length,string phone,string weight,string breast,string upper_hip,string size,string waist,string pants_length,string age,string hip,string thigh,string price,string userrmk,string address, string imgs,string mianliaormk="",string nanyi ="",string logo="",string gencustom="",string bodyspacial="",string isredo="否",string color="原图色",string mianliaohoudu="",string mianliaotanli="")        {            var excel_text = new System.Text.StringBuilder();            //excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//订单号 excel_text.Append("
"); excel_text.Append("
");//名称 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//用户id excel_text.Append("
");//----------------------------------------新增 excel_text.Append("
");//身高 excel_text.Append("
"); excel_text.Append("
");//肩宽 excel_text.Append("
"); excel_text.Append("
");//袖长 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//收件人 excel_text.Append("
"); excel_text.Append("
");//体重 excel_text.Append("
"); excel_text.Append("
");//胸围 excel_text.Append("
"); excel_text.Append("
");//上臀围 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//联系电话 excel_text.Append("
"); excel_text.Append("
");//码数 excel_text.Append("
"); excel_text.Append("
");//腰围 excel_text.Append("
"); excel_text.Append("
");//裤长 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//个性化logo excel_text.Append("
"); excel_text.Append("
");//年龄 excel_text.Append("
"); excel_text.Append("
");//臀围 excel_text.Append("
"); excel_text.Append("
");//大腿围 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//跟单客服 excel_text.Append("
"); excel_text.Append("
");//服装价格 excel_text.Append("
"); excel_text.Append("
");//是否重复下单 excel_text.Append("
"); excel_text.Append("
");//件数 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//身体特殊说明 excel_text.Append("
"); excel_text.Append("
");//面料厚度 excel_text.Append("
"); excel_text.Append("
");//色号 excel_text.Append("
"); excel_text.Append("
");//面料弹力 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//面料要求 excel_text.Append("
"); excel_text.Append("
");//面料难易度 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//服装要求 excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
");//收件人地址 excel_text.Append("
"); excel_text.Append("
"); string[] array = imgs.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < array.Length; i++) { excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("
"); } excel_text.Append("
"); excel_text.Append("
"); excel_text.Append("

订单信息表

"); excel_text.Append("

" + "订单号" + "

'" + order_id + "

" + "名称" + "

" + order_name + "

" + "用户id" + "

" + userid + "

" + "身高" + "

" + height + "

" + "肩宽" + "

" + shoulder_width + "

" + "袖长" + "

" + sleeve_length + "

" + "收件人" + "

" + rec_person + "

" + "体重" + "

" + weight + "

" + "胸围" + "

" + breast + "

" + "上臀围" + "

" + upper_hip + "

" + "联系电话" + "

" + phone + "

" + "码数" + "

" + size + "

" + "腰围" + "

" + waist + "

" + "裤长" + "

" + pants_length + "

" + "个性化logo" + "

" + "年龄" + "

" + age + "

" + "臀围" + "

" + hip + "

" + "大腿围" + "

" + thigh + "

" + "跟单客服" + "

" + gencustom + "

" + "服装价格" + "

" + price + "

" + "胚衣寄售" + "

" + isredo + "

" + "件数" + "

" + count + "

" + "身体特殊说明" + "

" + bodyspacial + "

" + "面料厚度" + "

" + mianliaohoudu + "

" + "颜色" + "

" + color + "

" + "面料弹力" + "

" + mianliaotanli + "

" + "面料要求" + "

" + mianliaormk + "

" + "面料难易度" + "

" + nanyi + "

" + "服装要求" + "

" + userrmk + "

" + "收件人地址" + "

" + address + "

"); string filename = order_id.Trim() + "-" + rec_person.Trim() +"-" + order_name.Trim() + "的订单.xls"; byte[] fileContents = System.Text.Encoding.Default.GetBytes(excel_text.ToString()); return File(fileContents, "application/ms-excel", filename); }

  我们看到,在c#中导出excel,这种方法可以通过画html中table的方式做出excel,而这种方法导出的表格是一个简单表格,在排版上还需要更多的斟酌。

  在前台的时候,务必要注意,不可使用ajax,可以通过使用使用localtion.href="/控制器/excel_generation?参数=参数值"的方法导出表格。

转载于:https://www.cnblogs.com/lsnct/p/6165871.html

你可能感兴趣的文章
自定义maven变量以及maven内置常量
查看>>
ChemDraw 15.1 Pro插入阿尔法可以这样做
查看>>
liunx下安装JDK
查看>>
如何有效减少测试用例数目
查看>>
Win10 + VS2015 下编译 Qt5.6.0
查看>>
js关闭当前页面(窗口)的几种方式总结
查看>>
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide
查看>>
前端模拟API数据的两种方式
查看>>
Django Rest Framework remove csrf
查看>>
linux 学习笔记 软件包管理
查看>>
HTML5学习之三:文件与拖放
查看>>
41. 包含min函数的栈
查看>>
07-linux基础四-系统监控和硬盘分区
查看>>
Struts2之上传
查看>>
centos7 修改 PATH环境变量(注意,不是添加!!!TMD)
查看>>
Fetch from Upstream 变灰失效
查看>>
唉,都是一辈子呀…
查看>>
pmwiki制作网页的基本操作
查看>>
织梦开发——相关阅读likeart应用
查看>>
SQL2008-表对表直接复制数据
查看>>