如何使用MVC将Excel文件导出到AngularJs中的.xlsx扩展名中

时间:2017-11-10 05:38:49

标签: angularjs asp.net-mvc

////这里设计部分

<a class="btn btn-success" style="margin-bottom:10px;" 
   ng-click="exporttoexcel(polist)">Export to Excel</a> 

////这里是一个角度控制器文件,用于获取id并将其传递给MVC控制器

var exportlist = [];
$scope.exporttoexcel = function (data) {
    debugger;
    var array = [];
    for (var i in data) {
        if (data[i].Selected == true) {
            array.push(data[i].POID);
        }
    }
    debugger;
    var PoIDS = array.toString();
    window.location.href = "/Order/Export?id=" + PoIDS;
};

///////现在这部分是MVC我们获取记录和创建的控制器         ExcelFile(我想从.xlsx扩展名中保存,但无法打开),         但是,当我将扩展名更改为xls时,它会正常工作并完全打开但是我的         老板需要在.xlsx请打开

public ActionResult Export(string id = "")
{
    var list = orderApi.Exportorder(id).ToList();
    StringBuilder sb = new StringBuilder();
    string sFileName = "PurchaseOrder.XLSX";
    sb.Append("<table style='1px solid black; font-size:12px;' border='1'>");
    sb.Append("<tr>");
    sb.Append("<td><b> POnumber </b></td>");
    sb.Append("<td><b> Itemnumber </b></td>");
    //sb.Append("<td><b> Marking </b></td>");
    sb.Append("<td><b> Partnumber</b> </td>");
    sb.Append("<td><b> DescriptionEN </b></td>");
    sb.Append("<td><b> DescriptionCN </b></td>");
    sb.Append("<td><b> MaterialEN </b></td>");
    sb.Append("<td><b> MaterialCN</b> </td>");
    sb.Append("<td><b> Remark</b> </td>");
    sb.Append("<td><b> CTNS</b> </td>");
    sb.Append("<td><b> Packing</b> </td>");
    sb.Append("<td><b> Unit </b></td>");
    sb.Append("<td><b> Quantity </b></td>");
    sb.Append("<td><b> UnitPriceRMB</b></td>");
    sb.Append("<td><b> AmountUSD</b> </td>");
    sb.Append("<td><b> L</b> </td>");
    sb.Append("<td><b> W </b></td>");
    sb.Append("<td><b> H </b></td>");
    sb.Append("<td><b> Weight </b></td>");
    sb.Append("<td><b> Status</b> </td>");
    sb.Append("<td><b> StatusRemark</b> </td>");
    sb.Append("</tr>");
    if (list != null)
    {
        foreach (var data in list)
        {
            sb.Append("<tr>");
            sb.Append("<td>" + data.POID + "</td>");
            sb.Append("<td>" + data.OIID + "</td>");
            //sb.Append("<td></td>");
            sb.Append("<td>" + data.PartNo + "</td>");
            sb.Append("<td>" + data.ItemDesc_en + "</td>");
            sb.Append("<td>" + data.ItemDesc_ch + "</td>");
            sb.Append("<td>" + data.Material_en + "</td>");
            sb.Append("<td>" + data.Material_ch + "</td>");
            sb.Append("<td>" + data.ItemRemark + "</td>");
            sb.Append("<td>" + data.CTN + "</td>");
            sb.Append("<td>" + data.Packing + "</td>");
            sb.Append("<td>" + data.Unit + "</td>");
            sb.Append("<td>" + data.Quantity + "</td>");
            sb.Append("<td>" + data.UnitPriceRMB + "</td>");
            sb.Append("<td>" + data.Amount + "</td>");
            sb.Append("<td>" + data.L + "</td>");
            sb.Append("<td>" + data.W + "</td>");
            sb.Append("<td>" + data.H + "</td>");
            sb.Append("<td>" + data.Weight + "</td>");
            sb.Append("<td>" + data.ItemStatus + "</td>");
            sb.Append("<td>" + data.RemarkInt + "</td>");
            sb.Append("</tr>");
        }
    }
    sb.Append("</table>");

    HttpContext.Response.AddHeader("content-disposition", "attachment; 
        filename=" + sFileName);
    this.Response.ContentType = "application/vnd.ms-excel";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
    return File(buffer, "application/vnd.ms-excel");
}

/////下载时无法打开它获取扩展错误...       请有人帮我创建并打开它..       谢谢      enter image description here

1 个答案:

答案 0 :(得分:0)

对于xlsx您必须将内容类型更改为beginning_of_month类似

的内容
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Find full list here