excel 导出 OpenXml
Posted yyzyou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了excel 导出 OpenXml相关的知识,希望对你有一定的参考价值。
public ActionResult Export(string queryJson) { try { var list = Biz.GetDetailView(queryJson); if (list == null) return Json(new { IsSuccess = false, Message = "无数据!" }, "text/html"); var basePath = Server.MapPath("~/"); var file = string.Format("{0:yyMMddHHmmssfff}.xlsx", DateTime.Now); System.IO.File.Copy(Path.Combine(basePath, "Templates", "详细明细.xlsx"), Path.Combine(basePath, "Export", file)); using (SpreadsheetDocument document = SpreadsheetDocument.Open(Path.Combine(basePath, "Export", file), true)) { IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>(); if (sheets.Count() == 0) TempData["Message"] = "文件中未包含指定数据!"; else { WorksheetPart sheet = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id); WorksheetWriter writer = new WorksheetWriter(document, sheet); SpreadsheetStyle style = SpreadsheetStyle.GetDefault(document); int rowIndex = 2; for (int i = 0; i < list.Rows.Count; i++) { writer.PasteText(string.Format("A{0}", rowIndex), list.Rows[i]["IMEI"].ToString(), style); writer.PasteText(string.Format("B{0}", rowIndex), list.Rows[i]["GOODSNAME"].ToString(), style); writer.PasteText(string.Format("C{0}", rowIndex), list.Rows[i]["UNITNAME"].ToString(), style); rowIndex++; } writer.Save(); } } return Json(new { IsSuccess = true, Message = "导出成功!", File = file }, "text/html"); } catch (Exception ex) { return Json(new { IsSuccess = false, Message = ex.Message }, "text/html"); } }
以上是关于excel 导出 OpenXml的主要内容,如果未能解决你的问题,请参考以下文章