Developers Guide - Web Report Generator

Execution Steps

Inputs

  • A Template created in one of the Supported Document Formats conforming to Supported Template Syntax.
  • A Connection String in order to connect to a DataBase that can be accessed through DataSet / DataTable etc
  • Type of Data Source. For example, Table / Views etc
  • Data Source. Select a table / view from the connected DataBase

System Processing

  • Verifies Template Document Format.
  • Connects to MS SQL Server Database using connection string
  • Tests Connection according to the provided connection string.
  • Populates all Tables from connected database.
  • Populates all Views from connected database.
  • Allows to enter Custom Query.
  • Passes Template and Data Source to the GroupDocs.Assembly - GroupDocs.Assembly generates a report according to the inputs
  • Serves the report back to the browser

Output

  • A Report generated based on the Template Syntax and the Data Source - This report will be in the same Document Format as that of the source Template Document Format.

Here is a sample report in Microsoft Word Document Format for ‘Products’ table

Code Walkthrough

Default.aspx.cs

// For complete examples and data files, please go to https://github.com/groupdocsassembly/GroupDocs_Assembly_NET
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GroupDocs.ReportGenerator.BusinessLayer;
namespace GroupDocs.ReportGenerator
{
public partial class Default : System.Web.UI.Page
{
#region Properties
static string uploadedTemplatePath;
static string uploadedTemplateType;
#endregion
#region Page load and events
// page load event
protected void Page_Load(object sender, EventArgs e)
{
try
{
// verify page is not post back, so we can setup default page view.
if (!Page.IsPostBack)
{
// Load form view.
InitForm();
}
}
catch (Exception exc)
{
lblMessage.Text = exc.Message;
}
}
//Reload/Populate Tables & Views dropdownlist
protected void TestConnectionAndLoad_Click(object sender, EventArgs e)
{
try
{
ddlSource.Attributes.Add("onchange", "javascript:sourceChanged();");
// Creating business layer object with user provided connection string
BusinessDB businessDBObj = new BusinessDB(txtConString.Text.Trim());
// Verify connection string successfully connect database
if (businessDBObj.IsValidConnection())
{
// populate dataset with tables
DataSet dsDBObjects = businessDBObj.getDBObjectNames(DBObjects.Tables);
if (dsDBObjects != null)
{
ddlTables.DataSource = dsDBObjects;
ddlTables.DataTextField = "name";
ddlTables.DataValueField = "name";
ddlTables.DataBind();
}
// populate dataset with views
dsDBObjects = null;
dsDBObjects = businessDBObj.getDBObjectNames(DBObjects.Views);
if (dsDBObjects != null)
{
ddlViews.DataSource = dsDBObjects;
ddlViews.DataTextField = "name";
ddlViews.DataValueField = "name";
ddlViews.DataBind();
}
lblMessage.Text = "Successfully connected to database.";
}
else
{
ddlTables.Items.Clear();
ddlViews.Items.Clear();
txtCustomQuery.Text = "";
}
}
catch (Exception exc)
{
lblMessage.Text = exc.Message;
}
}
//Verify and upoload template
protected void UploadTemplate_Click(object sender, EventArgs e)
{
try
{
lblMessage.Text = "";
ddlSource.Attributes.Add("onchange", "javascript:sourceChanged();");
// Verify file and upload to server and save file location in constant variable/property.
if (fuTemplate.HasFile)
{
uploadedTemplatePath = "";
uploadedTemplateType = "";
uploadedTemplateType = fuTemplate.FileName.Split('.')[fuTemplate.FileName.Split('.').Length - 1];
if (!Directory.Exists(Server.MapPath("~/UploadTemplates")))
{
Directory.CreateDirectory(Server.MapPath("~/UploadTemplates"));
}
if (!Directory.Exists(Server.MapPath("~/GeneratedReports")))
{
Directory.CreateDirectory(Server.MapPath("~/GeneratedReports"));
}
// verify uploaded file (MS Word, MS Excel, MS Power Point)
if (uploadedTemplateType.ToLower().Equals("doc") || uploadedTemplateType.ToLower().Equals("docx") || uploadedTemplateType.ToLower().Equals("xls") || uploadedTemplateType.ToLower().Equals("xlsx") || uploadedTemplateType.ToLower().Equals("ppt") || uploadedTemplateType.ToLower().Equals("pptx"))
{
fuTemplate.SaveAs(Server.MapPath("~/UploadTemplates/" + fuTemplate.FileName));
uploadedTemplatePath = Server.MapPath("~/UploadTemplates/" + fuTemplate.FileName);
}
else
{
lblMessage.Text = "Please select valid template file (MS Word, MS Excel, MS Power Point)";
return;
}
lblMessage.Text = "Template uploaded successfully.";
}
else
{
lblMessage.Text = "Please select template file.";
}
}
catch (Exception exc)
{
lblMessage.Text = exc.Message;
}
}
//Export data file as per selected export type
protected void GenerateReport_Click(object sender, EventArgs e)
{
try
{
lblMessage.Text = "";
ddlSource.Attributes.Add("onchange", "javascript:sourceChanged();");
lblMessage.Text = "Data exported successfully.";
if (uploadedTemplatePath != "")
{
// Creating business layer object with user provided connection string
BusinessDB businessDBObj = new BusinessDB(txtConString.Text.Trim());
// Verify connection string successfully connect database
if (businessDBObj.IsValidConnection())
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GroupDocs_GeneratedReport_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "_" + DateTime.Now.Millisecond.ToString() + "." + uploadedTemplateType);
Response.Charset = "";
Response.ContentType = "Application/" + uploadedTemplateType;
// if data source type is Table
if (ddlSource.SelectedItem.Text == "Tables")
{
businessDBObj.GenerateReport_DBO(ddlTables.SelectedValue, uploadedTemplatePath.Replace("UploadTemplates", "GeneratedReports"), uploadedTemplatePath);
Response.WriteFile(businessDBObj.ReportDestinationPath);
}
// if data source type is Views
else if (ddlSource.SelectedItem.Text == "Views")
{
businessDBObj.GenerateReport_DBO(ddlViews.SelectedValue, uploadedTemplatePath.Replace("UploadTemplates", "GeneratedReports"), uploadedTemplatePath);
Response.WriteFile(businessDBObj.ReportDestinationPath);
}
// if data source type is custom query
else
{
businessDBObj.GenerateReport_Query(txtCustomQuery.Text.Trim(), uploadedTemplatePath.Replace("UploadTemplates", "GeneratedReports"), uploadedTemplatePath);
Response.WriteFile(businessDBObj.ReportDestinationPath);
}
Response.End();
}
}
else
{
lblMessage.Text = "Please upload template file.";
}
}
catch (Exception exc)
{
lblMessage.Text = exc.Message;
}
}
//Clear form data
protected void ClearForm_Click(object sender, EventArgs e)
{
try
{
lblMessage.Text = "";
Response.Redirect("~/Default.aspx");
}
catch (Exception exc)
{
lblMessage.Text = exc.Message;
}
}
#endregion
#region Private methods
//Form Initiating
private void InitForm()
{
uploadedTemplatePath = "";
ddlSource.Attributes.Add("onchange", "javascript:sourceChanged();");
ddlTables.Style["display"] = "none";
ddlViews.Style["display"] = "none";
txtCustomQuery.Style["display"] = "none";
switch (ddlSource.SelectedValue)
{
case "0":
ddlTables.Style["display"] = "block";
break;
case "1":
ddlViews.Style["display"] = "block";
break;
case "2":
txtCustomQuery.Style["display"] = "block";
break;
}
}
#endregion
}
}

