有人能指出我正确的方向吗?我不是非常熟悉.NET或Javascript,所以这对我来说有点太难了。让用户以Excel电子表格的形式下载Javascript array
对象的最简单方法是什么?我知道将其导出为CSV是最简单的方法,但我真的需要以适当的.xlsx格式导出它。据我了解,我将需要使用OpenXML sdk,但没有太多关于如何实际接近它的信息(至少是noob友好信息)。任何帮助表示赞赏。
答案 0 :(得分:1)
您还可以输出基本HTML表并设置相应的响应标头(Content-type):
应用/ vnd.openxmlformats-officedocument.spreadsheetml.sheet
这将提示用户打开Excel以查看文件。
答案 1 :(得分:0)
Srry但是我不能写评论所以我回答了你的问题......
您可以使用JavaScripts。
也许你可以使用article
你应该这样{{3}}我认为......
我希望这个答案对你有所帮助......
检查一下;
[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>
答案 2 :(得分:0)
Folloiwng是ASP.Net MVC中的ClosedXML方法。它有助于将文件下载为xlsx
<强> 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);
}
注意:它使用NuGet提供的FastMember librabry。
<强>参考文献:强>