c# mvc如何生成excel
我们知道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的更多相关文章
- asp.net mvc NPOI 生成Excel文件
private string PushToDown(string addtime) { DataTable dt = _bCreateCode.PushtoExcel(addtime); //1.实例 ...
- Spring MVC-集成(Integration)-生成Excel示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_excel.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显示 ...
- Asp.net MVC 简单实现生成Excel并下载
由于项目上的需求,需要导出指定条件的Excel文件.经过一翻折腾终于实现了. 现在把代码贴出来分享 (直接把我们项目里面的一部份辅助类的代码分享一下) 我们项目使用的是Asp.Net MVC4.0模式 ...
- ASP.NET MVC 4.0 中使用NPOI 2.2.0 按模板生成Excel报表
使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写.NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/ ...
- 导出数据生成Excel(MVC)
/// <summary> /// 生成Excel /// </summary> /// <returns></returns> public File ...
- .net生成Excel,并下载
生成Excel的方式有很多种,这里记录两个最简单的: 1.将数据保存为html,然后输出到客户端,保存为Excel文件: 2.通过\t\n生成字符串,然后输出到客户端,保存为Excel. 以上两者的原 ...
- Echarts 数据视图 生成Excel的方法
一.生成Excel,两大方向:1后台生成Excel 查询数据库,使用NOPI生成Excel.2前台js生成Excel三种方式1)jquery.table2excel.js --采用,优势:兼容IE和C ...
- 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的路由方案,与原来的方案在使用上差别不 ...
- Spring MVC 实例:Excel视图的使用
对于Excel而言,Spring MVC所推荐的是使用AbstractXlsView,它实现了视图接口,从其命名也可以知道它只是一个抽象类,不能生成实例对象.它自己定义了一个抽象方法——buildEx ...
随机推荐
- android Instrumentoation 问答
android Instrumentoation 问答 1.instrumentation是执行application instrumentation代码的基类.当应用程序运行的时候instrum ...
- 老李分享:jvm垃圾回收
老李分享:jvm垃圾回收 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478 ...
- 移动开发必须要弄明白的问题】详解Eclipse转Android Studio
2015-12-09 13:01:244264浏览3评论 AS出来一年多了,最近才从Eclipse转到AS,但我并不觉得使用Eclipse有多落后,它们都只是一个工具而已,哪个顺手就用哪个,用得好都能 ...
- Maven项目搭建(三):Maven直接部署项目
上一章给大家讲解了如何使用Maven搭建SSM框架项目. 这次给大家介绍一下怎么使用Maven直接部署项目. Maven直接部署项目 1.新建系统变量CATALINA_HOME,值为:Tom ...
- AngularJS1.X学习笔记2-数据绑定
上一篇从整体上认识了Angular,从现在开始更加深入的学习Angular的特性.本次学习的是数据绑定.应该所有的MVC框架都会用到数据绑定,比如我所知道的ThinkPHP.struts等,只有实现了 ...
- c/c++程序员的技术栈
在当今的互联网时代, java, 安卓, ios, 大行其道,而c/c++却显得越来越落寞. 其实这并不是c/c++程序员本身的问题,而是这是一个产品快速响应市场的年代.用过c/c++的人都知道, ...
- ArcGIS API for JavaScript 4.2学习笔记[20] 使用参数查询要素(油井和地震关系)
这个例子相当复杂.我先简单说说这个例子是干啥的. 在UI上,提供了一个下拉框.两个滑动杆,以确定三个参数,使用这三个参数进行空间查询.这个例子就颇带空间查询的意思了. 这个例子是干嘛的呢?第一个参数是 ...
- 什么东西那么吸引别人的眼球!! -----------------------------------for循环
认识for循环结构 在编码过程中,把一些重复执行代码采用循环结构进行描述,可以大大减化编码工作, 使得代码更加简洁.宜都... 1. 为什么要用for? 比如: 老师叫小明统计全班人的编号,小 ...
- 《Algorithms Unlocked》读书笔记1——循环和递归
<Algorithms Unlocked>是 <算法导论>的合著者之一 Thomas H. Cormen 写的一本算法基础. 书中没有涉及编程语言,直接用文字描述算法,我用 J ...
- 图解函数重载以及arguments