In this article, we will learn about a quick way to generate reports using .net core API. When we work in companies, we may face a situation where a client asks to generate reports, and he is not ready to spend money on purchasing third-party libraries to get reports. Apart from this, he wants work to be done on the same day, or he has some urgency.
In this situation, this article will be helpful when we need to generate reports in a single day without spending and wasting time on research.
- We will create a .Net core API.
- Create an Endpoint for the Download file.
- Make a call to the database and fetch data.
- Create a report in HTML format and send the HTML file back to the user as a response.
Consider connecting to the database and getting the data table below.
Consider the code below.
using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Text;
namespace DownloadFile.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DownloadFile : ControllerBase
{
// GET: api/<DownloadFile>
[HttpGet]
public ActionResult GetFileDownload()
{
DataTable dt = GetDataFromDatabase();
string HtmlBody = ExportDatatableToHtml(dt);
var contentType = "text/xml";
var bytes = Encoding.UTF8.GetBytes(HtmlBody);
var result = new FileContentResult(bytes, contentType)
{
FileDownloadName = "Report.html"
};
return result;
}
protected string ExportDatatableToHtml(DataTable dt)
{
// Using HTML code to create an HTML file and print the datatable.
StringBuilder strHTMLBuilder = new StringBuilder();
strHTMLBuilder.Append("<html>");
strHTMLBuilder.Append("<head>");
strHTMLBuilder.Append("</head>");
strHTMLBuilder.Append("<body>");
strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'>");
strHTMLBuilder.Append("<tr>");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<td>");
strHTMLBuilder.Append(myColumn.ColumnName);
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
foreach (DataRow myRow in dt.Rows)
{
strHTMLBuilder.Append("<tr>");
foreach (DataColumn myColumn in dt.Columns)
{
strHTMLBuilder.Append("<td>");
strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
}
// Close tags.
strHTMLBuilder.Append("</table>");
strHTMLBuilder.Append("</body>");
strHTMLBuilder.Append("</html>");
return strHTMLBuilder.ToString();
}
private DataTable GetDataFromDatabase()
{
// Simulating a database call, here the datatable is hardcoded.
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("NAME", typeof(string));
table.Columns.Add("CITY", typeof(string));
table.Rows.Add(111, "Devesh", "Ghaziabad");
table.Rows.Add(222, "Nikhil", "United State");
table.Rows.Add(102, "Himanshu", "Canada");
table.Rows.Add(456, "Kapil", "Dubai");
table.Rows.Add(3345, "Sanjay", "Germany");
return table;
}
}
}
Understanding code
- We have created GetFileDownload() Endpoint.
- We are making a call to the database function name -> GetDataFromDatabase, in a real-time use case please replace the hard-coded data table with actual data from the database.
- We have created a Function to convert Datatable to an HTML file
- We are iterating each row of the data table and formatting rows and columns using very simple tr and td tags.
- The download code has FileContentResult, which is inherited from ActionResult. we are converting html string into bytes encapsulating bytes into the FileContentResult class and returning the result.
Running application
Click Download API.
We will get the file from the code.
Download file
Open File. We have opened the file in a browser.
We are ready with the report.
Conclusion
This is a quick way to generate files in html format. The above code is 100 % working and fast because we are not using any third-party library to create files