c# - Export to Excel the complete MVC View with Non-table Content -
I am working on an MVC application. We have a scene that has 30 different text boxes / dropdown controls and 2 tables.
When the user clicks on export in Excel, I need to export all the control and table data.
What is the best way to export these results in Excel?
I have created a different view for Excel and have tried to export the axle to see.
Public execution ExprotToExcel (int id) {var model = GetExportModel (id); HttpContext.Response.AddHeader ("content-type", "attachment; file name = XX_" + date time.Now toastring () + ".xls"); This.Response.ContentType = "app / vnd.ms-excel"; See Return (Model); } But the above code is not formatting Excel data Currency and decimal areas are not being formatted.
Can we format the data in the above scenario?
Please help me with the right perspective
You use the NPOI or Explet Library You can make a memory excel book like this and a file can be returned as a result.
Below is a simple case of writing a collection of data rows in a workbook using NPOI which can be returned as a file result.
public fileResult ExportResults (int id) {var model = GetExportModel (ID); Var workbook = new HSSFWorkbook (); Var Sheet = Workbook. Crete Sheet ("Test Sheet 1"); Var headerRow = sheet.CreateRow (0); HeaderRow.CreateCell (0) .SetCellValue ("id"); HeaderRow.CreateCell (1) .SetCellValue ("TestDataCol1"); HeaderRow.CreateCell (2) .SetCellValue ("TestDataCol2"); Sheet.playfifen (0, 1, 0, 1); Int rowNumber = 1; Foreign currency (var datarow in model.data) {var datestyle = SetCellStyle (workbook, "dd / MM / yyyy"); Var line = sheet. Cutter (line number ++); Row.CreateCell (0) .SetCellValue (datarow.Id); Row.CreateCell (1) .SetCellValue (datarow.TestData1); Row.CreateCell (2) .SetCellValue (datarow.TestData2); Line. Seals [1] .CellStyle = Dietstyle; Line. Seals [2] .CellStyle = Diet Style; } Memorystream output = new memorystream (); Workbook.Write (production); Return file (output.ORRA), "app / vnd.ms-excel", "ExcelData.xls"); } NPOI is an open source project that can help you read / write XLS, Doctor, PPT files.
Point you in the right direction,
Comments
Post a Comment