[英]Exporting Excel spreadsheets in ASP.NET MVC
Can anyone point me in the right direction? 谁能指出我正确的方向? I'm not very .NET or Javascript savvy so this is a bit too difficult for me.
我不太懂.NET或Java语言,所以这对我来说有点困难。 What's the easiest way to let a user download a Javascript
array
of objects in the form of an Excel spreadsheet? 让用户以Excel电子表格的形式下载对象的Javascript
array
的最简单方法是什么? I know exporting it to CSV is the easiest way but I really need to export it in proper .xlsx format. 我知道将其导出为CSV是最简单的方法,但是我确实需要以正确的.xlsx格式导出它。 As far as I understand, I will need to use the OpenXML sdk, but there's not much info (at least noob-friendly info) on how to actually approach it.
据我了解,我将需要使用OpenXML sdk,但是实际上没有多少信息(至少对noob友好)。 Any help is appreciated.
任何帮助表示赞赏。
You can also just output a basic HTML table and set the appropriate response headers (Content-type): 您还可以只输出基本的HTML表并设置适当的响应头(Content-type):
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application / vnd.openxmlformats-officedocument.spreadsheetml.sheet
This will prompt the user to open Excel to view the file. 这将提示用户打开Excel来查看文件。
Srry but i can't write comment so i answered your question... 抱歉,我无法发表评论,所以我回答了您的问题...
You can use JavaScripts for this maybe. 您可以为此使用JavaScript。
Maybe you can use this one 也许你可以用这个
You should this article too i think... 我想你也应该这篇文章 ...
I hope this answer will helpfull for you... 我希望这个答案对您有帮助...
Check this pls; 检查这个请;
[1]: [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>
Folloiwng is a ClosedXML approach in ASP.Net MVC. Folloiwng是ASP.Net MVC中的ClosedXML方法。 It will help download files as
xlsx
它将帮助将文件下载为
xlsx
GenerateExcelInvoiceController 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 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;
}
Controller Action for POST 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);
}
Note: It uses FastMember librabry available via NuGet. 注意:它使用可通过NuGet获得的FastMember librabry。
References: 参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.