// Event handler for ImageButton . public virtual void UOMExportExcelButton_Click(object sender, ImageClickEventArgs args) { try { // Enclose all database retrieval/update code within a Transaction boundary DbUtils.StartTransaction(); // To customize the columns or the format, override this function in Section 1 of the page // and modify it to your liking. // Build the where clause based on the current filter and search criteria // Create the Order By clause based on the user's current sorting preference. WhereClause wc = null; wc = CreateWhereClause(); OrderBy orderBy = null; orderBy = CreateOrderBy(); bool done = false; string val = ""; CompoundFilter join = CreateCompoundJoinFilter(); // Read pageSize records at a time and write out the Excel file. int totalRowsReturned = 0; this.TotalRecords = UOMTable.GetRecordCount(join, wc); if (this.TotalRecords > 10000) { // Add each of the columns in order of export. BaseColumn[] columns = new BaseColumn[] { UOMTable.UOMName, UOMTable.UOMDescription, UOMTable.Status, null}; ExportDataToCSV exportData = new ExportDataToCSV(UOMTable.Instance,wc,orderBy,columns); exportData.StartExport(this.Page.Response, true); DataForExport dataForCSV = new DataForExport(UOMTable.Instance, wc, orderBy, columns,join); // Read pageSize records at a time and write out the CSV file. while (!done) { ArrayList recList = dataForCSV.GetRows(exportData.pageSize); if (recList == null) break; //we are done totalRowsReturned = recList.Count; foreach (BaseRecord rec in recList) { foreach (BaseColumn col in dataForCSV.ColumnList) { if (col == null) continue; if (!dataForCSV.IncludeInExport(col)) continue; val = rec.GetValue(col).ToString(); exportData.WriteColumnData(val, dataForCSV.IsString(col)); } exportData.WriteNewRow(); } // If we already are below the pageSize, then we are done. if (totalRowsReturned < exportData.pageSize) { done = true; } } exportData.FinishExport(this.Page.Response); } else { // Create an instance of the ExportDataToExcel class with the table class, where clause and order by. ExportDataToExcel excelReport = new ExportDataToExcel(UOMTable.Instance, wc, orderBy); // Add each of the columns in order of export. // To customize the data type, change the second parameter of the new ExcelColumn to be // a format string from Excel's Format Cell menu. For example "dddd, mmmm dd, yyyy h:mm AM/PM;@", "#,##0.00" if (this.Page.Response == null) return; excelReport.CreateExcelBook(); int width = 0; int columnCounter = 0; DataForExport data = new DataForExport(UOMTable.Instance, wc, orderBy, null,join); data.ColumnList.Add(new ExcelColumn(UOMTable.UOMName, "Default")); data.ColumnList.Add(new ExcelColumn(UOMTable.UOMDescription, "Default")); data.ColumnList.Add(new ExcelColumn(UOMTable.Status, "Default")); // First write out the Column Headers foreach (ExcelColumn col in data.ColumnList) { width = excelReport.GetExcelCellWidth(col); if (data.IncludeInExport(col)) { excelReport.AddColumnToExcelBook(columnCounter, col.ToString(), excelReport.GetExcelDataType(col), width, excelReport.GetDisplayFormat(col)); columnCounter++; } } while (!done) { ArrayList recList = data.GetRows(excelReport.pageSize); if (recList == null) { break; } totalRowsReturned = recList.Count; foreach (BaseRecord rec in recList) { excelReport.AddRowToExcelBook(); columnCounter = 0; foreach (ExcelColumn col in data.ColumnList) { if (!data.IncludeInExport(col)) continue; Boolean _isExpandableNonCompositeForeignKey = col.DisplayColumn.TableDefinition.IsExpandableNonCompositeForeignKey(col.DisplayColumn); if (_isExpandableNonCompositeForeignKey && col.DisplayColumn.IsApplyDisplayAs) { val = UOMTable.GetDFKA(rec.GetValue(col.DisplayColumn).ToString(), col.DisplayColumn, null); if (String.IsNullOrEmpty(val)) { val = rec.Format(col.DisplayColumn); } } else val = excelReport.GetValueForExcelExport(col, rec); excelReport.AddCellToExcelRow(columnCounter, excelReport.GetExcelDataType(col), val); columnCounter++; } } // If we already are below the pageSize, then we are done. if (totalRowsReturned < excelReport.pageSize) { done = true; } } excelReport.SaveExcelBook(this.Page.Response); } } catch (Exception ex) { // Upon error, rollback the transaction this.Page.RollBackTransaction(sender); this.Page.ErrorOnPage = true; // Report the error message to the end user BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "BUTTON_CLICK_MESSAGE", ex.Message); } finally { DbUtils.EndTransaction(); } }
public ExportData(BaseTable tbl, WhereClause wc, OrderBy orderBy) { _exportDataToCSV = new ExportDataToCSV(tbl, wc, orderBy); _exportDataToExcel = new ExportDataToExcel(tbl, wc, orderBy); }