using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using OfficeOpenXml;
using System.Collections.Specialized;
namespace RssReader{
public partial class WebForm1 : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
NameValueCollection nvc = Request.Form;
if (nvc.AllKeys.Length > 0){
string[] cols = nvc["cols"].Split('\t');
string[] rows = nvc["data"].Split('\n');
string type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentType = type;
Response.AddHeader("content-disposition", "attachment; filename=dotNet.xlsx");
Response.BinaryWrite(new ExcelReportBuilder().Build(cols, rows));
}
}
}
public class ExcelReportBuilder{
public ExcelReportBuilder(){}
public byte[] Build(String[] cols, String[] rows){
byte[] xlsBytes;
// Create an excel workbook.
using (ExcelPackage pck = new ExcelPackage()){
// Add the worksheet.
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Rss Feed");
ws.Cells["A1"].Value = cols[0];
ws.Cells["A1"].Style.Font.Bold = true;
ws.Cells["B1"].Value = cols[1];
ws.Cells["B1"].Style.Font.Bold = true;
ws.Cells["C1"].Value = cols[2];
ws.Cells["C1"].Style.Font.Bold = true;
// Loop over each user and write out the data to columns.
for (int i = 0; i < (rows.Length -1); i++){
string[] cell;
cell = rows[i].Split('\t');
ws.Cells["A" + (i + 2).ToString()].Value = cell[0];
ws.Cells["B" + (i + 2).ToString()].Value = cell[1];
ws.Cells["C" + (i + 2).ToString()].Value = cell[2];
}
xlsBytes = pck.GetAsByteArray();
}
return xlsBytes;
}
}
}