Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using OfficeOpenXml;
  9. using System.Collections.Specialized;
  10.  
  11.  
  12. namespace RssReader{
  13.     public partial class WebForm1 : System.Web.UI.Page{
  14.         protected void Page_Load(object sender, EventArgs e){
  15.  
  16.             NameValueCollection nvc = Request.Form;
  17.             if (nvc.AllKeys.Length > 0){
  18.                 string[] cols = nvc["cols"].Split('\t');
  19.                 string[] rows = nvc["data"].Split('\n');
  20.                 string type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  21.                 Response.ContentType = type;
  22.                 Response.AddHeader("content-disposition", "attachment;  filename=dotNet.xlsx");
  23.                 Response.BinaryWrite(new ExcelReportBuilder().Build(cols, rows));
  24.             }
  25.         }
  26.     }
  27.  
  28.     public class ExcelReportBuilder{
  29.         public ExcelReportBuilder(){}
  30.  
  31.         public byte[] Build(String[] cols, String[] rows){
  32.             byte[] xlsBytes;
  33.  
  34.             // Create an excel workbook.
  35.             using (ExcelPackage pck = new ExcelPackage()){
  36.                 // Add the worksheet.
  37.                 ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Rss Feed");
  38.                 ws.Cells["A1"].Value = cols[0];
  39.                 ws.Cells["A1"].Style.Font.Bold = true;
  40.                 ws.Cells["B1"].Value = cols[1];
  41.                 ws.Cells["B1"].Style.Font.Bold = true;
  42.                 ws.Cells["C1"].Value = cols[2];
  43.                 ws.Cells["C1"].Style.Font.Bold = true;
  44.  
  45.                 // Loop over each user and write out the data to columns.
  46.                 for (int i = 0; i < (rows.Length -1); i++){
  47.                     string[] cell;
  48.                     cell = rows[i].Split('\t');
  49.                     ws.Cells["A" + (i + 2).ToString()].Value = cell[0];
  50.                     ws.Cells["B" + (i + 2).ToString()].Value = cell[1];
  51.                     ws.Cells["C" + (i + 2).ToString()].Value = cell[2];
  52.                 }
  53.                 xlsBytes = pck.GetAsByteArray();
  54.             }
  55.             return xlsBytes;
  56.         }
  57.     }
  58. }