我有以下情况。我正在做一个excel导出。通过在查询字符串中只传递一个ID来使用ng-href时,它工作正常。但是现在我有多个ID要通过,所以我在HTML和$ http.POST中使用ng-click将postData传递给服务器,如下所示。问题是如果我使用ng-click并调用$ http.post,它会获取所需的数据,但它不会创建excel文档。我认为这是因为我没有使用ng-href,但无法弄清楚如何修复它。任何帮助将不胜感激。
HTML:
<div id="transmission-buttons" data-ng-show="resultsReturned">
<!--<a ng-href="../ExcelDownload/ExcelDownload.aspx?workListID={{selectedExportWorkListID}}" class="btn btn-info pull-right" target="_blank"><span class="glyphicon glyphicon-export"></span>Export to Excel</a>-->
<a class="btn btn-info pull-right" href="" ng-click="getWorklistExportData($event)" target="_blank"><span class="glyphicon glyphicon-export"></span>Export to Excel</a>
</div>
Controller.JS:
$scope.getWorklistExportData = function (e) {
rest.post('../ExcelDownload/ExcelDownload.aspx', $scope.selectedWorkListIDs).success(function (resp) {
var successResp = resp;
e.preventDefault();
}).error(function (resp) {
var errResp = resp;
});
}
aspx.cs:
public partial class ExcelDownload_ExcelDownload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var sr = new StreamReader(Request.InputStream);
var stream = sr.ReadToEnd();
CreateExcel(stream);
//CreateExcel(Convert.ToInt32(Request.QueryString["workListID"]));
//CreateExcel(823);
}
}
private void CreateExcel(string cbrIdList)
{
var client = new RestClient(ConfigurationManager.AppSettings["TIBCOServer"]);
string url = ConfigurationManager.AppSettings["ExportWorklistCBRS"];
//url = url.Replace("{workListID}", workListID.ToString());
var request = CreateStandardRestRequest(url, Method.POST);
request.RootElement = "WorkListDetailExport";
request.AddParameter("CBR_ID", cbrIdList, ParameterType.RequestBody);
var queryResult = client.Execute<List<CBRSRecord>>(request);
var queryResult2 = client.Execute(request);
DataTable tbl = ToDataTable<CBRSRecord>(queryResult.Data);
tbl = FormatColumnNames(tbl);
// DataTable tbl = new DataTable();
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Worklist");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(tbl, true);
for (int x = 1; x < 50; x++)
{
ws.Column(x).AutoFit();
}
using (ExcelRange rng = ws.Cells["A1:AB1"])
{
rng.Style.Font.Bold = true;
}
//Write it back to the client
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=WorkList" + DateTime.Now.ToLongTimeString() + ".xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
Response.End();
}
}