我正在开发一个导出函数,在这里我需要通过
@Html.ActionLink
. 基本上,用户将选择开始日期、结束日期和阶段号,单击按钮可将整个表导出到excel中。下面我附上了js,html,cs文件的代码。
<script type="text/javascript">
$(document).ready(function () {
$("#departing").datepicker({ dateFormat: 'yy-mm-dd' });
$("#returning").datepicker({ dateFormat: 'yy-mm-dd' });
$("button").click(function () {
var selected = $("#dropdown option:selected").text();
var departing = $("#departing").val();
var returning = $("#returning").val();
});
});
var selPhaseNumber = "A";
$(document).ready(function () {
//fetch and bind the year dropdown list
$('input[type=radio][name=rdbPhase]').change(function () {
if ($(this).val() == "Phase I") {
selPhaseNumber = "I";
}
else if ($(this).val() == "Phase II") {
selPhaseNumber = "II";
}
else {
selPhaseNumber = "A";
}
})
})
</script>
html代码:
<div class="col-lg-6">
<div class="col-lg-3">@Html.ActionLink("Export to Excel", "ExportSubmittedTicketToExcel", "ExportExcel", null, new { @class = "btn btn-primary", @id = "btnExport"})</div>
</div>
(电话应该是这样的)
api/ProjectBilling/SubmittedExportBillDetails/" + startdate + '/' + enddate + '/' + selPhaseNumber
控制器代码:
[Route("ExportExcel/ExportSubmittedTicketToExcel/")]
public ActionResult ExportSubmittedTicketToExcel(string MemberName)
{
try
{
if (System.Web.HttpContext.Current.Session["DisplayName"] != null)
{
List<ExportExcelModelSubmitted> Result = new List<ExportExcelModelSubmitted>();
using (var client = new HttpClient())
{
int UserID = Convert.ToInt32(System.Web.HttpContext.Current.Session["CurrentUserID"]);
client.BaseAddress = new Uri(WebApiURL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/ProjectBilling/SubmittedExportBillDetails/" + startdate + '/' + enddate + '/' + selPhaseNumber).Result;
if (response.IsSuccessStatusCode)
{
Result = response.Content.ReadAsAsync<List<ExportExcelModelSubmitted>>().Result;
}
else
{
string returncode = response.StatusCode.ToString();
return Content("<script language='javascript' type='text/javascript'>alert('" + returncode + "');</script>");
}
}
DataTable dt = new DataTable();
dt = Result.ToDataTable();
MemoryStream stream = new MemoryStream();
using (ExcelPackage pck = new ExcelPackage(stream))
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("BillingDetails");
ws.Cells["A1"].LoadFromDataTable(dt, true);
ws.Cells.AutoFitColumns();
using (ExcelRange rng = ws.Cells[1, 1, 1, dt.Columns.Count])
{
rng.Style.Font.Bold = true;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189));
rng.Style.Font.Color.SetColor(System.Drawing.Color.White);
}
pck.SaveAs(stream);
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment;filename=SubmittedBilling.xls");
Response.BinaryWrite(stream.ToArray());
Response.End();
}
return View();
}
else
{
return RedirectToAction("Login", "Home");
}
}
catch (Exception ex)
{
throw ex;
}
}