我使用這個代碼在控制器導出到Excel。我正在使用NPOI dll導出爲excel。
公共的ActionResult ExportToExcelCombine(串select_rows) {
變種工作簿=新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 =新的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中,我使用的
$(文件)。就緒(樂趣ction(){$(「#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中選擇某些行時會發生變化。
請顯示您的代碼。否則,任何人都可以幫忙? – awrigley
請檢查代碼提及。我編輯了我的佇列 – Saloni
如果您顯示操作的代碼,這將會有所幫助。錯誤是服務器端錯誤,所以這是問題所在。所有的jQuery都會調用這個動作。 – awrigley