public void Index(FormCollection fc) { string filePath = Server.MapPath("/Template/Word/w1.docx"); //using (MemoryStream ms = NPOIHelp.ExportDoc(filepath)) //{ // Response.ContentType = "application/vnd.ms-word"; // Response.ContentEncoding = Encoding.UTF8; // Response.Charset = ""; // Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("123.docx", Encoding.UTF8)); // //Response.BinaryWrite(Export().GetBuffer()); // Response.BinaryWrite(ms.GetBuffer()); // Response.End(); //} //return View(); using (MemoryStream ms = NPOIHelp.ExportWord(filePath)) { string fileName = "123" + DateTime.Now.ToString(); if (Request.Browser.Browser == "IE") { fileName = HttpUtility.UrlEncode(fileName); } //byte[] byteArray = new Byte[ms.Length]; //ms.Read(byteArray, 0, (int)ms.Length); //Response.Buffer = false; Response.Clear(); //Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document; name=" + fileName; Response.ContentType = "application/vnd.ms-word; name=" + fileName; Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".docx"); Response.BinaryWrite(ms.ToArray()); //Response.BinaryWrite(byteArray); Response.End(); } //string eFilePath = "123" + DateTime.Now.ToString(); //byte[] byteArray; //if (Request.Browser.Browser == "IE") // eFilePath = HttpUtility.UrlEncode(eFilePath); //using (FileStream fs = new FileStream(eFilePath, FileMode.Open)) //{ // byteArray = new byte[fs.Length]; // fs.Read(byteArray, 0, byteArray.Length); //} //Response.Buffer = false; //Response.Clear(); ////Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document; name=" + eFilePath; //Response.ContentType = "application/vnd.ms-word; name=" + eFilePath; //Response.AddHeader("content-disposition", "attachment;filename=" + eFilePath + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".docx"); //Response.BinaryWrite(byteArray); //Response.End(); }
public IActionResult OutToExcel() { string headStr = HttpUtility.UrlDecode(Request.Form["table_head_hid"]); string data = HttpUtility.UrlDecode(Request.Form["table_data_hid"]); string[] headArr = JsonConvert.DeserializeObject <string[]>(headStr); DataTable dt = JsonConvert.DeserializeObject <DataTable>(data); for (int i = 0; i < dt.Columns.Count; i++) { dt.Columns[i].ColumnName = headArr[i]; } MemoryStream ms = NPOIHelp.Excel_OutByDT(dt); return(File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); }
public void ToExcel(FormCollection fc) { string filePath = Server.MapPath("/Template/Excel/e1.xlsx"); using (MemoryStream ms = NPOIHelp.ExportExcel(filePath)) { string fileName = "123" + DateTime.Now.ToString(); if (Request.Browser.Browser == "IE") { fileName = HttpUtility.UrlEncode(fileName); } Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document; name=" + fileName; //Response.ContentType = "application/vnd.ms-word; name=" + fileName; Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xlsx"); Response.BinaryWrite(ms.ToArray()); Response.End(); } }