将html表格导出到asp.net core中的excel文件
我想将视图的html表转换为excel格式文件。 诸如此类的事:将html表格导出到asp.net core中的excel文件,html,asp.net-core,html-table,export,export-to-excel,Html,Asp.net Core,Html Table,Export,Export To Excel,我想将视图的html表转换为excel格式文件。 诸如此类的事: @model DataTable <table class="table table-hover table-responsive table-striped"> <thead> <tr > @foreach (DataColumn col in Model.Columns) { <th class=&q
@model DataTable
<table class="table table-hover table-responsive table-striped">
<thead>
<tr >
@foreach (DataColumn col in Model.Columns)
{
<th class="border-white">@col.Caption</th>
}
</tr>
</thead>
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
@foreach (DataColumn col in Model.Columns)
{
<td class="border-white">@row[col.ColumnName]</td>
}
</tr>
}
</tbody>
@model数据表
@foreach(Model.Columns中的数据列col)
{
@标题
}
@foreach(Model.Rows中的数据行)
{
@foreach(Model.Columns中的数据列col)
{
@行[列名称]
}
}
我在网上搜索过,但什么也没找到。
有关此问题的信息限制将SQL表或模型类导出到excel文件。
谁能帮我把html表格导出到excel吗?我用谷歌搜索把html表格导出到csv文件。并找到以下链接: 这很有帮助 注意:我编辑此代码是因为我的输出文件编码错误。在“jquery.tabletosv.js”文件中,需要更改以下代码:
var uri = 'data:text/csv;charset=UTF-8,' + encodeURIComponent(csv);
取而代之的是顶层代码,我写道:
var uri = 'data:text/csv;charset=UTF-8,%EF%BB%BF' + encodeURIComponent(csv);
并正确导出编码。您可以参考以下方法将表格导出到excel文件
@using System.Data
@model DataTable
<table id="tblExport" class="table table-hover table-responsive table-striped">
@*table content*@
</table>
<input type="button" onclick="tableToExcel('tblExport', 'W3C Example Table')" value="Export to Excel">
@section scripts{
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
}
您可以创建Blob.js和xls.core.min.js文件,并从这些链接中的js内容添加内容:和
该网页如下所示:
使用此方法,可以将表格导出为excel、csv和txt文件
//index page: display the table data.
public IActionResult ExportExcel()
{
var custTable = CreateDataTable();
return View(custTable);
}
public IActionResult ExportDataTabletoExcel()
{
var dt = CreateDataTable();
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt);
using (MemoryStream stream = new MemoryStream())
{
wb.SaveAs(stream);
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Grid.xlsx");
}
}
}
public DataTable CreateDataTable()
{
// Create a new DataTable.
DataTable custTable = new DataTable("Customers");
DataColumn dtColumn;
DataRow myDataRow;
... //add columns and rows.
return custTable;
}
查看页面中的代码:
@using System.Data
@model DataTable
<table id="tblExport" class="table table-hover table-responsive table-striped">
@*table content*@
</table>
<a href='@Url.Action("ExportDataTabletoExcel","Home")'> export to excel</a>
@使用System.Data
@模型数据表
@*表内容*@
使用此方法,它将生成一个.xlsx
文件,内容如下:
//index page: display the table data.
public IActionResult ExportExcel()
{
var custTable = CreateDataTable();
return View(custTable);
}
public IActionResult ExportDataTabletoExcel()
{
var dt = CreateDataTable();
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt);
using (MemoryStream stream = new MemoryStream())
{
wb.SaveAs(stream);
return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Grid.xlsx");
}
}
}
public DataTable CreateDataTable()
{
// Create a new DataTable.
DataTable custTable = new DataTable("Customers");
DataColumn dtColumn;
DataRow myDataRow;
... //add columns and rows.
return custTable;
}
也许有个提示:写一个csv文件。Excel对此非常满意。也许会对您有用?@berkansasmaz此解决方案在asp.net上有效,而在asp.net core上无效。@GerardH.Pille谢谢您的提示。我找到了我的答案。啊,我看到了你的答案。你应该在问题中说“下载”;-)您的编码还可以,但Microsoft希望在文件的开头有一个BOM字节顺序标记。非常感谢。。。你的回答非常完整。。。