我们知道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. [深入学习Redis]RedisAPI的原子性分析

    在学习Redis的常用操作时,经常看到介绍说,Redis的set.get以及hset等等命令的执行都是原子性的,但是令自己百思不得其解的是,为什么这些操作是原子性的? 原子性 原子性是数据库的事务中的 ...

  2. JavaWeb总结(一)—Servlet

    一.Servlet生命周期 1.Servlet生命周期 Serlet加载---->实例化---->服务---->销毁 2.init() Servlet容器启动时:读取web.xml配 ...

  3. SystemVerilog搭建验证平台使用DPI时遇到的问题及解决方案

    本文目的在于分享一下把DPI稿能用了的过程,主要说一下平台其他部分搭建好之后,在完成DPI相关工作阶段遇到的问题,以及解决的办法. 工作环境:win10 64bit, Questasim 10.1b ...

  4. css form表单样式清除

    开发项目中表单常用的清楚样式: 1.改变placeholder默认字体颜色 ::-webkit-input-placeholder{color: #333;} :-moz-placeholder{co ...

  5. SEO,搜索引擎优化原理方法等整体把握

    SEO 搜索算法: 全文文字 title 标签,title里面的文字 link 链接 link 链接里的文字 站点信任度 最佳实践: 一.设置title 准确的描述当前网页的内容 提高站点内title ...

  6. html 压缩工具 html-minifier

    https://github.com/kangax/html-minifier#options-quick-reference 1.参数列表 option Description Default re ...

  7. 快速认识HTML及一般标签

    HTML(Hype Text Language,超文本标记语言) <html >--开始标签 <head> 网页控制信息 <title>网页标题</title ...

  8. CF #356 div1 A. Bear and Prime 100

    题目链接:http://codeforces.com/contest/679/problem/A CF有史以来第一次出现交互式的题目,大致意思为选择2到100中某一个数字作为隐藏数,你可以询问最多20 ...

  9. mysql 优化方法

    1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ...

  10. 创建,删除DOM

    需求说明: 1.上传图片,有删除功能,可上传5张,至少上传一张 html代码如下 <div class="imgUpBox"> <div class=" ...