<经验杂谈>前端form提交导出数据
之前在做列表的是总会遇到一些导出的功能,而在做导出的时候总是习惯于用get的方法将参数放在url上,这样一来就会有很多的弊端,一是url的参数长度有限,遇到有的参数很长的时候就会报错,二是也不太安全。
按照之前写法:
var url = '@Url.Action("")';
window.open(url, "_blank");
现在改成前端form提交的方式:
function doExport() {
getCards();
var element = '<form action="'+url+" target="_self" method="post">'
+ '<input type="text" name="StartDate" value="' + vm.searchReportParam.StartDate + '" />'
+ '<input type="text" name="EndDate" value="' + vm.searchReportParam.EndDate + '" />'
+ '<input type="text" name="CardIdsStr" value="' + vm.CardIdsStr + '" />'
+ '</form>';
$(element).appendTo('body').submit().remove();
};
后端数据处理:
public static void ToExcel<T>(List<T> datas, int SheetRows, string exportName, HttpResponseBase response)
{
AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument(); doc.FileName = exportName + ".xls";
string SheetName = string.Empty;
//记录条数
int mCount = datas.Count; //每个SHEET的数量
int inv = SheetRows;
//计算当前多少个SHEET
int k = Convert.ToInt32(Math.Round(Convert.ToDouble(mCount / inv))) + ; Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties(); for (int i = ; i < k; i++)
{
SheetName = "数据表" + i.ToString();
AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add(SheetName);
AppLibrary.WriteExcel.Cells cells = sheet.Cells; //创建列样式创建列时引用
XF cellXF = doc.NewXF();
cellXF.VerticalAlignment = VerticalAlignments.Centered;
cellXF.HorizontalAlignment = HorizontalAlignments.Centered;
cellXF.Font.FontFamily = FontFamilies.Roman;//设置字体 默认为宋体 for (int ColIndex = ; ColIndex < properties.Length; ColIndex++)
{ PropertyInfo property = properties[ColIndex];
ExportAttribute attribute = property.GetCustomAttribute<ExportAttribute>();
if (attribute != null)
{
cells.Add(, ColIndex + , attribute.Name, cellXF);
} }
int f = ;
for (int m = i * inv; m < mCount && m < (i + ) * inv; m++)
{
f++;
for (int CellIndex = ; CellIndex < properties.Length; CellIndex++)
{
ExportAttribute attribute = properties[CellIndex].GetCustomAttribute<ExportAttribute>();
if (attribute != null)
{
object value = properties[CellIndex].GetValue(datas[m]);
if (properties[CellIndex].PropertyType == typeof(DateTime))
{
value = ((DateTime)value).ToString("yyyy/MM/dd");
}
cells.Add(f, CellIndex + , value, cellXF); }
}
}
} doc.Send();
response.Flush();
response.End();
}
使用插件NPOI来生成EXCEL:
private static HttpResponseMessage GetExcelResponse(List<T> models)
{ HSSFWorkbook book = new HSSFWorkbook();
ISheet sheet = book.CreateSheet("Sheet1"); int rowIndex = ;
IRow headRow = sheet.CreateRow(rowIndex++);
var headColIndex = ;
headRow.CreateCell(headColIndex++).SetCellValue("rows1");
headRow.CreateCell(headColIndex++).SetCellValue("rows2");
headRow.CreateCell(headColIndex++).SetCellValue("rows3");
headRow.CreateCell(headColIndex++).SetCellValue("rows4");
headRow.CreateCell(headColIndex++).SetCellValue("rows5");
foreach (var model in models)
{
IRow row = sheet.CreateRow(rowIndex++);
var colIndex = ;
row.CreateCell(colIndex++).SetCellValue(model.CardName);
row.CreateCell(colIndex++).SetCellValue(model.Code);
row.CreateCell(colIndex++).SetCellValue((double)model.ItemPrice);
row.CreateCell(colIndex++).SetCellValue((double)model.CostPriceTotal);
row.CreateCell(colIndex++).SetCellValue(model.OrderCode);
}
var ms = new MemoryStream();
book.Write(ms);
ms.Position = 0L; HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
ms.Position = 0L;
response.Content = new StreamContent(ms);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.ms-excel");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"导出{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls"
};
return response;
}
<经验杂谈>前端form提交导出数据的更多相关文章
- js模拟form提交 导出数据
//创建模拟提交formfunction dataExport(option) { var form = $("<form method='get'></form>& ...
- servlet对form提交的数据进行XML转换后发送
今天遇到一个项目,要求对form表单提交的数据进行以xml格式发送出去: 直接写XMLUtil工具类如下: package com.yfit.controller; import javax.serv ...
- form提交所有数据
HTML: <div class="panel"> <div class="panel-body"> <h3>完善简历< ...
- form 提交数据编码梳理
之前对form单提交的操作一直都是迷迷糊糊,知道怎么用,但是随着ajax2的出现,我们有更多的方式操作form表单提交,但是底层的原理我们要好好的做个梳理. 常见的form提交有post和get这两种 ...
- 导出excel时,以form方式提交json数据
今天在写项目时写到一个excel的导出,开始想用ajax请求后台后导出,但发现ajax会有返回值,而且ajax无法直接输出文件,而后台的excel导出方法已经封装好,不方便修改. 就改用了提交的方式f ...
- servlet自动获取前端页面提交数据
servlet自动获取前端页面jsp提交数据 以下是本人在学习过程中,因前端页面提交参数过多,后台servlet封装实体类过于麻烦而写的一个工具类,应用于jsp/servlet数据提交后,基于MVC+ ...
- django ajax提交form表单数据
后台: from django.shortcuts import render from django.shortcuts import redirect from django.shortcuts ...
- ligerui_实际项目_003:form中添加数据,表格(grid)里面显示,最后将表格(grid)里的数据提交到servlet
实现效果: "Form"中填写数据,向本页"Grid"中添加数据,转换成Json数据提交,计算总和,Grid文本框可编辑,排序 图片效果: 总结: //disp ...
- 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据
jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...
随机推荐
- MySQL5.6.36 linux rpm包安装配置文档
一.卸载自带mysql,删除MySQL的lib库,服务文件 [root@localhost ~]#rpm -qa|grep mysql qt-mysql-4.6.2-26.el6_4.x86_64 m ...
- Java IO 之 BIO、NIO、AIO
1.BIO.NIO.AIO解释 Java BIO : 同步并阻塞 (Blocking IO) 一个连接一个线程 即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情会造成不 ...
- 禁止UIWebView随键盘的弹起而往上滚动
问题:当UIWebView中的html有输入框,点击输入框,UIWebView会随键盘的弹起而整体往上移动,收起键盘后,UIWebView无法回到原来的位置; 问题的原因:由于UIWebView继承的 ...
- 回味Python2.7——笔记4
一.Python 标准库概览 1.操作系统接口 os 模块提供了很多与操作系统交互的函数: >>> import os >>> os.getcwd() # Retu ...
- tensorflow dropout函数应用
1.dropout dropout 是指在深度学习网络的训练过程中,按照一定的概率将一部分神经网络单元暂时从网络中丢弃,相当于从原始的网络中找到一个更瘦的网络,这篇博客中讲的非常详细 2.tens ...
- RxSwift 实战操作【注册登录】
前言 看了前面的文章,相信很多同学还不知道RxSwift该怎么使用,这篇文件将带领大家一起写一个 注册登录(ps:本例子采用MVVM)的例子进行实战.本篇文章是基于RxSwift3.0写的,采用的是C ...
- (转)Linux端口nmap和netstat命令
场景:弄不清楚端口和服务的关系,总觉得这个命令很有用但是还不清楚如何使用 1 linux端口与服务 1.1 安全概述 网络传输安全.操作系统安全.应用软件安全构成了整个网络应用的安全:其中应用软件安全 ...
- (转)HTTP协议漫谈
HTTP协议漫谈 简介 园子里已经有不少介绍HTTP的的好文章.对HTTP的一些细节介绍的比较好,所以本篇文章不会对HTTP的细节进行深究,而是从够高和更结构化的角度将HTTP协议的元素进行分类讲 ...
- Verilog HDL常用综合语法
前面已经记录了一些组成Verilog的基本组成,可以用这些基本组成来构成表达式.这一节,就来记录一下把这些表达式构成一个文件的各种行为描述语句. ①这里用Verilog基本要素进行的行为描述主要是针对 ...
- ubuntu下升级网卡驱动
ubuntu下升级网卡驱动 无线局域网环境下,有个笔记本儿的无线经常断,而其它的终端都好好的,唯独它不行.所以想到检查和更新下无线网卡的驱动看看.以下是操作流程,记录一下. 阅读说明:##为标签, / ...