<经验杂谈>前端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 ...
随机推荐
- 51nod_1181:质数中的质数
题目链接 #include<bits/stdc++.h> using namespace std; typedef long long LL; const LL N=1e6; //vect ...
- 如何开始使用bootstrap
登陆Bootstrap官网:http://getbootstrap.com/ Bootstrap中的JS插件依赖于jQuery,因此jQuery要在Bootstrap之前引用 bootstrap框架初 ...
- 关于frameset的一些小总结
如果我们在一个页面需要由多个子页面一同组成,那么我们可以通过frameset标签来嵌套别的页面 例如 这样的页面布局的思路是: 1,先将页面分为上下两部分也就是 A B 2,再将B部分分为左右两部分 ...
- MapReduce笔记——技术点汇总
目录 · 概况 · 原理 · MapReduce编程模型 · MapReduce过程 · 容错机制 · API · 概况 · WordCount示例 · Writable接口 · Mapper类 · ...
- iOS-联系人应用(一)
环境:xcode6,iphone 4s simulator with iOS8.0 一.功能界面介绍 1.1 应用启动进入联系人列表页面,数据为模拟数据,来源与一个plist文件: 1.2 点击右上角 ...
- 用gensim学习word2vec
在word2vec原理篇中,我们对word2vec的两种模型CBOW和Skip-Gram,以及两种解法Hierarchical Softmax和Negative Sampling做了总结.这里我们就从 ...
- 蓝桥杯比赛javaB组练习《四平方和》
四平方和 四平方和定理,又称为拉格朗日定理:每个正整数都可以表示为至多4个正整数的平方和.如果把0包括进去,就正好可以表示为4个数的平方和. 比如:5 = 0^2 + 0^2 + 1^2 + 2^27 ...
- Nlpir Parser智能语义平台全文搜索
全文索引用于处理大文本集合,利用它人们可以在海量文本中快速获取需要的信息.全文检索系统是按照全文检索理论建立起来的用于提供全文检索服务的软件系统.一般来说,全文检索需要具备建立索引和提供查询的基本功能 ...
- log4go的日志滚动处理——适应生产环境的需要
日志处理有三类使用环境,开发环境DE,测试环境TE,生产环境PE. 前两类可以看成是一类,重要的是屏幕显示--termlog.生产环境中主要用的是socklog 和 filelog,即网络传输日志和文 ...
- 度度熊与邪恶大魔王 DP | 完全背包
Problem Description 度度熊为了拯救可爱的公主,于是与邪恶大魔王战斗起来. 邪恶大魔王的麾下有n个怪兽,每个怪兽有a[i]的生命值,以及b[i]的防御力. 度度熊一共拥有m种攻击方式 ...