2015-07-21 53 views
0

任何人都可以指向正確的方向嗎?我不是很熟悉.NET或Javascript的人,所以這對我來說有點太難了。讓用戶以Excel電子表格的形式下載對象的Javascript array的最簡單方法是什麼?我知道將其導出爲CSV是最簡單的方法,但我確實需要將其以適當的.xlsx格式導出。據我所知,我需要使用OpenXML sdk,但沒有太多的信息(至少noob友好的信息)如何實際接近它。任何幫助表示讚賞。在ASP.NET MVC中導出Excel電子表格

回答

1

您也可以直接輸出一個基本的HTML表,並設置適當的響應頭(內容類型):

應用/ vnd.openxmlformats-officedocument.spreadsheetml.sheet

這將提示用戶打開Excel查看文件。

0

Srry,但我不能發表評論,所以我回答了你的問題...

您可以使用JavaScript的這個可能。

也許你可以使用this one

你應該這樣article過我想......

我希望這個答案將會有幫助你...

檢查這一PLS;

[1]:

$("[id$=myButtonControlID]").click(function(e) { 
 
    window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('div[id$=divTableDataHolder]').html())); 
 
    e.preventDefault(); 
 
});
table 
 
{ 
 
    border: 1px solid black;  
 
} 
 
th 
 
{ 
 
    border: 1px solid black; 
 
    padding: 5px; 
 
    background-color:skyblue; 
 
    color: white; 
 
} 
 
td 
 
{ 
 
    border: 1px solid black; 
 
    padding: 5px; 
 
    color: green; 
 
}
<button id="myButtonControlID">Export Table data into Excel</button> 
 
<div id="divTableDataHolder"> 
 
<table> 
 
    <tr><th>ColumnOne </th><th>ColumnTwo</th></tr> 
 
<tr> 
 
<td>row1ColValue1</td><td>row1ColValue2</td> 
 
</tr> 
 
<tr> 
 
<td>row2ColValue1</td><td>row2ColValue2</td> 
 
</tr> 
 
</table> 
 
</div>

0

Folloiwng是在ASP.Net MVC一個ClosedXML方法。這將有助於下載文件爲xlsx

enter image description here

GenerateExcelInvoiceController

private void GenerateExcelInvoiceController(ClosedXML.Excel.XLWorkbook workBook, string formtFileName) 
    { 

     string currentTime = DateTime.Now.ToString(); 

     string headerString = formtFileName.Replace("{0}", currentTime); 
     headerString = headerString.Replace(" ", ""); 
     Response.Clear(); 
     Response.Buffer = true; 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.AddHeader("content-disposition", headerString); 
     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      workBook.SaveAs(memoryStream); 
      memoryStream.WriteTo(Response.OutputStream); 
      memoryStream.Close(); 
     } 
     Response.End(); 
    } 

ClosedXML.Excel.XLWorkbook GetWorkBook

public static ClosedXML.Excel.XLWorkbook GetWorkBook(DataTable table, string sheetName) 
    { 
     ClosedXML.Excel.XLWorkbook workBook = new ClosedXML.Excel.XLWorkbook(); 
     ClosedXML.Excel.IXLWorksheet workSheet = workBook.Worksheets.Add(table, sheetName); 
     return workBook; 
    } 

控制器動作POST

[Authorize] 
    [HttpPost] 
    public void PracticeList(BalanceStatementModel modelReceived) 
    { 

     List<FinanceEntity> rows = GetRunningBalanceDueForPractice(); 

     DataTable table = new DataTable(); 
     using (var reader = FastMember.ObjectReader.Create(rows, "ItemDescription", "EventDate", "Amount", "RunningBalanceDue")) 
     { 
      table.Load(reader); 
     } 

     ClosedXML.Excel.XLWorkbook workBook = CommonBusinessMethodHelpers.GetWorkBook(table, "Statement"); 
     string formtFileName = "attachment;filename=\"BalanceStatement-{0}.xlsx\""; 
     GenerateExcelInvoiceController(workBook, formtFileName); 


    } 

注:它使用可FastMember librabry通過的NuGet。

參考文獻:

  1. Making OpenXML Easy with ClosedXML

  2. Use the library ClosedXml for creating Excel files