之前在做列表的是总会遇到一些导出的功能,而在做导出的时候总是习惯于用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提交导出数据的更多相关文章

  1. js模拟form提交 导出数据

    //创建模拟提交formfunction dataExport(option) { var form = $("<form method='get'></form>& ...

  2. servlet对form提交的数据进行XML转换后发送

    今天遇到一个项目,要求对form表单提交的数据进行以xml格式发送出去: 直接写XMLUtil工具类如下: package com.yfit.controller; import javax.serv ...

  3. form提交所有数据

    HTML: <div class="panel"> <div class="panel-body"> <h3>完善简历< ...

  4. form 提交数据编码梳理

    之前对form单提交的操作一直都是迷迷糊糊,知道怎么用,但是随着ajax2的出现,我们有更多的方式操作form表单提交,但是底层的原理我们要好好的做个梳理. 常见的form提交有post和get这两种 ...

  5. 导出excel时,以form方式提交json数据

    今天在写项目时写到一个excel的导出,开始想用ajax请求后台后导出,但发现ajax会有返回值,而且ajax无法直接输出文件,而后台的excel导出方法已经封装好,不方便修改. 就改用了提交的方式f ...

  6. servlet自动获取前端页面提交数据

    servlet自动获取前端页面jsp提交数据 以下是本人在学习过程中,因前端页面提交参数过多,后台servlet封装实体类过于麻烦而写的一个工具类,应用于jsp/servlet数据提交后,基于MVC+ ...

  7. django ajax提交form表单数据

    后台: from django.shortcuts import render from django.shortcuts import redirect from django.shortcuts ...

  8. ligerui_实际项目_003:form中添加数据,表格(grid)里面显示,最后将表格(grid)里的数据提交到servlet

    实现效果: "Form"中填写数据,向本页"Grid"中添加数据,转换成Json数据提交,计算总和,Grid文本框可编辑,排序 图片效果: 总结: //disp ...

  9. 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据

    jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...

随机推荐

  1. ConcurrentLinkedQueue 源码 since java1.5

    1 父类 java.lang.Object 继承者 java.util.AbstractCollection<E> 继承者 java.util.AbstractQueue<E> ...

  2. Swift 轻量级网络层设计

    前言 普遍我们的网络层设计的时候直接是如下结构APIManager.post(url, parameter,completeHandle),服务器配置在APIManager.m文件中进行配置.这样一个 ...

  3. 【SpringMVC】使用SpringMVC进行上传文件!

    写在前面: 之前在上传文件的时候,使用的是commons-file-upload这个插件,非常方便,能控制每个文件的大小,总共大小,缓存,以及支持多个文件的同时上传,但是写一次上传文件的后台代码量太大 ...

  4. 【设计模式】Bridge模式(桥接模式)

    最近的一次面试中,被问到桥接模式,以前呢并没有很仔细的研究过这个设计模式,借此机会剖析一下. 先给出自己对这个模式理解后的源码: interface A{ void methodA(); } inte ...

  5. VerilogHDL常用的仿真知识

    在描述完电路之后,我们需要进行对代码进行验证,主要是进行功能验证.现在验证大多是基于UVM平台写的systemverilog,然而我并不会sv,不过我会使用verilog进行简单的验证,其实也就是所谓 ...

  6. [COGS 1752] 摩基亚Mokia

    照例先上题面 1752. [BOI2007]摩基亚Mokia 输入文件:mokia.in   输出文件:mokia.out 时间限制:1.5 s   内存限制:128 MB [题目描述] 摩尔瓦多的移 ...

  7. Swift学习之元组(Tuple)

    定义 元组是由若干个类型的数据组成,组成元组的数据叫做元素,每个元素的类型都可以是任意的. 用法一 let tuples1 = ("Hello", "World" ...

  8. Android异步处理技术

    前言: 在移动端开发中,我们必须正确处理好主线程和子线程之间的关系,耗时操作必须在子线程中完成,避免阻塞主线程,导致ANR.异步处理技术是提高引用性能,解决主线程和子线程之间通信问题的关键. 通常在如 ...

  9. ASP.NET Core 源码学习之 Logging[2]:Configure

    在上一章中,我们对 ASP.NET Logging 系统做了一个整体的介绍,而在本章中则开始从最基本的配置开始,逐步深入到源码当中去. 默认配置 在 ASP.NET Core 2.0 中,对默认配置做 ...

  10. PDF修改器

    亲测可用的绿色版PDF修改器供大家分享使用 下载地址:http://pan.baidu.com/s/1pLPnhQb