// The class constructor public DataGridViewPrinter(DataGridViewPersonalizat pDGV, StructPaletaDGV pPaleta, PrintDocument aPrintDocument, bool CenterOnPage, bool WithTitle, string aTitleText, bool pImprimaTitlulPeFiecarePagina, Font aTitleFont, Color aTitleColor, bool WithPaging, List <string> pListaColoane, Image logo, string pHeader, string pFooter) { this.lDGVdePrintat = pDGV; this.lPaleta = pPaleta; this.ThePrintDocument = aPrintDocument; this.IsCenterOnPage = CenterOnPage; this.IsWithTitle = WithTitle; this.TheTitleText = aTitleText; this.TheTitleFont = aTitleFont; this.TheTitleColor = aTitleColor; this.IsWithPaging = WithPaging; this.lImprimaTitlulPeFiecarePagina = pImprimaTitlulPeFiecarePagina; this.lLogo = logo; this.lHeader = pHeader; this.lFooter = pFooter; if (CUtil.EsteListaVida <string>(pListaColoane)) { this.lListaColoaneImprimare = new List <string>(); foreach (DataGridViewColumn coloana in pDGV.Columns) { if (!pDGV.SeIgnoraColoanaLaImprimare(coloana.Name)) { this.lListaColoaneImprimare.Add(coloana.Name); } } } else { this.lListaColoaneImprimare = pListaColoane; } PageNumber = 0; RowsHeight = new Dictionary <int, float>(); ColumnsWidth = new Dictionary <int, float>(); mColumnPoints = new List <int[]>(); mColumnPointsWidth = new List <float>(); // Claculating the PageWidth and the PageHeight if (!ThePrintDocument.DefaultPageSettings.Landscape) { PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width; PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height; } else { PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Width; PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Height; } // Calculating the page margins LeftMargin = ThePrintDocument.DefaultPageSettings.Margins.Left; TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top; RightMargin = ThePrintDocument.DefaultPageSettings.Margins.Right; BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom; //Fie cele bifate, fie toate listaLiniiSelectate = lDGVdePrintat.GetListaLiniiSelectate(); listaIndecsiColoanePrint = new List <int>(); listaIndecsiLiniiSelectate = new List <int>(); foreach (DataGridViewRow linie in listaLiniiSelectate) { listaIndecsiLiniiSelectate.Add(linie.Index); } // First, the current row to be printed is the first row in the DataGridView control CurrentRow = 0; }
/// <summary> /// Export the DataGridView to Comma Separated Value file. /// This method should be used with the methods: /// CsvThread(). /// GetCsvReady(). /// </summary> /// <param name="dGV">Extended DataGridView.</param> /// <param name="filename">Full path & name of the Name of csv file.</param> void ToCsV(DataGridViewPersonalizat dGV, string filename) { if (string.IsNullOrEmpty(filename)) { return; } string valoareCelula = string.Empty; using (StreamWriter myFile = new StreamWriter(filename, false, Encoding.Default)) { //IMPORTANT!!! //Separatorul CVS pentru tarile europene este ; si nu , care este rezervat pentru zecimale myFile.WriteLine(this.lDenumireDocument); // Export only visible columns. //if (dGV.ExportVisibleColumnsOnly) //{ // Export titles: StringBuilder sHeaders = new StringBuilder(); List <int> listaIndecsiColoaneExportabile = new List <int>(); string[] culturiVirgula = { "ar-JO", "ar-SY", "az-Cyrl-AZ", "az-Latn-AZ", "zh-CN", "zh-SG", "zh-TW", "nl-BE", "nl-NL", "en-BZ", "en-CA", "en-NZ", "en-US", "fr-CA", "ms-BN", "ms-MY", "nb-NO", "nn-NO", "pt-BR", "sr-Cyrl-CS", "sr-Latn-CS", "es-AR", "es-DO", "es-MX", "es-ES_tradnl", "sv-FI", "sv-SE", "uz-Cyrl-UZ", "uz-Latn-UZ" }; string separator = "; "; if (culturiVirgula.Contains <string>(CultureInfo.CurrentCulture.Name)) { separator = ", "; } for (int j = 0; j < dGV.Columns.Count; j++) { if (dGV.Columns[j].Visible && !dGV.SeIgnoraColoanaLaImprimare(dGV.Columns[j].Name)) { valoareCelula = CUtil.InlocuiesteDiacritice(dGV.Columns[j].HeaderText); sHeaders.Append(valoareCelula); sHeaders.Append(separator); listaIndecsiColoaneExportabile.Add(j); //pentru a exporta datele coloanelor pretabile pentru export } } myFile.WriteLine(sHeaders); sHeaders = null; // Export data. //Fie cele bifate, fie toate List <DataGridViewRow> listaLiniiSelectate = dGV.GetListaLiniiSelectate(); foreach (DataGridViewRow linie in listaLiniiSelectate) { if (linie.Tag == null) { continue; //in Tag-ul tuturor liniilor din aplicatie salvam obiectul corespunzator liniei respective; lipsa acestuia implica ignorarea liniei } StringBuilder stLine = new StringBuilder(); for (int j = 0; j < linie.Cells.Count; j++) { if (listaIndecsiColoaneExportabile.Contains(j)) { stLine.Append(CUtil.ConvertObjectToString(linie.Cells[j].EditedFormattedValue, true)); stLine.Append(separator); } } myFile.WriteLine(stLine); } //} //else //{ // // Export titles: // string sHeaders = ""; // for (int j = 0; j < dGV.Columns.Count; j++) { sHeaders = sHeaders.ToString() + dGV.Columns[j].HeaderText + ", "; } // myFile.WriteLine(sHeaders); // // Export data. // for (int i = 0; i < dGV.RowCount - 1; i++) // { // string stLine = ""; // for (int j = 0; j < dGV.Rows[i].Cells.Count; j++) { stLine = stLine.ToString() + dGV.Rows[i].Cells[j].Value + ", "; } // myFile.WriteLine(stLine); // } //} } IHMUtile.PornesteProces(filename); }
public static void Exporta(DataGridViewPersonalizat dGV, List <string> pListaColoaneImprimare, string pEtichetaTotal, string pFisier, bool pDeschideDupaCreare) { string fisierSalvare = pFisier; DataType tipCelula = DataType.String; if (string.IsNullOrEmpty(fisierSalvare)) { fisierSalvare = CCL.UI.IHMUtile.GetCaleSalvareFisier(CCL.UI.IHMUtile.GetFormParinte(dGV), ".xls"); } if (string.IsNullOrEmpty(fisierSalvare)) { return; } string idStilHeader = "idSHeader"; WorksheetStyle stilHeader = new WorksheetStyle(idStilHeader); stilHeader.Font.Bold = true; stilHeader.Interior.Pattern = StyleInteriorPattern.Solid; stilHeader.Interior.Color = "#c0c0c0"; string idStilTotal = "idSTotal"; WorksheetStyle stilTotal = new WorksheetStyle(idStilTotal); stilTotal.Font.Bold = true; stilTotal.Interior.Pattern = StyleInteriorPattern.Solid; stilTotal.Interior.Color = "#80ff80"; string idStilNormal = "idSNormal"; WorksheetStyle stilNormal = new WorksheetStyle(idStilNormal); Workbook book = new Workbook(); book.Styles.Add(stilHeader); book.Styles.Add(stilNormal); book.Styles.Add(stilTotal); Worksheet sheet = book.Worksheets.Add("iDava"); List <string> listaNumeColoaneExportabile = new List <string>(); Dictionary <string, DataType> dictColoaneTipuri = new Dictionary <string, DataType>(); string valoareCelula = string.Empty; WorksheetRow rowHeader = sheet.Table.Rows.Add(); foreach (string numeColoana in pListaColoaneImprimare) { if (dGV.Columns[numeColoana].Visible && !dGV.SeIgnoraColoanaLaImprimare(numeColoana)) { listaNumeColoaneExportabile.Add(numeColoana); //pentru a exporta datele coloanelor pretabile pentru export dictColoaneTipuri.Add(numeColoana, getDataType(dGV.Columns[numeColoana])); valoareCelula = CUtil.InlocuiesteDiacritice(dGV.Columns[numeColoana].HeaderText); rowHeader.Cells.Add(valoareCelula, dictColoaneTipuri[numeColoana], idStilHeader); } } List <DataGridViewRow> listaLiniiSelectate = dGV.GetListaLiniiSelectate(); bool existaValoareNumerica = false; double totalValoareNumerica = 0; foreach (DataGridViewRow linie in listaLiniiSelectate) { if (linie.Tag == null) { continue; //in Tag-ul tuturor liniilor din aplicatie salvam obiectul corespunzator liniei respective; lipsa acestuia implica ignorarea liniei } WorksheetRow rowValoare = sheet.Table.Rows.Add(); foreach (string numeColoane in pListaColoaneImprimare) { if (listaNumeColoaneExportabile.Contains(numeColoane)) { if (linie.Cells[numeColoane].Tag != null && linie.Cells[numeColoane].Tag is String) { valoareCelula = CUtil.ConvertObjectToString(linie.Cells[numeColoane].Tag, true); } else { valoareCelula = CUtil.ConvertObjectToString(linie.Cells[numeColoane].EditedFormattedValue, true); } if (dGV.esteColoanaNumericaExport(numeColoane)) { existaValoareNumerica = true; totalValoareNumerica += CUtil.GetAsDouble(valoareCelula); rowValoare.Cells.Add(valoareCelula, dictColoaneTipuri[numeColoane], idStilNormal); } else { rowValoare.Cells.Add(valoareCelula, dictColoaneTipuri[numeColoane], idStilNormal); } } } } if (existaValoareNumerica) { WorksheetRow rowValoare = sheet.Table.Rows.Add(); int indexCelula = 0; foreach (string numeColoane in pListaColoaneImprimare) { if (listaNumeColoaneExportabile.Contains(numeColoane)) { if (dGV.esteColoanaNumericaExport(numeColoane)) { rowValoare.Cells.Add(Convert.ToString(Math.Round(totalValoareNumerica, 2)), dictColoaneTipuri[numeColoane], idStilTotal); } else { if (indexCelula == 0) { rowValoare.Cells.Add(pEtichetaTotal, tipCelula, idStilTotal); } else { rowValoare.Cells.Add(string.Empty, tipCelula, idStilTotal); } indexCelula += 1; } } } } if (!string.IsNullOrEmpty(fisierSalvare)) { book.Save(fisierSalvare); if (pDeschideDupaCreare) { Process.Start(fisierSalvare); } } }