Exporting to an excel file

public void Export() { ExcelPackage pck = new ExcelPackage(); //Add a new ExcelSheet ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Rapport"); List<string> propsString = _home.getProperties(); PropertyInfo[] props = _home.GetProperties(); string[] columnHeaders = this.AlphabeticalColumns(propsString.Count()); int counter = 0; IEnumerable<Home> data = _homesFiltered; foreach (var pr in propsString) { ws.Cells[columnHeaders[counter] + "1"].Value = pr.ToString(); counter++; } int rowStart = 2; foreach (var item in data) { int columnCounter = 1; foreach (var pr in props) { ws.Cells[string.Format("{0}{1}", columnHeaders[columnCounter], rowStart)].Value = pr.GetValue(item); columnCounter++; } rowStart++; } ws.Cells["A:ZZ"].AutoFitColumns(); Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.