1

In need of some advice here.

I have written a ASP.NET webpage to display a report. The rendered html table was done in the server side and passed back to the calling AJAX as a string:

public string GenerateHtml()
{
    var html = "";
    html = "<table><tr><td></td></tr></table>"; 
    return html;
}

and the calling AJAX:

$.ajax({
    url: '/BoardTransaction/GenerateHtml',
    type: "GET", 
    },
    async: false,
    success: function (result) {
        $('#divReport').html(result); 
    } 
});

and in the page I have a button that downloads the generated table to Excel:

$("#btnDownload").click(function (e) {
    var file = new Blob([$('#divReport').html()],{ type: "application/vnd.ms-excel" });
    var fileName = "FORECAST_" + jQueryToday() + ".xls";

    var url = URL.createObjectURL(file);

    var a = $("<a />",
            {
                href: url,
                download: fileName
            })
        .appendTo("body")
        .get(0)
        .click();
    e.preventDefault();
});

Now another of the requirement is that to have a scheduler to download the same table and e-mail them once a week. I've done coding to Excel before so I knew it would be a pain to write GenerateHtml() to suit the coding needed to generate Excel file so I was wondering if I could just basically copy the same function into my console app and use the html strings to generate Excel.

Hope if you could give any suggestions on where should I start. Tried searching html to excel but the results gave me if it is in ASP.NET but not concole app.

Thank you for your kind attention.

Khairul
  • 191
  • 2
  • 14
  • Is it a requirement that the file attached to the email is Excel? If so, would it be fine to send a comma separated text file? Those are notoriously simple to generate and can be read by Excel just fine. – AndreasHassing Jun 12 '18 at 06:32
  • if you have your data model properly separated from the view (html), it should not be a problem to obtain the excel file from the data model as an alternative to the browser rendition. With the exception maybe of a webservice API, I would never convert back from html using a http request to the same backend just because it seems convenient - if there is no generic data set as the basis for both html and excel, this is the time to create one. – Cee McSharpface Jun 12 '18 at 06:39

1 Answers1

1

Try Web scraping it will fit on your requirement in console application, you just need the HTML path, may be in your local or, on server. I'm using as local HTML pages . just you have to install the package (HtmlAgilityPack) : https://www.nuget.org/packages/HtmlAgilityPack/

    string FileLocation = @"D:\HTMLTable.html";
            string HtmlInTextWeGot = File.ReadAllText(FileLocation);
            /*this will call the HTML as text */
            if (!string.IsNullOrEmpty(HtmlInTextWeGot))
                {
                  /*here you load your HTML in HTMLDocument*/
                  HtmlDocument HtmlDocument = new HtmlDocument();
                  HtmlDocument.LoadHtml(HtmlInTextWeGet);
                  /* here you will get all your tables ( which is precent in your HTML)in list */
                  var TablesDataWeGet 
                  =HtmlDocument.DocumentNode.Descendants("table").ToList();

    /*you can also pick the particular HTML table by providing LINQ conditions like */

 var TablesDataWeGet 
                  =HtmlDocument.DocumentNode.Descendants("table").Where(node => node.GetAttributeValue("Id", "").Equals("DatabaseSummary").ToList();
                }

After getting your HTML into List you will manipulate the Tables as per you requirement: if you want how to convert Table into Column and row in list check out this page: https://html-agility-pack.net/knowledge-base/2431652/html-agility-pack and: HTML Agility Pack : In ASP you already have the Excel functionality, copy and paste it in your console application.)