我们知道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("<table width='400 height='392' border='1' cellpadding='1'>");
excel_text.Append("<table border='1' cellpadding='1'>");
excel_text.Append("<tbody>");
excel_text.Append("<tr>");
excel_text.Append("<td height='44' colspan='8'><div align='center'>");
excel_text.Append("<h2><strong>订单信息表</strong></h2>");
excel_text.Append("</div></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "订单号" + "</p></strong></td>");//订单号
excel_text.Append("<td><p align='center'>'" + order_id + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "名称" + "</p></strong></td>");//名称
excel_text.Append("<td colspan='5'><p align='center'>" + order_name + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28' width='106'><strong><p align='center'>" + "用户id" + "</p></strong></td>");//用户id
excel_text.Append("<td width='184'><p align='center'>" + userid + "</p></td>");//----------------------------------------新增
excel_text.Append("<td width='106'><strong><p align='center'>" + "身高" + "</p></strong></td>");//身高
excel_text.Append("<td width='78'><p align='center'>" + height + "</p></td>");
excel_text.Append("<td width='82'><strong><p align='center'>" + "肩宽" + "</p></strong></td>");//肩宽
excel_text.Append("<td width='78'><p align='center'>" + shoulder_width + "</p></td>");
excel_text.Append("<td width='79'><strong><p align='center'>" + "袖长" + "</p></strong></td>");//袖长
excel_text.Append("<td width='81'><p align='center'>" + sleeve_length + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "收件人" + "</p></strong></td>");//收件人
excel_text.Append("<td><p align='center'>" + rec_person + "</p></td>"); excel_text.Append("<td><strong><strong><p align='center'>" + "体重" + "</p></strong></td>");//体重
excel_text.Append("<td><p align='center'>" + weight + "</p></td>");
excel_text.Append("<td><strong><strong><p align='center'>" + "胸围" + "</p></strong></td>");//胸围
excel_text.Append("<td><p align='center'>" + breast + "</p></td>");
excel_text.Append("<td><strong><strong><p align='center'>" + "上臀围" + "</p></strong></td>");//上臀围
excel_text.Append("<td><p align='center'>" + upper_hip + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "联系电话" + "</p></strong></td>");//联系电话
excel_text.Append("<td><p align='center'>" + phone + "</p></td>"); excel_text.Append("<td><strong><p align='center'>" + "码数" + "</p></strong></td>");//码数
excel_text.Append("<td><p align='center'>" + size + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "腰围" + "</p></strong></td>");//腰围
excel_text.Append("<td><p align='center'>" + waist + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "裤长" + "</p></strong></td>");//裤长
excel_text.Append("<td><p align='center'>" + pants_length + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "个性化logo" + "</p></strong></td>");//个性化logo
excel_text.Append("<td align='center'><img src='" + logo + "' height='28'/></td>"); excel_text.Append("<td><strong><p align='center'>" + "年龄" + "</p></strong></td>");//年龄
excel_text.Append("<td><p align='center'>" + age + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "臀围" + "</p></strong></td>");//臀围
excel_text.Append("<td><p align='center'>" + hip + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "大腿围" + "</p></strong></td>");//大腿围
excel_text.Append("<td><p align='center'>" + thigh + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "跟单客服" + "</p></strong></td>");//跟单客服
excel_text.Append("<td><p align='center'>" + gencustom + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "服装价格" + "</p></strong></td>");//服装价格
excel_text.Append("<td><p align='center'>" + price + "</p></td>"); excel_text.Append("<td><strong><p align='center'>" + "胚衣寄售" + "</p></strong></td>");//是否重复下单
excel_text.Append("<td><p align='center'>" + isredo + "</p></td>"); excel_text.Append("<td><strong><p align='center'>" + "件数" + "</p></strong></td>");//件数
excel_text.Append("<td><p align='center'>" + count + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "身体特殊说明" + "</p></strong></td>");//身体特殊说明
excel_text.Append("<td><p align='center'>" + bodyspacial + "</p></td>");
excel_text.Append("<td ><strong><p align='center'>" + "面料厚度" + "</p></strong></td>");//面料厚度
excel_text.Append("<td><p align='center'>" + mianliaohoudu + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "颜色" + "</p></strong></td>");//色号
excel_text.Append("<td><p align='center'>" + color + "</p></td>");
excel_text.Append("<td><strong><p align='center'>" + "面料弹力" + "</p></strong></td>");//面料弹力
excel_text.Append("<td><p align='center'>" + mianliaotanli + "</p></td>");
excel_text.Append("</tr>");
excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "面料要求" + "</p></strong></td>");//面料要求
excel_text.Append("<td colspan='3'><p align='center'>" + mianliaormk + "</p></td>");
excel_text.Append("<td colspan='2'><strong><p align='center'>" + "面料难易度" + "</p></strong></td>");//面料难易度
excel_text.Append("<td colspan='2'><p align='center'>" + nanyi + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='68'><strong><p align='center'>" + "服装要求" + "</p></strong></td>");//服装要求
excel_text.Append("<td colspan='7'><p align='center'>" + userrmk + "</p></td>");
excel_text.Append("</tr>"); excel_text.Append("<tr>");
excel_text.Append("<td height='28'><strong><p align='center'>" + "收件人地址" + "</p></strong></td>");//收件人地址
excel_text.Append("<td colspan='7'><p align='center'>" + address + "</p></td>");
excel_text.Append("</tr>"); string[] array = imgs.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
for (int i = ; i < array.Length; i++)
{
excel_text.Append("<tr>");
excel_text.Append("<td height='310' colspan='8' align='center'><img src='" + array[i] + "' height='310'/></td>");
excel_text.Append("</tr>");
} excel_text.Append("</tbody>");
excel_text.Append("</table>"); 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?参数=参数值"的方法导出表格。

c# mvc如何生成excel的更多相关文章

  1. asp.net mvc NPOI 生成Excel文件

    private string PushToDown(string addtime) { DataTable dt = _bCreateCode.PushtoExcel(addtime); //1.实例 ...

  2. Spring MVC-集成(Integration)-生成Excel示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_excel.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显示 ...

  3. Asp.net MVC 简单实现生成Excel并下载

    由于项目上的需求,需要导出指定条件的Excel文件.经过一翻折腾终于实现了. 现在把代码贴出来分享 (直接把我们项目里面的一部份辅助类的代码分享一下) 我们项目使用的是Asp.Net MVC4.0模式 ...

  4. ASP.NET MVC 4.0 中使用NPOI 2.2.0 按模板生成Excel报表

    使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/ ...

  5. 导出数据生成Excel(MVC)

    /// <summary> /// 生成Excel /// </summary> /// <returns></returns> public File ...

  6. .net生成Excel,并下载

    生成Excel的方式有很多种,这里记录两个最简单的: 1.将数据保存为html,然后输出到客户端,保存为Excel文件: 2.通过\t\n生成字符串,然后输出到客户端,保存为Excel. 以上两者的原 ...

  7. Echarts 数据视图 生成Excel的方法

    一.生成Excel,两大方向:1后台生成Excel 查询数据库,使用NOPI生成Excel.2前台js生成Excel三种方式1)jquery.table2excel.js --采用,优势:兼容IE和C ...

  8. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  9. Spring MVC 实例:Excel视图的使用

    对于Excel而言,Spring MVC所推荐的是使用AbstractXlsView,它实现了视图接口,从其命名也可以知道它只是一个抽象类,不能生成实例对象.它自己定义了一个抽象方法——buildEx ...

随机推荐

  1. 老李分享:https协议

    老李分享:https协议   最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如果你访问 google,查看一下地址栏 ...

  2. scrollWidth,offsetWidth,clientWidth,width;scrollHeight,offsetHeight,clientHeight,height;offsetTop,scrollTop,top;offsetLeft,scrollLeft,left还有谁

    题中的那么多属性让人头都大了,他们到底是什么意思?不同浏览器的实现是一样的吗?以下所有结论来自chrome版本 53.0.2785.89 (64-bit)和firefox版本52.0.2,操作系统ub ...

  3. C#设计模式:责任链模式

    设计模式是面向对象编程的基础,是用于指导程序设计.在实际项目开发过程中,并不是一味将设计模式进行套用,也不是功能设计时大量引入设计模式.应该根据具体需求和要求应用适合的设计模式.设计模式是一个老话题了 ...

  4. R语言生成随机数

    1.概述 作为一种语言进行统计分析,R有一个随机数生成各种统计分布功能的综合性图书馆.R语言可以针对不同的分布,生成该分布下的随机数.其中,有许多常用的个分布可以直接调用.本文简单介绍生成常用分布随机 ...

  5. UPS不间断电源网络功能介绍

    UPS不间断电源网络功能介绍 随着联网技术的善及,用户又向UPS提出了更高的要求:ups电源应具有无人値守的功铅酸蓄电池能,不但具有自检的功能,还应具有联网功能,在网上UPS不间断电源可以随时观察up ...

  6. 网站启用gzip压缩

    gzip压缩启用不启用还是要看实际情况的,启用gzip后可以相应的减轻带宽压力但是同时也会增加cpu的压力(压缩解压),相反的如果不启用那么cpu压力也会相应的减少,具体情况具体分析. Linux开启 ...

  7. 一道CVTE前端二面笔试题

    题目:给你一个数组,输出数组中出现次数第n多的数字; 比如:[1,1,1,2,2,2,3,3,4,4,5,5,6,6,7]; 1---3次 2---3次 3---2次 4---2次 5---2次 6- ...

  8. superslide2插件

    地址:http://www.superslide2.com/ 做自适应要注意该宽度和高度 等比缩放

  9. tomcat的常用配置

    1.解決get请求的中文乱码问题 解决办法: 首先找到tomcat路径下的apache-tomcat-7.0.52\conf文件夹,打开server.xml文件,编辑如下内容: <Connect ...

  10. 进程间通信系列 之 socket套接字及其实例

    进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685  进程间通信系列 之 共享内存及其实例   ...