Friday, February 18, 2011

Drawing graph in MS Excel using C#

Platform: ASP.NET-2.0
Database: MS SQL Server-2005
AdventureWork (Download)

In this article we will try to learn drawing graph in MS Excel.

For this article, we have used Customer table of Northwind database. Here we are counting companies in various countries.

First we populate the GridView. This will give you an idea about the data only. There is nothing to do with drawing the graph.

On clicking btnExport, all functionality of drawing graph is written.

DatatTable that has the resultant table of the query using which we have to draw the graph, first it is being written on the Excel file and on the basis of the data written on the excel, graph is plotted. Data written in the excel file and the graph plotted into it is interrelated. At the end don’t forget the go through the function ReleaseObject(). Also there will be needed a .Net reference of Microsoft.Office.Interop.Excel

Excel.Application xlApp;

Excel.Workbook xlWorkBook;

Excel.Worksheet xlWorkSheet;

object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.ApplicationClass();

xlWorkBook = xlApp.Workbooks.Add(misValue);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

DataTable dt = new DataTable();

dt = PopulateCompanyCount(0);

xlWorkSheet.Cells[1, 1] = "";

xlWorkSheet.Cells[1, 2] = "Count";

for (int i = 0; i <>

{

for (int j = 0; j <>

{

xlWorkSheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();

}

}

chartRange = xlWorkSheet.get_Range("A1", str);

chartPage.SetSourceData(chartRange, misValue);

chartPage.ChartType = Excel.XlChartType.xlColumnClustered;

string strPath = @"C:\Documents and Settings\awadheshs\My Documents\";

string strFileName = "CompanyCount";

xlWorkBook.SaveAs(strPath + strFileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

xlWorkBook.Close(true, misValue, misValue);

xlApp.Quit();

releaseObject(xlWorkSheet);

releaseObject(xlWorkBook);

releaseObject(xlApp);

lblAlert.Text = "Excel file created...
"
+

"Created on: " + DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss") +

"
Collect the file "
+ strFileName + " at location "
+ strPath;

//This function will be used as it is….

private void releaseObject(object obj)

{

try

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

obj = null;

}

catch (Exception ex)

{

obj = null;

lblAlert.Text = "Exception Occured while releasing object " + ex.ToString();

}

finally

{

GC.Collect();

}

}


Click on download to get the complete code

Exporting GridView to MS Excel using C#

Platform: ASP.NET-2.0
Database: MS Sql Server-2005
AdventureWork (download)

Main aim of this article is to export ASP.NET-2.0 GridView data into MS Excel. Additionally this article will focus on proper report formatting as well.

We have taken data from Customer table of default database Northwind.

Before going into export to excel functionality, first we have to fill the GridView.

We assume GridView is filled and its matter of exporting it into Excel. (Refer code attached with this article. AdventureWork).

There is button with ID btnExport on its Click event; we have to write the functionality of exporting. First set AllowPaging property of GridView to false so that whole data can be exported into single Excel file.

grdCustomerDetails.AllowPaging = false;

And then fill the GridView. At this time formatting GridView can be done so that it might reflect into Excel file.

grdCustomerDetails.ForeColor = Color.Black;

grdCustomerDetails.Style.Add("font-family", "Verdana");

grdCustomerDetails.Style.Add("font-size", "10px");

Here are few lines of codes that are self explanatory…

Response.Clear();

Response.AddHeader("content-disposition", "attachment; filename=Customer_Detailed_Report.xls");

Response.Charset = "";

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd-ms.xls";

StringWriter stringWrite = new StringWriter();

HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

While exporting we have to take care of report header as well. So for this purpose take a label control with visibility false. It will help in rendering the report header information with its text property.

lblExcelHeader.Visible = true;

string strReportHeader = "Employee Detailed Report
"
+

"Report Generated on: " + DateTime.Now.ToString("dd-MMM-yyyy") + "
"
+

"" +

"Note: This is a demo report";

lblExcelHeader.Text = strReportHeader;

lblExcelHeader.RenderControl(htmlWrite);

lblExcelHeader.Visible = false;

grdCustomerDetails.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();

Saturday, February 12, 2011

Exporting repots into MS Excel, MS Word or PDF

Reports are essential part of any application and showing them into proper format is also important. Showing reports into MS Excel, MS Word and sometimes in pdf format are mostly common. Some people use third part reporting services that are supported my SQL Server as well. But prefer to show reports in web pages only because it can be handled very efficiently.

So in coming topics we will see the exporting data from asp.net gridview to above mentioned formats. Exporting to MS Excel is very common but we will not only take exporting issue but also formatting of reports as well.

Also topic exporting to MS Excel will be in to parts... first Exporting a gridview and second exporting a graph into MS Excel.