Excelへのエクスポートのために、コントローラーでこのコードを使用しています。Excel へのエクスポートに NPOI dll を使用しています。
public ActionResult ExportToExcelCombine(string select_rows) {
var ワークブック = 新しい 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("");
using (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", "ホーム")?select_rows='+val); }); });
$('#select_rows').val() を隠しフィールド値に使用しています。これは、Jtable でいくつかの行を選択すると変化します。