我在控制器中使用此代码导出到 excel。我正在使用 NPOI dll 导出到 excel。
公共 ActionResult ExportToExcelCombine(string select_rows) {
var workbook = new HSSFWorkbook();
string[] test = select_rows.ToString().Split(';');
var sheet = workbook.CreateSheet("Summary");
int row_index = 0;
var excelnewrow = sheet.CreateRow(row_index);
excelnewrow.CreateCell(0).SetCellValue("Entity Name");
row_index++;
excelnewrow = sheet.CreateRow(row_index);
excelnewrow.CreateCell(0).SetCellValue("Type of Entity");
row_index++;
excelnewrow = sheet.CreateRow(row_index);
excelnewrow.CreateCell(0).SetCellValue("Industry");
row_index++;
excelnewrow = sheet.CreateRow(row_index);
excelnewrow.CreateCell(0).SetCellValue("Country");
row_index++;
excelnewrow = sheet.CreateRow(row_index);
excelnewrow.CreateCell(0).SetCellValue("");
row_index++;
excelnewrow = sheet.CreateRow(row_index);
HSSFCellStyle style_summary_color = workbook.CreateCellStyle();
// cell background
style_summary_color.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLACK.index;
style_summary_color.FillPattern = HSSFCellStyle.SOLID_FOREGROUND;
// font color
HSSFFont font_summary_color = workbook.CreateFont();
font_summary_color.Color = NPOI.HSSF.Util.HSSFColor.WHITE.index;
style_summary_color.SetFont(font_summary_color);
excelnewrow.RowStyle = style_summary_color;
excelnewrow.CreateCell(0).SetCellValue("Facebook");
excelnewrow.GetCell(0).CellStyle = style_summary_color;
row_index++;
excelnewrow = sheet.CreateRow(row_index);
excelnewrow.CreateCell(0).SetCellValue("");
使用 (var exportData = new MemoryStream()) { workbook.Write(exportData);
string saveAsFileName = string.Format("Report-{0:d}.xls", DateTime.Now).Replace("/", "-");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
return File(saveAsFileName, "application/vnd.ms-excel", saveAsFileName);
}
TempData["Message"] = "Excel报表创建成功!";
//return File(buffer, "application/vnd.ms-excel");
return RedirectToAction("ExcelPackage");
}
鉴于,我正在使用
在 jquery 中,我正在使用
$(document).ready(function () { $("#export_excel ").click(function (event) { // $(this).attr('href', "/Home/ExportToExcelCombine?select_rows="+ $ ('#select_rows').val()); var val=$('#select_rows').val(); $(this).attr('href', '@Url.Action("ExportToExcelCombine", "Home ")?select_rows='+val); }); });
我将 $('#select_rows').val() 用于隐藏字段值,当我在 Jtable 中选择一些行时,它会发生变化。