<经验杂谈>前端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 ...
随机推荐
- 常用PHP函数
md5_file() 生成md5 $zip = new \ZipArchive(); if($zip->open($savepath.$key) === TRUE){ $zip ->ext ...
- 关于启动文件分析的(MDK-ARM) 【转】
;******************** (C) COPYRIGHT 2010 STMicroelectronics ******************** ;* File Name : star ...
- poj_3122:Pie(二分)
不算难的一道二分..各种玄学错误..eps小了T,大了WA..最后G++改成C++提交就AC了.. #include<iostream> #include<cstdio> #i ...
- Carthage的安装和使用
为什么要使用Carthage CocoaPods是已存在很长时间的Cocoa依赖管理器, 那么为什么要创建Carthage呢? CoaoaPods是一套整体解决方案,我们在Podfile中指定好我们需 ...
- maven:pom.xml中没有dependency标签错误
dependency的标签是包含在dependencies中的.
- UVALive - 5107 - A hard Aoshu Problem
题目链接:https://vjudge.net/problem/UVALive-5107 题目大意:用ABCDE代表不同的数字,给出形如ABBDE___ABCCC = BDBDE的东西: 空格里面可以 ...
- jar包的一生
经常会头疼于一个jar包是如何制作的,包括maven的打包方式,springboot的打jar包的原理,jar包稍稍有错误就会完全无法运行.在网上折腾了很久终于有些思路和步骤,在这里做个笔记 本文大纲 ...
- 【CSS】less 学习小结
1. less 使用 less 可直接使用浏览器解析 or 使用node 的grunt/gulp 解析成传统css . 推荐开发环境直接使用less 文件调试, 生产环境部署解析好的css 2. l ...
- 3.如何安装Apache Spark
如何安装Apache Spark 1 Why Apache Spark 2 关于Apache Spark 3 如何安装Apache Spark 4 Apache Spark的工作原理 5 spark弹 ...
- OOP in Javascript
写了几篇Vue入门的内容了,今天写点其它的放松一下,简单讲讲javascript中的面相对象. 在面向对象的语言中,都有类的概念,当然es6中开始javascript中也有类的概念了,这里以es5为基 ...