protected void btnExportToExcel_Click(object sender, EventArgs e) { try { string FileName = "AR_Aging_"; DateTime now = DateTime.Now; FileName = FileName + now.ToString("MM_dd_yyyy"); // handle filter parameters string artype = this.lbxARTypeF.SelectedValue; string branch = this.lbxBranchYardF.SelectedValue; string country = this.lbxCountryF.SelectedValue; string contr = this.lbxControllerF.SelectedValue; string creditgrp = this.lbxCreditGroupF.SelectedValue; string curr = this.ddlCurrencyF.SelectedValue; string cust = this.txtCustomerF.Text; string custnbr = this.txtCustNbrF.Text; string payterm = this.lbxPaymentTermsF.SelectedValue; string salesgrp = this.lbxSalesGroupF.SelectedValue; string salespers = this.lbxSalesPersonF.SelectedValue; string Src = this.ddlSourceTypeF.SelectedValue; int Threshold = Convert.ToInt32(this.ddlThresholdF.SelectedValue); string sBegin = this.txtBeginInvDateF.Text; DateTime? asofdate = null; DateTime? bdate = null; if (sBegin.Length > 0) { bdate = Convert.ToDateTime(sBegin); } // load aging data InvoiceLib inv = new InvoiceLib(); DataTable dt = inv.SelectARAgingData("01", creditgrp, salespers, cust, custnbr, artype, salesgrp, contr, payterm, country, branch, curr, asofdate, Threshold, bdate, Src, 0, _UserID); if (dt.Rows.Count > 0 ) { string tab = ""; Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; foreach (DataColumn dc in dt.Columns) { Response.Write(tab + dc.ColumnName); tab = "\t"; } Response.Write("\n"); int i; foreach (DataRow dr in dt.Rows) { tab = ""; for (i = 0; i < dt.Columns.Count; i++) { Response.Write(tab + dr[i].ToString()); tab = "\t"; } Response.Write("\n"); } Response.Flush(); Response.End(); } else { this.lblErrorMsg.Text = "No rows could be extracted that matched your filtering criteria."; } } catch (Exception ex) { this.lblErrorMsg.Text = "Error when exporting to Excel: " + ex.Message; } }
protected void btnExportToPDF_Click(object sender, EventArgs e) { this.lblErrorMsg.Text = ""; try { // handle filter parameters string artype = this.lbxARTypeF.SelectedValue; string branch = this.lbxBranchYardF.SelectedValue; string country = this.lbxCountryF.SelectedValue; string contr = this.lbxControllerF.SelectedValue; string creditgrp = this.lbxCreditGroupF.SelectedValue; string curr = this.ddlCurrencyF.SelectedValue; string cust = this.txtCustomerF.Text; string custnbr = this.txtCustNbrF.Text; string payterm = this.lbxPaymentTermsF.SelectedValue; string salesgrp = this.lbxSalesGroupF.SelectedValue; string salespers = this.lbxSalesPersonF.SelectedValue; string Src = this.ddlSourceTypeF.SelectedValue; int Threshold = Convert.ToInt32(this.ddlThresholdF.SelectedValue); string sBegin = this.txtBeginInvDateF.Text; DateTime? asofdate = null; DateTime? bdate = null; if (sBegin.Length > 0) { bdate = Convert.ToDateTime(sBegin); } // load aging data InvoiceLib inv = new InvoiceLib(); DataTable dt = inv.SelectARAgingData("01", creditgrp, salespers, cust, custnbr, artype, salesgrp, contr, payterm, country, branch, curr, asofdate, Threshold, bdate, Src, 0, _UserID); if (dt.Rows.Count > 0) { string FileName = "AR_Aging_"; DateTime now = DateTime.Now; FileName = FileName + now.ToString("MM_dd_yyyy"); Document pdfDoc = new Document(PageSize.A4.Rotate(), 30, 30, 40, 25); System.IO.MemoryStream mStream = new System.IO.MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream); int cols = dt.Columns.Count; int rows = dt.Rows.Count; iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5); pdfDoc.Open(); PdfPTable pdfTable = new PdfPTable(cols); pdfTable.DefaultCell.BorderWidth = 1; pdfTable.WidthPercentage = 100; pdfTable.DefaultCell.Padding = 1; PdfPRow row = null; float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f }; pdfTable.SetWidths(widths); pdfTable.WidthPercentage = 100; string colname = ""; string parag = "CAT Availability dated " + now.ToString("MM/dd/yyyy"); PdfPCell Tcell = new PdfPCell(new Phrase(parag)); Tcell.Colspan = dt.Columns.Count; Tcell.HorizontalAlignment = Element.ALIGN_CENTER; // Add header title pdfTable.AddCell(Tcell); PdfPCell cell; // Add column titles foreach (DataColumn c in dt.Columns) { pdfTable.AddCell(new Phrase(c.ColumnName, font5)); } // Add all rows to table if (dt.Rows.Count > 0) { foreach (DataRow r in dt.Rows) { // Add Column by column for each row for (int i = 0; i < dt.Columns.Count; i++) { pdfTable.AddCell(new Phrase(r[i].ToString(), font5)); } } } pdfDoc.Add(pdfTable); pdfDoc.Close(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".pdf"); Response.Clear(); Response.BinaryWrite(mStream.ToArray()); Response.End(); } else { this.lblErrorMsg.Text = "No rows could be extracted that matched your filtering criteria."; } } catch (DocumentException de) { this.lblErrorMsg.Text = "Error while generating PDF: " + de.Message; } catch (IOException ioEx) { this.lblErrorMsg.Text = "Error while generating PDF: " + ioEx.Message; } catch (Exception ex) { this.lblErrorMsg.Text = "Error when exporting to PDF: " + ex.Message; } }
// ****************************************************************************** protected void LoadAgingList(int LType) { string Msg = "Error loading data grid data: "; try { // handle filter parameters string artype = this.lbxARTypeF.SelectedValue; string branch = this.lbxBranchYardF.SelectedValue; string country = this.lbxCountryF.SelectedValue; string contr = this.lbxControllerF.SelectedValue; string creditgrp = this.lbxCreditGroupF.SelectedValue; string curr = this.ddlCurrencyF.SelectedValue; string cust = this.txtCustomerF.Text; string custnbr = this.txtCustNbrF.Text; string payterm = this.lbxPaymentTermsF.SelectedValue; string salesgrp = this.lbxSalesGroupF.SelectedValue; string salespers = this.lbxSalesPersonF.SelectedValue; string Src = this.ddlSourceTypeF.SelectedValue; int Threshold = Convert.ToInt32(this.ddlThresholdF.SelectedValue); string sBegin = this.txtBeginInvDateF.Text; DateTime? asofdate = null; DateTime? bdate = null; if (sBegin.Length > 0) { bdate = Convert.ToDateTime(sBegin); } ViewState["ARTypeF"] = artype; ViewState["BranchYardF"] = branch; ViewState["CountryF"] = country; ViewState["ControllerF"] = contr; ViewState["CreditGroupF"] = creditgrp; ViewState["CurrencyF"] = curr; ViewState["CustomerF"] = cust; ViewState["PayTermsF"] = payterm; ViewState["SalesGroupF"] = salesgrp; ViewState["SalesPersonF"] = salespers; ViewState["SourceTypeF"] = Src; ViewState["ThresholdF"] = Threshold.ToString(); // reset to first page if necessary if (LType == 1) { _PgNbr = 0; ViewState["PgNbr"] = 0; gvInvoiceList.PageIndex = 0; } // load aging data InvoiceLib inv = new InvoiceLib(); DataTable dt = new DataTable(); dt = inv.SelectARAgingData("01", creditgrp, salespers, cust, custnbr, artype, salesgrp, contr, payterm, country, branch, curr, asofdate, Threshold, bdate, Src, 0, _UserID); //string Company, string CreditGrp, string SalesPers, string Cust, string ARType, string SalesGrp, string Controller, string PayTerms, string Country, string Branch, string Currency, DateTime? AsOfDate, string Src, int Sort, int ByI Msg = "Error binding data grid: "; this.gvInvoiceList.DataSource = dt; this.gvInvoiceList.DataBind(); if (dt.Rows.Count < 1) { this.lblErrorMsg.Text = "No AR Aging data was returned that match all of the filters you have set."; } } catch (Exception ex) { this.lblErrorMsg.Text = Msg + ex.Message; } }