protected void AddTotalsRow(double[] ttlsarray, PdfPTable pdfobj, string lbl, int nrpreccol, int nbridentcols, int NbrQtyCols, int[] colvis, int prec) { float fig = 0; PdfPCell cell; BaseColor bc = BaseColor.LIGHT_GRAY; switch (nrpreccol) { case 0: bc = new BaseColor(Color.Yellow); break; case 3: bc = new BaseColor(Color.LightSteelBlue); break; default: break; } int cspan = nbridentcols - nrpreccol + 1; iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5); // add preceding cells if (nrpreccol > 0) { cell = new PdfPCell(new Phrase("", font5)); cell.BackgroundColor = bc; if (nrpreccol > 1) { cell.Colspan = (nrpreccol - 1); } pdfobj.AddCell(cell); } else { cspan--; } // add label cell cell = new PdfPCell(new Phrase(lbl, font5)); cell.Colspan = cspan; cell.BackgroundColor = bc; pdfobj.AddCell(cell); // add column totals for (int p2 = 0; p2 < NbrQtyCols; p2++) { cell = new PdfPCell(new Phrase(GenUtilities.FormatNbrSpecial(ttlsarray[p2], "", 1, ",", ".", prec), font5)); cell.BackgroundColor = bc; pdfobj.AddCell(cell); } }
public static string GetTotalsRow(double[] totals, string lbl, int NbrPreCols, int LblCol, int prec) { var sb = new StringBuilder(); if (LblCol > 0) { for (int c = 0; c < LblCol; c++) { sb.Append(","); } } sb.Append(lbl + ","); for (int c = LblCol + 1; c < NbrPreCols; c++) { sb.Append(","); } for (int i = 0; i <= 17; i++) { sb.Append("," + GenUtilities.FormatNbrSpecial(totals[i], "", 1, ",", ".", prec)); } return(sb.ToString()); }
public static String ToCSV(DataTable dt, DataTable CDat, int NbrCols, int FirstQtyCol) { int[] colvis = new int[50]; double fig = 0; int FirstNCol = 0; string Color = string.Empty; string Grade = string.Empty; double[] GrandTotals = new double[30]; double[] Ptotal = new double[30]; double[] PAtotal = new double[30]; double[] Stotal = new double[30]; double[] Ttotal = new double[30]; double[] Gtotal = new double[30]; double[] Ctotal = new double[30]; double[] Sttotal = new double[30]; double[] NPtotal = new double[30]; string NoPrint = string.Empty; string prod = string.Empty; string prodA = string.Empty; string FSort = string.Empty; string spec = string.Empty; string Thickness = string.Empty; bool STProd = true; bool STProdA = false; bool STSpec = false; bool STThick = false; bool STGrade = false; bool STColor = false; bool STSort = false; bool STNoP = false; var sb = new StringBuilder(); string s = string.Empty; try { if (dt.Rows.Count > 0) { // grab formatting data string SubTotals = dt.Rows[0]["SubTotals"].ToString(); if (SubTotals.Length > 7) { if (SubTotals.Substring(1, 0) == "0") { STProd = false; } if (SubTotals.Substring(2, 1) == "1") { STProdA = true; } if (SubTotals.Substring(3, 1) == "1") { STSpec = true; } if (SubTotals.Substring(4, 1) == "1") { STThick = true; } if (SubTotals.Substring(5, 1) == "1") { STGrade = true; } if (SubTotals.Substring(6, 1) == "1") { STColor = true; } if (SubTotals.Substring(7, 1) == "1") { STSort = true; } if (SubTotals.Substring(8, 1) == "1") { STNoP = true; } } // establish column visibility //Add Header for (var x = 0; x < NbrCols; x++) { if (Convert.ToInt32(CDat.Rows[x]["IsVisible"]) == 1) { if (x != 0) { sb.Append(","); // (";"); } sb.Append(CDat.Rows[x]["ColumnTitle"].ToString()); if (x < FirstQtyCol) { FirstNCol++; } } } sb.AppendLine(); //Add Rows foreach (DataRow row in dt.Rows) { if (STProd && prod != row["ProductCode"].ToString()) { sb.AppendLine(GetTotalsRow(Ptotal, "Product Totals", FirstNCol, 10, 1)); Ptotal = EmptyItemArray(Ptotal); } if (STProdA && prodA != row["ProductCode"].ToString() + " " + row["ItemLength"].ToString() + " " + row["Color"].ToString() + " " + row["Sort"].ToString() + " " + row["Milling"].ToString() + " " + row["NoPrint"].ToString()) { sb.AppendLine(GetTotalsRow(PAtotal, "Product/Attribute Totals", FirstNCol, 5, 1)); PAtotal = EmptyItemArray(PAtotal); } if (STSpec && spec != row["Specie"].ToString()) { sb.AppendLine(GetTotalsRow(Stotal, "Species Totals", FirstNCol, 0, 1)); Stotal = EmptyItemArray(Stotal); } if (STThick && Thickness != row["Thickness"].ToString()) { sb.AppendLine(GetTotalsRow(Ttotal, "Thickness Totals", FirstNCol, 1, 1)); Ttotal = EmptyItemArray(Ttotal); } if (STGrade && Grade != row["Grade"].ToString()) { sb.AppendLine(GetTotalsRow(Gtotal, "Grade Totals", FirstNCol, 2, 1)); Gtotal = EmptyItemArray(Gtotal); } if (STColor && Color != row["Color"].ToString()) { sb.AppendLine(GetTotalsRow(Ctotal, "Color Totals", FirstNCol, 6, 1)); Ctotal = EmptyItemArray(Ctotal); } if (STSort && FSort != row["Sort"].ToString()) { sb.AppendLine(GetTotalsRow(Sttotal, "Sort Totals", FirstNCol, 7, 1)); Sttotal = EmptyItemArray(Sttotal); } if (STNoP && NoPrint != row["NoPrint"].ToString()) { sb.AppendLine(GetTotalsRow(NPtotal, "Sort Totals", FirstNCol, 9, 1)); NPtotal = EmptyItemArray(NPtotal); } for (var c = 0; c < NbrCols; c++) { if (Convert.ToInt32(CDat.Rows[c]["IsVisible"]) == 1) { if (c != 0) { sb.Append(","); //(";"); } sb.Append(row[dt.Columns[c]]); if (c >= FirstQtyCol) { fig = Convert.ToDouble(row[dt.Columns[c]]); if (STProd) { Ptotal[c - FirstNCol] = fig; } if (STProdA) { PAtotal[c - FirstNCol] = fig; } if (STSpec) { Stotal[c - FirstNCol] = fig; } if (STThick) { Ttotal[c - FirstNCol] = fig; } if (STGrade) { Gtotal[c - FirstNCol] = fig; } if (STColor) { Ctotal[c - FirstNCol] = fig; } if (STSort) { Sttotal[c - FirstNCol] = fig; } if (STNoP) { NPtotal[c - FirstNCol] = fig; } GrandTotals[c - FirstNCol] = fig; } } } sb.AppendLine(); prod = row["ProductCode"].ToString(); prodA = row["ProductCode"].ToString() + " " + row["ItemLength"].ToString() + " " + row["Color"].ToString() + " " + row["Sort"].ToString() + " " + row["Milling"].ToString() + " " + row["NoPrint"].ToString(); spec = row["Specie"].ToString(); Grade = row["Grade"].ToString(); FSort = row["Sort"].ToString(); Color = row["Color"].ToString(); NoPrint = row["NoPrint"].ToString(); Thickness = row["Thickness"].ToString(); } // append grand totals for (var c2 = 0; c2 < NbrCols; c2++) { if (c2 < FirstQtyCol) { if (colvis[c2] == 1) { if (c2 != 0) { sb.Append(","); //(";"); } if (c2 == 0) { sb.Append("Grand Totals"); } else { sb.Append(""); } } } else { sb.Append(GenUtilities.FormatNbrSpecial(GrandTotals[c2 - FirstNCol], "", 1, ",", ".", 0)); } } sb.AppendLine(); } return(sb.ToString()); } catch (Exception ex) { return("Error encounterd: " + ex.Message); } }