Don't talk much Code up
1. front end
let postData = { starttime: new Date(this.$moment(starttime).format('YYYY-MM-DD')), endtime: new Date(this.$moment(endtime).format('YYYY-MM-DD')), type: 2 } 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))// Pass parameters
2. Build your own right world How to operate It's better to build an entity Get an assistant or something I'll go straight to the method
#region //1 Create title /// <summary> /// Create title /// /// </summary> /// <param name="doc"></param> /// <param name="title"></param> public void SetDocTitle(XWPFDocument doc, string title) { XWPFParagraph p0 = doc.CreateParagraph();// Create paragraphs p0.Alignment = ParagraphAlignment.CENTER;// centered XWPFRun r0 = p0.CreateRun(); // Set the font r0.FontFamily = " Song style "; // Set font size r0.FontSize = 20; // Is the font bold , It's bold here r0.IsBold = true; r0.SetText(title);// Write text } /// <summary> /// establish H1 /// </summary> /// <param name="doc"></param> /// <param name="title"></param> public void SetDocH1(XWPFDocument doc, string title) { XWPFParagraph p0 = doc.CreateParagraph();// Create paragraphs p0.Alignment = ParagraphAlignment.LEFT;// It's on the left XWPFRun r0 = p0.CreateRun(); r0.IsCapitalized = true; // Set the font r0.FontFamily = " Song style "; // Set font size r0.FontSize = 16; // Is the font bold , It's bold here r0.IsBold = true; r0.SetText(title);// Write text } public void SetDocH2(XWPFDocument doc, string title) { XWPFParagraph p0 = doc.CreateParagraph();// Create paragraphs p0.Alignment = ParagraphAlignment.LEFT;// It's on the left XWPFRun r0 = p0.CreateRun(); r0.IsCapitalized = true; // Set the font r0.FontFamily = " Song style "; // Set font size r0.FontSize = 14; // Is the font bold , It's bold here r0.IsBold = true; r0.SetText(title);// Write text } public void SetTableTitle(XWPFDocument doc, string title) { XWPFParagraph p1 = doc.CreateParagraph(); p1.Alignment = ParagraphAlignment.LEFT; XWPFRun r1 = p1.CreateRun(); r1.SetText(title); } public void SetTable(XWPFDocument doc, DataTable dt) { if (dt != null && dt.Rows.Count > 0) { int rowCount = dt.Rows.Count; int colCount = dt.Columns.Count; XWPFTable table = doc.CreateTable(rowCount + 1, colCount); int colWidth = 38 / colCount; colWidth = colWidth == 0 ? 1 : colWidth; // Set width for (int i = 0; i < colCount; i++) { table.SetColumnWidth(i, (ulong)colWidth * 256);// Set the width of the column } // Fill in the first line of the form for (int i = 0; i < colCount; i++) { string colName = dt.Columns[i].ColumnName; table.GetRow(0).GetCell(i).SetText(colName); } // Fill in the form for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { string value = dt.Rows[i][j].ToString(); table.GetRow(i + 1).GetCell(j).SetText(value); } } } } #endregion
3. Back end When it's all done It's time to create a file
DateTime startDate = Convert.ToDateTime(starttime); DateTime endDate = Convert.ToDateTime(endtime); XWPFDocument docx = new XWPFDocument(); MemoryStream ms = new MemoryStream(); ExcelTools wordHelper = new ExcelTools(); //1 Set title wordHelper.SetDocTitle(docx, DateTime.Now.ToString("yyyy/MM/dd")+" Ha ha ha "); // 2 Create a subtitle wordHelper.SetDocH1(docx, " modular "); //3 Write content wordHelper.SetTableTitle(docx, " Hello "); docx.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.docx"); System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.UTF8; System.Web.HttpContext.Current.Response.ContentType = "application/ms-world"; System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray()); System.Web.HttpContext.Current.Response.End(); ms.Close(); ms.Dispose();
Finished No, you can leave a message It's relatively simple. That's