話不多說直接上程式碼

1.前端(個人邏輯做了Excel匯出和world匯出,world匯出會在下一個部落格中列出)

 var xhr = new XMLHttpRequest()
var url = window.SITE_CONFIG['baseUrl'] + 'Api/Arrange/ExportPerListByTimeDoc'
var filename = this.myDateType === 'DAY' ? '1.docx' : '2.xls'
xhr.open('post', url, true)
xhr.responseType = 'blob'
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8')
xhr.setRequestHeader('Authorization', 'BasicAuth123 ')
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
if (this.response.type === 'application/ms-excel') {
var eleLink = document.createElement('a')
eleLink.download = filename
eleLink.style.display = 'none'
var blob = new Blob([this.response], {
type: 'application/ms-excel'
})
eleLink.href = URL.createObjectURL(blob)
document.body.appendChild(eleLink)
eleLink.click()
document.body.removeChild(eleLink)
// showObj.loading = false
} else if (this.response.type === 'application/ms-world') {
var eleLink1 = document.createElement('a')
eleLink1.download = filename
eleLink1.style.display = 'none'
var blob1 = new Blob([this.response], {
type: 'application/ms-world'
})
eleLink1.href = URL.createObjectURL(blob1)
document.body.appendChild(eleLink1)
eleLink1.click()
document.body.removeChild(eleLink1)
}
}
}
}
xhr.send(JSON.stringify(postData))

2.後端

 string filenname = null;
try
{
string pathMsg = System.Web.HttpContext.Current.Server.MapPath("/"); //獲得應用程式根目錄所在的位置.
string path = pathMsg + @"Download";//檔案複製到的位置
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string tempaltePath = pathMsg + @"ExportTemplate/" + copyTempName; //要拷貝的模板
filenname = Path.Combine(path, expName + DateTime.Now.ToString("yyyyMMddHHmmssms") + @".xls");
FileInfo info = new FileInfo(tempaltePath);//獲得要拷貝的模板
info.CopyTo(filenname); //將模板拷貝到指定位置
XSSFWorkbook hssfworkbookDown;
using (FileStream file = new FileStream(filenname, FileMode.Open, FileAccess.Read))
{
hssfworkbookDown = new XSSFWorkbook(file);
file.Close();
}
XSSFSheet FirstSheet = (XSSFSheet)hssfworkbookDown.GetSheet(sheetName);
//這裡寫操作邏輯 我給你寫個對單元格操作的例子

XSSFCell cell1 = (XSSFCell)FirstSheet.GetRow(i + 4).CreateCell(0);
                     cell1.CellStyle.BorderBottom = BorderStyle.Thin;//設定單元格邊框
                     cell1.SetCellValue(listMsg[i].USER_NAME);//單元格賦值

                    FileStream files = new FileStream(filenname, FileMode.Create);
hssfworkbookDown.Write(files);
MemoryStream ms = new MemoryStream();
hssfworkbookDown.Write(ms);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.Charset = "utf-8";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls");
System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
System.Web.HttpContext.Current.Response.End();
files.Close();
files.Dispose();

這種寫法會自動建立一個檔案存在系統,留著備用核對啥的  也可以把建立方法去掉 直接匯出