public void Export()
{
ExcelPackage pck = new ExcelPackage();
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Rapport");
//the object has 32 properties, so that's why I loop over them, to much to type out
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";
// Response.AddHeader("content-disposition","attachment: filename=" + "ExcelReport.xlsx");
//Response.BinaryWrite(pck.GetAsByteArray());
//Response.end();
}
public string[] AlphabeticalColumns(int amount)
{
int counter = 0;
int counter2 = 0;
string pre = "";
string[] value = new string[amount];
string[] alphabet = new[] { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
for (int i = 0; i <= amount; i++)
{
value.Append(pre + alphabet[i-counter2]);
if (i == alphabet.Count()-1)
{
pre = alphabet[counter];
counter++;
counter2 = counter2 + alphabet.Count();
}
}
return alphabet;
}
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.