保存到服務器javascript
<a class="easyui-linkbutton" href="javascript:void(0);" onclick="downloadexcel();">保存到服務器</a> function downloadexcel() { $.ajax({ //提交數據的類型 POST GET type: "POST", //提交的網址 url: "/Signup/GetDownloadEXCEL", //成功返回以後調用的函數 success: function (data) { if (data == true) { //alert("保存成功!"); } else { //alert("保存失敗!"); } }, //調用執行後調用的函數 complete: function (XMLHttpRequest, textStatus) { //alert(XMLHttpRequest.responseText); //alert(textStatus); }, //調用出錯執行的函數 error: function () { //請求出錯處理 } }); }
//說明:HSSFWorkbook 用於建立 .xls // XSSFWorkbook 用於建立 .xlsx //1.建立EXCEL中的Workbook IWorkbook myHSSFworkbook = new HSSFWorkbook(); IWorkbook myXSSFworkbook = new XSSFWorkbook(); //2.建立Workbook中的Sheet ISheet mysheetHSSF = myHSSFworkbook.CreateSheet("sheet1"); ISheet mysheetXSSF = myXSSFworkbook.CreateSheet("sheet1"); //3.建立Sheet中的Row IRow rowHSSF = mysheetHSSF.CreateRow(0); //SetCellValue有5個重載方法 bool、DateTime、double、string、IRichTextString(未演示) rowHSSF.CreateCell(0).SetCellValue(true); rowHSSF.CreateCell(1).SetCellValue(System.DateTime.Now); rowHSSF.CreateCell(2).SetCellValue(9.32); rowHSSF.CreateCell(3).SetCellValue("Hello World!"); //4.建立Row中的Cell並賦值 IRow rowXSSF = mysheetXSSF.CreateRow(0); rowXSSF.CreateCell(0).SetCellValue(false); rowXSSF.CreateCell(1).SetCellValue(System.DateTime.Now); rowXSSF.CreateCell(2).SetCellValue(9.32); rowXSSF.CreateCell(3).SetCellValue("Hello World!"); //5.保存 FileStream fileHSSF = new FileStream(@"E:\myHSSFworkbook.xls", FileMode.Create); myHSSFworkbook.Write(fileHSSF); fileHSSF.Close(); FileStream fileXSSF = new FileStream(@"E:\myXSSFworkbook.xlsx", FileMode.Create); myXSSFworkbook.Write(fileXSSF); fileXSSF.Close();
導出到客戶端: <a class="easyui-linkbutton" href='/Signup/GetDownloadEXCEL' target="_self">導出</a>java
public FileResult GetDownloadEXCEL() { //獲取list數據 ExcelClass ec = new ExcelClass(); ec.Id = "1"; ec.Title = "2"; ec.SalePrice = "3"; ec.Summary = "4"; ec.VipPrice = "5"; List<ExcelClass> list = new List<ExcelClass>(); list.Add(ec); //建立Excel文件的對象 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); //添加一個sheet NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1"); //給sheet1添加第一行的頭部標題 NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0); row1.CreateCell(0).SetCellValue("ID"); row1.CreateCell(1).SetCellValue("用戶姓名"); row1.CreateCell(2).SetCellValue("電話"); row1.CreateCell(3).SetCellValue("註冊時間"); row1.CreateCell(4).SetCellValue("邀請人ID"); row1.CreateCell(5).SetCellValue("邀請人名稱"); row1.CreateCell(6).SetCellValue("邀請人電話"); row1.CreateCell(7).SetCellValue("總積分"); row1.CreateCell(8).SetCellValue("已使用積分"); row1.CreateCell(9).SetCellValue("可用積分"); //將數據逐步寫入sheet1各個行 for (int i = 0; i < list.Count; i++) { NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1); rowtemp.CreateCell(0).SetCellValue(list[i].Id); rowtemp.CreateCell(1).SetCellValue(list[i].Title); rowtemp.CreateCell(2).SetCellValue(list[i].SalePrice); rowtemp.CreateCell(3).SetCellValue(list[i].Summary); rowtemp.CreateCell(4).SetCellValue(list[i].VipPrice); } // 寫入到客戶端 System.IO.MemoryStream ms = new System.IO.MemoryStream(); book.Write(ms); ms.Seek(0, SeekOrigin.Begin); return File(ms, "application/vnd.ms-excel", "用戶信息.xls"); }