1

How to Convert Table into a Normal Range of cells while exporting excel in c#. By default it is in Table format, not able to sort due to tabel format. I need this to normal range of cells.

After excel exported, you able to do in manually by selecting Table tools (design) and have option to select "convert to range". I need to done via code by using closedXML.

System.Data.DataTable dataTable = magicButtonApi.ExportData(magicButtonCheckedNodesBE); var fileName = "Excel_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx"; //save the file to server temp folder string fullPath = Path.Combine(Server.MapPath("~/temp"), fileName);

        using (MemoryStream stream = new MemoryStream())
        {
            // Start a new workbook
            XLWorkbook wb = new XLWorkbook();

            // Add a DataTable as a worksheet
            var ws = wb.Worksheets.Add(dataTable, "MagicButton");               

            //wb.SaveAs(stream, false);
            wb.SaveAs(fullPath);
            return Json(new { fileName = fullPath, errorMessage = "" });
            // Return a byte array
            //return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "YourReportName.xlsx");
        }
Akash
  • 11
  • 4

1 Answers1

-1
wb.Worksheets.Tables.Remove(dataTable.Name);
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 1
    Welcome to Stack Overflow! Please read [answer] and [edit] your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post. – Adriaan Jan 10 '23 at 08:54