ReportGenerator.cs

// For complete examples and data files, please go to https://github.com/groupdocsassembly/GroupDocs_Assembly_NET
using GroupDocs.Assembly;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace GroupDocs.ReportGenerator.BusinessLayer
{
public class ReportGenerator
{
/// <summary>
/// Properties for Report Generator
/// </summary>
private String _TemplateSourcePath;
public String TemplateSourcePath
{
get { return _TemplateSourcePath; }
set { _TemplateSourcePath = value; }
}
private String _ReportDestinationPath;
public String ReportDestinationPath
{
get { return _ReportDestinationPath; }
set { _ReportDestinationPath = value; }
}
private String _LicensePath = "D:/ReportGenerator/GroupDocs.ReportGenerator/GroupDocs.Assembly Product Family.lic";
/// <summary>
/// Generates the report by using dataset.
/// </summary>
/// <param name="ds"></param>
public void GenerateReportDB(DataSet ds)
{
try
{
License lic = new License();
//lic.SetLicense(_LicensePath);
//Instantiate DocumentAssembler class
DocumentAssembler assembler = new DocumentAssembler();
//Call AssembleDocument to generate Common List Report in open document format
assembler.AssembleDocument(_TemplateSourcePath, _ReportDestinationPath, ds);
}
catch (Exception ex)
{
throw ex;
}
}
}
}

Technical References

Creating Templates

An Example Business Case

Note: This business case covers almost all the Supported Data Formats

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.