<經驗雜談>前端form提交導出數據
阿新 • • 發佈:2017-09-01
count ssa date ice base class appendto oid 安全
之前在做列表的是總會遇到一些導出的功能,而在做導出的時候總是習慣於用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))) + 1; Type type = typeof(T); PropertyInfo[] properties = type.GetProperties(); for (int i = 0; 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 = 0; ColIndex < properties.Length; ColIndex++) { PropertyInfo property = properties[ColIndex]; ExportAttribute attribute = property.GetCustomAttribute<ExportAttribute>(); if (attribute != null) { cells.Add(1, ColIndex + 1, attribute.Name, cellXF); } } int f = 1; for (int m = i * inv; m < mCount && m < (i + 1) * inv; m++) { f++; for (int CellIndex = 0; 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 + 1, 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 = 0; IRow headRow = sheet.CreateRow(rowIndex++); var headColIndex = 0; 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 = 0; 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提交導出數據