using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using APNSoft.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
public partial class DataGrid_RowExporting : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Apply server-side events
myDataGrid.ExportToExcel += new DataGridEventHandler(myDataGrid_ExportToExcel);
myDataGrid.ExportToHTML += new DataGridEventHandler(myDataGrid_ExportToHTML);
myDataGrid.ExportCustom += new DataGridEventHandler(myDataGrid_ExportCustom);
//Define SQL query
string SqlQuery = @"SELECT C.CategoryName, C.Description, Sum(OD.UnitPrice*OD.Quantity) AS InTotal " +
"FROM (Categories as C INNER JOIN Products AS P ON C.CategoryID = P.CategoryID) " +
"INNER JOIN [Order Details] AS OD ON P.ProductID = OD.ProductID " +
"GROUP BY C.CategoryName, C.Description ORDER BY CategoryName;";
//Get DataTable (MS Access Database)
DataTable myDataSource = DataBase.GetDataTableOleDb(SqlQuery, "~/DataGrid/DataBases/Nwind.mdb");
//Set the data source
//myDataGrid.KeyFieldName = "CustomerID";
myDataGrid.DataSource = myDataSource;
myDataGrid.DataBind();
//Format data
System.IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
myDataGrid.Columns["InTotal"].FormatProvider = format;
myDataGrid.Columns["InTotal"].FormatType = typeof(System.Decimal);
myDataGrid.Columns["InTotal"].FormatString = "C";
}
//Server-side procedure for Export to Excel
private void myDataGrid_ExportToExcel(object sender, DataGridEventArgs e)
{
//Set file name (optional)
e.ExportFileName = "ExportData.xls";
}
//Server-side procedure for Export to HTML
private void myDataGrid_ExportToHTML(object sender, DataGridEventArgs e)
{
//Set file name (optional)
e.ExportFileName = "ExportData.html";
}
//Server-side procedure for Export to custom format
private void myDataGrid_ExportCustom(object sender, DataGridEventArgs e)
{
//Declarations
byte[] buffer = null; //Bytes to export
MemoryStream myMemoryStream = new MemoryStream();
//Get Grid Data
APNSoftDataGrid myGrid = (APNSoftDataGrid)sender;
GridRowCollection myRows = myGrid.Rows;
GridColumnCollection myColumns = myGrid.Columns;
//Create PDF
if (e.Parameter == "PDF")
{
#region Create PDF document
//Create & open PDF document
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 50, 50, 50, 50);
PdfWriter wri = PdfWriter.GetInstance(doc, myMemoryStream);
doc.Open();
#region Add Table
PdfPTable table = new PdfPTable(3);
PdfPCell cell = null;
//Specify the font
iTextSharp.text.Font myFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
//Set column widths
float[] widths = new float[] { 100, 300, 100 };
table.SetWidths(widths);
//Add column headers
for (int i = 0; i < myColumns.Count; i++)
{
cell = new PdfPCell(new Phrase(myColumns[i].ColumnName, myFont));
cell.BackgroundColor = new BaseColor(192, 192, 192);
cell.PaddingBottom = 4f;
table.AddCell(cell);
}
//Add rows
for (int i = 0; i < myRows.Count; i++)
{
for (int j = 0; j < myColumns.Count; j++)
{
object CellValue = myRows[i].Cells[j].Value;
//Format the InTotal
if (myColumns[j].ColumnName == "InTotal")
CellValue = ((decimal)CellValue).ToString("C", new System.Globalization.CultureInfo("en-US", true));
cell = new PdfPCell(new Phrase(CellValue.ToString(), myFont));
cell.PaddingBottom = 4f;
table.AddCell(cell);
}
}
doc.Add(table);
#endregion
//Close the PDF document
doc.Close();
//Get Bytes
buffer = myMemoryStream.ToArray();
#endregion
//Set Bytes
e.ExportBytes = buffer;
//Set file name
e.ExportFileName = "ExportData.pdf";
//Set ContentType
e.ExportContentType = "application/pdf";
goto Completed;
}
//Create PNG
if (e.Parameter == "Image")
{
#region Create PNG image
//Declarations
int Angle = 0;
int AngleNext = 0;
int Offset = 20;
int Diameter = 160;
decimal InTotalSum = 0;
object[] arrColor = { Color.Red, Color.Green, Color.Gold,
Color.Maroon, Color.Blue, Color.Gray, Color.Violet, Color.Tan};
//Create image
Bitmap bmp = new Bitmap(420, 200, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bmp);
System.Drawing.Font fnt = new System.Drawing.Font("Arial", 8);
SolidBrush sb = new SolidBrush(Color.Gray);
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.HighQuality;
//Get InTotalSum
for (int i = 0; i < myRows.Count; i++)
InTotalSum += (decimal)myRows[i].Cells["InTotal"].Value;
//Draw pie with labels
for (int i = 0; i < myRows.Count; i++)
{
decimal InTotal = (decimal)myRows[i].Cells["InTotal"].Value;
int Percent = (int)Math.Round((decimal)(InTotal / InTotalSum) * 100);
string CategoryName = (string)myRows[i].Cells["CategoryName"].Value;
AngleNext = AngleNext + Percent;
int Piece = (AngleNext * 360 / 100) - Angle;
g.FillPie(new SolidBrush((Color)arrColor[i]), Offset, Offset, Diameter, Diameter, Angle, Piece); //Add piece of pie
g.FillRectangle(new SolidBrush((Color)arrColor[i]), new System.Drawing.Rectangle(Offset + Diameter + 30, Offset + i * 20, 15, 15)); //Add label box
//Format the InTotal
string InTotalFormatted = InTotal.ToString("C", new System.Globalization.CultureInfo("en-US", true));
g.DrawString(CategoryName + " - " + Percent + "% (" + InTotalFormatted + ")", fnt, sb, Offset + Diameter + 50, Offset + i * 20); //Add label text
Angle = Angle + Piece;
}
//Get Bytes
bmp.Save(myMemoryStream, ImageFormat.Png);
buffer = myMemoryStream.ToArray();
//Clear resources
g.Dispose();
bmp.Dispose();
#endregion
//Set Bytes
e.ExportBytes = buffer;
//Set file name
e.ExportFileName = "ExportData.png";
//Set ContentType
e.ExportContentType = "image/png";
goto Completed;
}
Completed:
//Set Bytes
e.ExportBytes = buffer;
}
}
|