using OfficeOpenXml; using OfficeOpenXml.Style; using OfficeOpenXml.Table; using Swift.DAL.BL.Remit.Transaction; using Swift.DAL.SwiftDAL; using Swift.web.Library; using Swift.web.RemittanceSystem.RemittanceReports; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace Swift.web.AccountReport.AccountSummary { public partial class AccountingSummaryDetail : System.Web.UI.Page { private Boolean AllowDrillDown; private string cssClass = "table table-responsive table-bordered table-striped"; private string fieldAlignment = ""; private string fieldFormat = ""; private string fieldFormatExcel = ""; private string fieldWrap = ""; private string tblCaption = ""; private Boolean mergeColumnHead; private ReportResult reportResult = new ReportResult(); private string excludeColumns = ""; private bool includeSerialNo = false; private bool useDBRowColorCode = false; private int subTotalBy = -1; private int totalTextCol = -1; private int subTotalTextCol = -1; private string subTotalFields = ""; private string subTotalText = ""; private string totalFields = ""; private string totalText = ""; private int totalPage = 0; private int pageNo = 0; private double grandTotal = 0.00; private double grandTotalUsd = 0.00; private double grandTotal_1 = 0.00; private string reportName = ""; private int extraCol = 0; private string flag = ""; protected string Url = GetStatic.GetUrlRoot(); private string isExportFull = ""; private int SerialNo = 0; protected void Page_Load(object sender, EventArgs e) { PrintReport(); } private void PrintReport() { string reportName = GetStatic.ReadQueryString("reportName", "").ToLower(); string mode = GetStatic.ReadQueryString("mode", "").ToLower(); isExportFull = mode.ToLower() == "download" ? "Y" : "N"; reportResult = PrepareReport(reportName); if (mode == "download") { string format = GetStatic.ReadQueryString("format", "xls"); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=" + reportName + "." + format); exportDiv.Visible = false; } PrintHead(); PrintFilters(); if (reportResult.ErrorCode != "0") { PrintError(); return; } DataTable dt; int tableCount = reportResult.Result.Tables.Count; if (tableCount > 4) { if (mode != "download") ShowPaging(reportResult.Result.Tables[0]); dt = reportResult.Result.Tables[1]; } else { dt = reportResult.ResultSet; } string reportText = GenerateReport(ref dt); rptDiv.InnerHtml = reportText; } private ReportResult PrepareReport(string reportName) { if (reportName == "accountingsummary") reportResult = PrepareAccountingSummary(); return reportResult; } private ReportResult PrepareAccountingSummary() { string fromDate = GetStatic.ReadQueryString("fromDate", ""); string toDate = GetStatic.ReadQueryString("toDate", ""); mergeColumnHead = true; fieldFormat = "|N|N|N|N|N|N|N|N|N|N|N|N|N|N|N|N|N|N|N|N"; fieldAlignment = "|R|R|R|R|R|R|R|R|R|R|R|R|R|R|R|R|R|R|R|R"; totalFields = "1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20"; totalText = "Total"; return (new TranAgentReportDao().GetAccountingSummary(GetStatic.GetUser(), "RPT", fromDate, toDate)); } private void PrintHead() { Page.Title = reportResult.ReportHead; head.InnerHtml = reportResult.ReportHead; } private void PrintFilters() { var user = GetStatic.GetUser(); var now = GetStatic.GetUserDateTime(); filters.InnerHtml = "Filters Applied :
" + reportResult.Filters + "

Generated On=" + now + " || Generated By=" + user; } private void PrintError() { rptDiv.InnerHtml = "
" + reportResult.Msg + "
"; } private void ShowPaging(DataTable dt) { DataTable dtPaging = dt; int totalRecords = Convert.ToInt32(dtPaging.Rows[0]["TXNCOUNT"].ToString()); int PageSizes = Convert.ToInt32(dtPaging.Rows[0]["PAGESIZE"].ToString()); int PageNumber = Convert.ToInt32(dtPaging.Rows[0]["PAGENUMBER"].ToString()); if (dtPaging.Columns.Count > 3) grandTotal = Convert.ToDouble(dtPaging.Rows[0]["GRANDTOTAL"].ToString()); if (dtPaging.Columns.Count > 4) grandTotalUsd = Convert.ToDouble(dtPaging.Rows[0]["GRANDTOTAL_USD"].ToString()); string cssLink = "pagingLink"; totalPage = totalRecords / PageSizes; if ((totalPage * PageSizes) < totalRecords) totalPage++; var sbPaging = new StringBuilder("
"); sbPaging.AppendLine("
"); sbPaging.AppendLine("Results:  " + totalRecords + " records   "); paging.Visible = true; int currPage = GetPageNumber(); int startPage = (currPage - 5 <= 0 ? 1 : currPage - 5); int offSet = (startPage == 1 ? ((currPage - 5) * -1 + 1) : 0); int endPage = currPage + 4 + offSet; endPage = currPage == 0 ? 10 : endPage; endPage = (endPage > totalPage ? totalPage : endPage); if (currPage > 10 && (endPage - startPage) + 1 != 10) { startPage = startPage - (10 - (endPage - startPage + 1)); } if (totalRecords > PageSizes)// Convert.ToInt32(GetStatic.GetReportPagesize())) { string url = GetURL(); sbPaging.AppendLine("First Page"); sbPaging.AppendLine("Previous Page"); for (int i = startPage; i < endPage + 1; i++) { cssLink = PageNumber == i ? "pagingLinkSelected" : "pagingLink"; sbPaging.AppendLine("" + i + ""); } sbPaging.AppendLine("Next Page"); sbPaging.AppendLine(""); } sbPaging.AppendLine("
"); if (totalRecords > PageSizes) //Convert.ToInt32(GetStatic.GetReportPagesize())) sbPaging.AppendLine("Goto Page: " + GotoList(totalPage)); sbPaging.AppendLine("
"); paging.InnerHtml = sbPaging.ToString(); } private int GetPageNumber() { return Convert.ToInt32(GetStatic.ReadNumericDataFromQueryString("pageNumber") == 0 ? 1 : GetStatic.ReadNumericDataFromQueryString("pageNumber")); } protected string GetURL() { return Request.Url.AbsoluteUri.Replace("&pageNumber=" + GetPageNumber(), ""); } private string GotoList(int totalPage) { StringBuilder sb = new StringBuilder(""); sb.AppendLine(""); return sb.ToString(); } public String GenerateReport(ref DataTable dt) { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us"); var ExcludeFieldList = new ArrayList(); foreach (var col in excludeColumns.Split('|')) { ExcludeFieldList.Add(col.ToLower()); } ExcludeFieldList.Add("rowcolor"); var html = new StringBuilder(""); html.AppendLine("
"); html.AppendLine(""); html.AppendLine(CreateReportHead(ref dt, mergeColumnHead, ref ExcludeFieldList)); html.AppendLine(CreateReportBody(ref dt, subTotalFields, totalFields, ref ExcludeFieldList, totalTextCol, subTotalTextCol)); if (GetStatic.ReadQueryString("mode", "").ToLower() != "download") { html.AppendLine(""); } html.AppendLine("
" + tblCaption + "
"); if (totalPage == 0) totalPage = 1; html.AppendLine("Page " + (GetPageNumber() == 0 ? 1 : GetPageNumber()) + " of " + totalPage + ""); html.AppendLine("
"); html.AppendLine("
"); return html.ToString(); } private String CreateReportHead(ref DataTable dt, Boolean merge, ref ArrayList ExcludeFieldList) { var head = new StringBuilder(""); var SerialNoColumnHead = ""; if (includeSerialNo) { if (GetStatic.ReadQueryString("mode", "").ToLower() == "download") SerialNoColumnHead = "SN."; else SerialNoColumnHead = "SN."; extraCol = 1; } if (!merge) { head.AppendLine(""); head.AppendLine(SerialNoColumnHead); foreach (DataColumn col in dt.Columns) { if (ExcludeFieldList.IndexOf(col.ColumnName.ToLower()) > -1) { extraCol--; continue; } if (GetStatic.ReadQueryString("mode", "").ToLower() == "download") head.AppendLine("" + col.ColumnName + ""); else head.AppendLine("" + col.ColumnName + ""); } head.AppendLine(""); } else { var columns = new Dictionary(); foreach (DataColumn col in dt.Columns) { if (ExcludeFieldList.IndexOf(col.ColumnName.ToLower()) > -1) { extraCol--; continue; } var splitPos = col.ColumnName.IndexOf('_'); if (splitPos == -1) { columns.Add(col.ColumnName, col.ColumnName); } else { var key = col.ColumnName.Substring(0, splitPos); var value = col.ColumnName.Substring(splitPos + 1, col.ColumnName.Length - splitPos - 1); if (!columns.ContainsKey(key)) { columns.Add(key, value); } else { columns[key] = columns[key] + "|" + value; } } } var row1 = ""; var row2 = ""; foreach (var kvp in columns) { string[] values = kvp.Value.Split('|'); if (values.Length == 1) { if (GetStatic.ReadQueryString("mode", "").ToLower() == "download") row1 = row1 + "" + kvp.Key + ""; else row1 = row1 + "" + kvp.Key + ""; } else { if (GetStatic.ReadQueryString("mode", "").ToLower() == "download") row1 = row1 + "" + kvp.Key + ""; else row1 = row1 + "" + kvp.Key + ""; foreach (string value in values) { if (GetStatic.ReadQueryString("mode", "").ToLower() == "download") row2 = row2 + "" + value + ""; else row2 = row2 + "" + value + ""; } } } if (includeSerialNo) { if (GetStatic.ReadQueryString("mode", "").ToLower() == "download") SerialNoColumnHead = "SN."; else SerialNoColumnHead = "SN."; } head.AppendLine("" + SerialNoColumnHead + row1 + ""); head.AppendLine("" + row2 + ""); } return head.ToString(); } private String CreateReportBody(ref DataTable dt, string subTotalFieldList, string totalFieldList, ref ArrayList ExcludeFieldList, int totalTextCol, int subTotalTextCol) { int cnt = 0; var body = new StringBuilder(""); var SerialNoColumnValue = ""; bool doSubTotal = subTotalBy > -1 ? true : false; bool doTotal = totalFieldList != "" ? true : false; string[] totalFieldsArray = totalFieldList.Replace(" ", "").Split('|'); var totalValues = new double[totalFieldsArray.Length]; string[] subTotalFieldsArray = subTotalFieldList.Replace(" ", "").Split('|'); var subTotalValues = new double[subTotalFieldsArray.Length]; string[] fieldFormatList = fieldFormat.Replace(" ", "").Split('|'); string tmpSubTotalText = "||"; var hasRowColorCol = dt.Columns.Contains("rowColor"); foreach (DataRow row in dt.Rows) { if (includeSerialNo) { SerialNo++; SerialNoColumnValue = "" + SerialNo.ToString() + ""; } else { SerialNoColumnValue = ""; } if (doSubTotal) { if (tmpSubTotalText == "||") tmpSubTotalText = row[subTotalBy].ToString(); if (tmpSubTotalText != row[subTotalBy].ToString()) { body.AppendLine(CreateTotalRow(ref dt, subTotalText, subTotalBy, subTotalFieldsArray, subTotalValues, fieldFormatList, fieldAlignment, fieldWrap, ref ExcludeFieldList, subTotalTextCol, includeSerialNo)); tmpSubTotalText = row[subTotalBy].ToString(); for (int i = 0; i < subTotalValues.Length; i++) { subTotalValues[i] = 0; } } } if (useDBRowColorCode && hasRowColorCol) { body.AppendLine(""); } else { body.AppendLine(++cnt % 2 == 1 ? "" : ""); } body.AppendLine(SerialNoColumnValue); for (int i = 0; i < dt.Columns.Count; i++) { if (ExcludeFieldList.IndexOf(dt.Columns[i].ColumnName.ToLower()) > -1) { continue; } string format = GetFormat(fieldFormatList, i); string data = row[i].ToString(); if (format != "") { double dataParse; double.TryParse(row[i].ToString(), out dataParse); data = dataParse < 0 ? GetStatic.ParseMinusValue(dataParse) : dataParse.ToString(format); //Parse Minus Value } if (AllowDrillDown) { data = CreateLink(data); } string alignment = GetAlignment(fieldAlignment, i); string noWrapProperty = GetNoWrapping(fieldWrap, i); if (GetStatic.ReadQueryString("mode", "").ToLower() == "download") body.AppendLine("" + data + ""); else body.AppendLine("" + data + ""); var data2 = row[i].ToString(); if (doTotal) { int pos = Array.IndexOf(totalFieldsArray, i.ToString()); if (pos >= 0) { double value; double.TryParse(GetStatic.RemoveHtmlTagsRegex(data2), out value); totalValues[pos] = totalValues[pos] + value; } } if (doSubTotal) { int pos = Array.IndexOf(subTotalFieldsArray, i.ToString()); if (pos >= 0) { if (data2.IndexOf('<') > -1) { data2 = GetStatic.RemoveAllTags(data2); } double value; double.TryParse(GetStatic.RemoveHtmlTagsRegex(data2), out value); subTotalValues[pos] = subTotalValues[pos] + value; } } } body.AppendLine(""); } if (doSubTotal) { body.AppendLine(CreateTotalRow(ref dt, subTotalText, subTotalBy, subTotalFieldsArray, subTotalValues, fieldFormatList, fieldAlignment, fieldWrap, ref ExcludeFieldList, totalTextCol, includeSerialNo)); } if (doTotal) { if (grandTotal != 0.00) { if (totalPage == GetPageNumber()) body.AppendLine(CreatGrandTotalRow(ref dt, totalText, 0, totalFieldsArray, grandTotal, fieldFormatList, fieldAlignment, fieldWrap, grandTotal, grandTotalUsd, grandTotal_1, ref ExcludeFieldList, includeSerialNo)); } else { body.AppendLine(CreateTotalRow(ref dt, totalText, 0, totalFieldsArray, totalValues, fieldFormatList, fieldAlignment, fieldWrap, ref ExcludeFieldList, totalTextCol, includeSerialNo)); } } return body.ToString(); } private static String CreateTotalRow(ref DataTable dt, string totalText, int totalFieldIndex, string[] totalFields, Double[] totalValues, string[] fieldFormatList, string fieldAlignmentList, string fieldWrapList, ref ArrayList ExcludeFieldList, int totalTextCol, bool includeSerialNo) { var rowText = new StringBuilder(""); rowText.AppendLine(""); if (includeSerialNo) { if (totalText.IndexOf("") == -1) rowText.AppendLine(""); } for (int i = 0; i < dt.Columns.Count; i++) { if (ExcludeFieldList.IndexOf(dt.Columns[i].ColumnName.ToLower()) > -1) { continue; } int pos = Array.IndexOf(totalFields, i.ToString()); string data = ""; string alignment = ""; string nowrapProperty = ""; if (pos >= 0) { string format = GetFormat(fieldFormatList, i); data = totalValues[pos] < 0 ? GetStatic.ParseMinusValue(totalValues[pos]) : totalValues[pos].ToString(format.ToUpper()); alignment = GetAlignment(fieldAlignmentList, i); nowrapProperty = GetNoWrapping(fieldWrapList, i); } if (totalTextCol > -1) { totalFieldIndex = totalTextCol; } if (i == totalFieldIndex) data = totalText; rowText.AppendLine("" + data + ""); } rowText.AppendLine(""); return rowText.ToString(); } private static String CreatGrandTotalRow(ref DataTable dt, string totalText, int totalFieldIndex, string[] totalFields, Double totalValues, string[] fieldFormatList, string fieldAlignmentList, string fieldWrapList, Double grandTotal, Double grandTotalUsd, Double grandTotal_1, ref ArrayList ExcludeFieldList, bool includeSerialNo) { var rowText = new StringBuilder(""); rowText.AppendLine(""); if (includeSerialNo) { if (totalText.IndexOf("") == -1) rowText.AppendLine(""); } for (int i = 0; i < dt.Columns.Count; i++) { if (ExcludeFieldList.IndexOf(dt.Columns[i].ColumnName.ToLower()) > -1) { continue; } int pos = Array.IndexOf(totalFields, i.ToString()); string data = ""; string alignment = ""; string nowrapProperty = ""; if (pos >= 0) { data = GetStatic.ParseMinusValue(grandTotal.ToString()); alignment = GetAlignment(fieldAlignmentList, i); nowrapProperty = GetNoWrapping(fieldWrapList, i); } if (i == totalFieldIndex) data = totalText; if (i == 9 && grandTotal != 0.00) { rowText.AppendLine("" + GetStatic.ParseMinusValue(grandTotal) + ""); } else if (i == 11 && grandTotalUsd != 0.00) { rowText.AppendLine("" + GetStatic.ParseMinusValue(grandTotalUsd) + ""); } else if (i == 13 && grandTotal_1 != 0.00) { rowText.AppendLine("" + GetStatic.ParseMinusValue(grandTotal_1) + ""); } else { rowText.AppendLine("" + data + ""); } } rowText.AppendLine(""); return rowText.ToString(); } private static string GetAlignment(string fieldAlignmentList, int currFieldIndex) { if (fieldAlignmentList == "") return ""; string[] alignListArray = fieldAlignmentList.Split('|'); string alignName = alignListArray.Length > currFieldIndex ? alignListArray[currFieldIndex] : ""; string align = ""; switch (alignName.ToUpper()) { case "R": align = " align=\"right\""; break; case "L": align = " align=\"left\""; break; case "C": align = " align=\"center\""; break; default: break; } return align; } private static string GetNoWrapping(string fieldWrapList, int currFieldIndex) { if (fieldWrapList == "") return ""; string[] wrapListArray = fieldWrapList.Split('|'); string isWrap = wrapListArray.Length > currFieldIndex ? wrapListArray[currFieldIndex] : ""; string noWrapValue = ""; if (isWrap == "Y") noWrapValue = " nowrap = \"nowrap\""; return noWrapValue; } private static string GetFormat(string[] fieldFormatList, int currFieldIndex) { return fieldFormatList.Length > currFieldIndex ? fieldFormatList[currFieldIndex] : ""; //return ( pos == -1 ? "": fieldFormatList[pos]); } private static string CreateLink(string data) { return ""; } protected void export_Click(object sender, EventArgs e) { ReportDownload rds = new ReportDownload(); string path = rds.PrepareAccountingSummaryReport(); Response.Redirect(path); //string fromDate = GetStatic.ReadQueryString("fromDate", ""); //string toDate = GetStatic.ReadQueryString("toDate", ""); //DataSet dataset = (new TranAgentReportDao().GetAccountingSummary(GetStatic.GetUser(), "RPT", fromDate, toDate)).Result; //DataSetToExcelNew(dataset, Response, "AccountingReport"); //TestingTesting(); } public void DataSetToExcelNew(DataSet dtExport, System.Web.HttpResponse response, string strFileName) { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; ExcelPackage excelPackg = new ExcelPackage(); string fileName = strFileName; // Guid.NewGuid().ToString(); var formatArr = fieldFormatExcel.Split('|'); ExcelWorksheet workSheet = excelPackg.Workbook.Worksheets.Add("Sheet1"); //using (ExcelRange Rng = workSheet.Cells["A1:U500"]) //{ // ExcelTableCollection tblcollection = workSheet.Tables; // ExcelTable table = tblcollection.Add(Rng, strFileName); // //Set Columns position & name //} PrepareHeaderOfAccountingSummary(workSheet); int j = 4; foreach (DataRow item in dtExport.Tables[0].Rows) { using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(0, j)]) { Rng.Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.ShortDatePattern; Rng.Value = GetStatic.ParseDateTime(item["TXN DATE"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(1, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE SENT_JME SENT"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(2, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE SENT_DONGA SENT"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(3, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE SENT_TF SENT"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(4, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE SENT_TOTAL SENT"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(5, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE CANCELLED_JME CANCELLED"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(6, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE CANCELLED_DONGA CANCELLED"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(7, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE CANCELLED_TF CANCELLED"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(8, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["REMITTANCE CANCELLED_TOTAL CANCELLED"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(9, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["SERVICE CHARGE_JME SC"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(10, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["SERVICE CHARGE_DONGA SC"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(11, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["SERVICE CHARGE_TF SC"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(12, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["SERVICE CHARGE_TOTAL SC"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(13, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["COMMISSION_JME COMM"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(14, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["COMMISSION_DONGA COMM"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(15, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["COMMISSION_TF COMM"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(16, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["COMMISSION_TOTAL COMM"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(17, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["FOREX INCOME_JME FX"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(18, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["FOREX INCOME_DONGA FX"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(19, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["FOREX INCOME_TF FX"].ToString()); } using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(20, j)]) { Rng.Style.Numberformat.Format = "#,##0.00"; Rng.Value = GetStatic.ParseDouble(item["FOREX INCOME_TOTAL FX"].ToString()); } j++; } var totalRow = dtExport.Tables[0].Rows.Count + 4; var totalColumn = 21; SetTotalFormula(workSheet, totalRow, totalColumn); workSheet.Cells[workSheet.Dimension.Address].AutoFitColumns(); string path = GetStatic.ReadWebConfig("root", "") + @"\ExcelSample\" + GetStatic.GetUser() + @"\"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); File.Delete(path + fileName + ".xlsx"); excelPackg.SaveAs(new FileInfo(path + fileName + ".xlsx")); Response.Redirect(GetStatic.GetUrlRoot() + @"\ExcelSample\" + GetStatic.GetUser() + @"\" + fileName + ".xlsx"); } private string GetCellRangeNew(int i, int j) { if (i == 0) return "A" + j.ToString(); else if (i == 1) return "B" + j.ToString(); else if (i == 2) return "C" + j.ToString(); else if (i == 3) return "D" + j.ToString(); else if (i == 4) return "E" + j.ToString(); else if (i == 5) return "F" + j.ToString(); else if (i == 6) return "G" + j.ToString(); else if (i == 7) return "H" + j.ToString(); else if (i == 8) return "I" + j.ToString(); else if (i == 9) return "J" + j.ToString(); else if (i == 10) return "K" + j.ToString(); else if (i == 11) return "L" + j.ToString(); else if (i == 12) return "M" + j.ToString(); else if (i == 13) return "N" + j.ToString(); else if (i == 14) return "O" + j.ToString(); else if (i == 15) return "P" + j.ToString(); else if (i == 16) return "Q" + j.ToString(); else if (i == 17) return "R" + j.ToString(); else if (i == 18) return "S" + j.ToString(); else if (i == 19) return "T" + j.ToString(); else if (i == 20) return "U" + j.ToString(); else if (i == 21) return "V" + j.ToString(); else if (i == 22) return "W" + j.ToString(); else if (i == 23) return "X" + j.ToString(); else if (i == 24) return "Y" + j.ToString(); else if (i == 25) return "Z" + j.ToString(); else if (i == 26) return "AA" + j.ToString(); else if (i == 27) return "AB" + j.ToString(); else if (i == 28) return "AC" + j.ToString(); else if (i == 29) return "AD" + j.ToString(); else if (i == 30) return "AE" + j.ToString(); else if (i == 31) return "AF" + j.ToString(); else if (i == 32) return "AG" + j.ToString(); else if (i == 33) return "AH" + j.ToString(); else if (i == 34) return "AI" + j.ToString(); else if (i == 35) return "AJ" + j.ToString(); else if (i == 36) return "AK" + j.ToString(); else if (i == 37) return "AL" + j.ToString(); else return "AM" + j.ToString(); } public void PrepareHeaderOfAccountingSummary(ExcelWorksheet workSheet) { using (ExcelRange Rng = workSheet.Cells["A1:U1"]) { Rng.Value = "Accounting Summary"; Rng.Merge = true; Rng.Style.Font.Size = 11; Rng.Style.Font.Bold = true; Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left; } using (ExcelRange Rng = workSheet.Cells["A2"]) { Rng.Value = "Date"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["B2:E2"]) { Rng.Value = "Remittance Send Tamt"; Rng.Merge = true; Rng.Style.Font.Size = 11; Rng.Style.Font.Bold = true; Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; } using (ExcelRange Rng = workSheet.Cells["F2:I2"]) { Rng.Value = "Remittance Cancelled Tamt"; Rng.Merge = true; Rng.Style.Font.Size = 11; Rng.Style.Font.Bold = true; Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; } using (ExcelRange Rng = workSheet.Cells["J2:M2"]) { Rng.Value = "Service Charge"; Rng.Merge = true; Rng.Style.Font.Size = 11; Rng.Style.Font.Bold = true; Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; } using (ExcelRange Rng = workSheet.Cells["N2:Q2"]) { Rng.Value = "Commission"; Rng.Merge = true; Rng.Style.Font.Size = 11; Rng.Style.Font.Bold = true; Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; } using (ExcelRange Rng = workSheet.Cells["R2:U2"]) { Rng.Value = "Forex Income"; Rng.Merge = true; Rng.Style.Font.Size = 11; Rng.Style.Font.Bold = true; Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; } using (ExcelRange Rng = workSheet.Cells["B3"]) { Rng.Value = "JME Nepal"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["C3"]) { Rng.Value = "DongA"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["D3"]) { Rng.Value = "Transfast"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["E3"]) { Rng.Value = "Total"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["F3"]) { Rng.Value = "JME Nepal"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["G3"]) { Rng.Value = "DongA"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["H3"]) { Rng.Value = "Transfast"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["I3"]) { Rng.Value = "Total"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["J3"]) { Rng.Value = "JME Nepal"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["K3"]) { Rng.Value = "DongA"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["L3"]) { Rng.Value = "Transfast"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["M3"]) { Rng.Value = "Total"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["N3"]) { Rng.Value = "JME Nepal"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["O3"]) { Rng.Value = "DongA"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["P3"]) { Rng.Value = "Transfast"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["Q3"]) { Rng.Value = "Total"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["R3"]) { Rng.Value = "JME Nepal"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["S3"]) { Rng.Value = "DongA"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["T3"]) { Rng.Value = "Transfast"; Rng.Style.Font.Bold = true; } using (ExcelRange Rng = workSheet.Cells["U3"]) { Rng.Value = "Total"; Rng.Style.Font.Bold = true; } } public void SetTotalFormula(ExcelWorksheet workSheet, int endingIndex, int totalColumn) { SetFormatForTotal(workSheet, endingIndex, totalColumn); CalaulateTotal(workSheet, endingIndex, totalColumn); } public void SetFormatForTotal(ExcelWorksheet workSheet, int endingIndex, int totalColumn) { using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(0, endingIndex)]) { Rng.Style.Numberformat.Format = "@"; Rng.Value = "Total"; Rng.Style.Font.Bold = true; } for (int i = 1; i < totalColumn; i++) { using (ExcelRange Rng = workSheet.Cells[GetCellRangeNew(i, endingIndex)]) { Rng.Style.Numberformat.Format = "#,##0.00"; ; Rng.Style.Font.Bold = true; } } } public void CalaulateTotal(ExcelWorksheet workSheet, int endingIndex, int totalColumn) { for (int i = 1; i < totalColumn; i++) { workSheet.Cells[GetCellRangeNew(i, endingIndex)].Formula = "=SUM(" + GetCellRangeNew(i, 4) + ":" + GetCellRangeNew(i, endingIndex - 1) + ")"; } } public void TestingTesting() { string fromDate = GetStatic.ReadQueryString("fromDate", ""); string toDate = GetStatic.ReadQueryString("toDate", ""); mergeColumnHead = true; var ExcludeFieldList = new ArrayList(); foreach (var col in excludeColumns.Split('|')) { ExcludeFieldList.Add(col.ToLower()); } DataTable dt; ExcludeFieldList.Add("rowcolor"); ReportDownload rd = new ReportDownload(); reportResult = PrepareReport(reportName); dt = reportResult.ResultSet; var html = new StringBuilder(""); html.AppendLine("
"); html.AppendLine(""); html.AppendLine(CreateReportHead(ref dt, mergeColumnHead, ref ExcludeFieldList)); html.AppendLine("
" + tblCaption + "
"); html.AppendLine("
"); rptDiv.InnerHtml = html.ToString(); ReportDownload rds = new ReportDownload(); //string path = rds.PrepareAccountingSummaryReport(); string path = HtmlToExcelWorksheet(html.ToString()); Response.Redirect(path); //string format = "xls"; //Response.Clear(); //Response.ClearContent(); //Response.ClearHeaders(); //Response.ContentType = "application/vnd.ms-excel"; //Response.AddHeader("Content-Disposition", "attachment; filename=" + "test" + "." + format); //exportDiv.Visible = false; } public string HtmlToExcelWorksheet(string html) { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; ExcelPackage excelPackg = new ExcelPackage(); string fileName = "Myworksheet"; // Guid.NewGuid().ToString(); ExcelWorksheet workSheet = excelPackg.Workbook.Worksheets.Add("Sheet1"); //workSheet.Cells["A1"].LoadFromDataTable(GetStatic.ConvertHTMLTableToDataSet(html),true); //using (ExcelRange Rng = workSheet.Cells["A1:U1"]) //{ // Rng.Value = "Accounting Summary"; // Rng.Merge = true; // Rng.Style.Font.Size = 11; // Rng.Style.Font.Bold = true; // Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Left; //} workSheet.Cells[workSheet.Dimension.Address].AutoFitColumns(); string path = GetStatic.ReadWebConfig("root", "") + @"\ExcelSample\" + GetStatic.GetUser() + @"\"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); File.Delete(path + fileName + ".xlsx"); excelPackg.SaveAs(new FileInfo(path + fileName + ".xlsx")); return GetStatic.GetUrlRoot() + @"\ExcelSample\" + GetStatic.GetUser() + @"\" + fileName + ".xlsx"; } } }