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 ...
随机推荐
- UWP: 掌握编译型绑定 x:Bind
在 UWP 开发中,我们在进行数据绑定时,除了可以使用传统的绑定 Binding,也可以使用全新的 x:Bind,由于后者是在程序编译时进行初始化操作(不同于 Binding,它是在运行时创建.初始化 ...
- List分组 用于客服对话分组场景
工作用可能会用到会话分组: Message是消息实体对象,里面有toId和fromId 指明接收方ID和发送方Id,通过组合形式"12-22-" 为map的key public M ...
- JavaScript高级程序设计---学习笔记(五)
1.2D上下文 1)填充与描边 填充和描边的两个操作取决于两个属性:fillStyle和strokeStyle.两个属性的值可以是字符串.渐变对象或模式对象,默认值都是#000000 例: html: ...
- vue实现全选效果
vue实现全选效果 接触vue快半年了,记得刚用vue做项目的时候遇到一个全选功能,当时到处百度也没有找到怎么实现,最后还是用了jquery进行dom操作实现的. 今天没事就顺手写了一个,感觉很简单, ...
- bootstrap table分页后刷新跳到第一页
之前这样写是不行的,这时候页数还是原来的页数 $('#tb_departments').bootstrapTable(('refresh')); 需要改成: $("#tb_departmen ...
- javaWeb项目(SSH框架+AJAX+百度地图API+Oracle数据库+MyEclipse+Tomcat)之一 基础Struts框架搭建篇
即将开始着手写这个项目,所以希望通过这篇博客来记录自己学习的过程 今天开学第一天,就上了软件工程实践课,自己也开始着手做这个大作业了.首先我的项目名称叫做智能班车管理系统. 项目的概况: 该软件产品是 ...
- [ext4]空间管理 - 分配机制
在Ext4系统中,存在很多分配策略,比如预分配.多块分配.延迟分配等 Prealloc预分配 在ext4系统中,对于小文件和大文件的空间申请请求,都有不同的分配策略.对用小文件的空间请求,e ...
- 通过 bootloader 向其传输启动参数
作者:Younger Liu, 本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可. Linux提供了一种通过bootloader向其传输启动参数的功能,内核开发 ...
- PHP 安装 Xdebug 扩展(一)
一.前言 1. Xdebug 简介 Xdebug 是一个开放源代码的 PHP 程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况.当前最新版本为 Xdebug 2.5.0 ...
- PDO(数据访问抽象层)、pdo事务功能和预处理功能---2017-05-05
之前所学的数据访问都是用mysqli做成类来访问的,但是mysqli这个类只是针对mysql这个数据库的:那么如果访问其他类型的数据库呢? 那么这就用到了PDO(数据访问抽象层). 一.关于PDO基本 ...