export

  • 16
  • 0
  • 2025-06-02

export

bootstraptable

var tableinfo = $('#name-table').bootstrapTable('getData', false);

source : https://www.itxst.com/bootstrap-table-methods/izznveei.html

$.ajax({
   type: 'POST',
   url: https://domain/projectName/api/controller/action',
   data: {
tableInfo: JSON.stringify(tableinfo ),
   },
   cache: false,
   async: true,
   headers: {
       'requestverificationtoken': $('input[name="__RequestVerificationToken"]').val()
   },
   beforeSend: function () {
   },
   success: function (data) {
       if (data != null) {
           var url = '/ProjectName/DownLoad/DownLoadFile?filePath=DownloadFolder&fileName=' + data;
           document.location.href = url;
       } else {
           console.log("Fail");
       }
   },
   error: function (error) {
       if (error.status === 400) {
           SweetAlert('warning', error.responseText, 3000);
       }
   },
   complete: function (data) {
   }
});

source : https://ithelp.ithome.com.tw/articles/10230992

server :

[ValidateAntiForgeryToken]
[HttpPost]
[Route("ExportServer")]
public IActionResult ExportServer([FromForm] string title,[FromForm] string tableInfos)
{
   var tableTitles = title.Split(",", StringSplitOptions.RemoveEmptyEntries).ToList();
   var tableInfo = JsonConvert.DeserializeObject<List<TableVM>>(tableInfos);
   if (tableInfo.Count > 0 )
   {
       string fileName = $"CustomName.xlsx";
       string path = $@"{_env.ContentRootPath}/DownLoad/fileName";
       Service.GeneratTableToExcel(path, tableTitles, tableInfo);

       return Ok(fileName);
   }

   return BadRequest("查無資訊");
}

source : https://learn.microsoft.com/zh-tw/aspnet/core/mvc/models/model-binding?view=aspnetcore-9.0