首页 技术 正文
技术 2022年11月7日
0 收藏 491 点赞 545 浏览 3363 个字

之前在做列表的是总会遇到一些导出的功能,而在做导出的时候总是习惯于用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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,287