J’ai essayé de trouver une réponse à cette question et je n’ai eu aucun succès. Mon problème est le suivant. J’ai un comboBox qui répertorie différents formats pour exporter un rapport. Une de ces options est HTML. Fondamentalement, ce que je veux faire est de prendre le dataGridView dans un formulaire Windows et exporter tel quel vers une page HTML Je voudrais simplement exporter vers un tableau HTML. Je ne sais même pas comment commencer, alors je n’ai pas d’exemple de code à fournir. J’utilise c sharp 2008 windows app. Tout conseil serait grandement apprécié.
Voici une méthode que j’ai codée et qui inclut le formatage de DataGridView:
public ssortingng ConvertDataGridViewToHTMLWithFormatting(DataGridView dgv) { SsortingngBuilder sb = new SsortingngBuilder(); //create html & table sb.AppendLine(""); sb.AppendLine(""); //create table header for (int i = 0; i < dgv.Columns.Count; i++) { sb.Append(DGVHeaderCellToHTMLWithFormatting(dgv, i)); sb.Append(DGVCellFontAndValueToHTML(dgv.Columns[i].HeaderText, dgv.Columns[i].HeaderCell.Style.Font)); sb.AppendLine(""); } sb.AppendLine(" "); //create table body for (int rowIndex = 0; rowIndex < dgv.Rows.Count; rowIndex++) { sb.AppendLine(""); foreach (DataGridViewCell dgvc in dgv.Rows[rowIndex].Cells) { sb.AppendLine(DGVCellToHTMLWithFormatting(dgv, rowIndex, dgvc.ColumnIndex)); ssortingng cellValue = dgvc.Value == null ? ssortingng.Empty : dgvc.Value.ToSsortingng(); sb.AppendLine(DGVCellFontAndValueToHTML(cellValue, dgvc.Style.Font)); sb.AppendLine(""); } sb.AppendLine(" "); } //table footer & end of html file sb.AppendLine("
"); return sb.ToSsortingng(); } //TODO: Add more cell styles described here: https://msdn.microsoft.com/en-us/library/1yef90x0(v=vs.110).aspx public ssortingng DGVHeaderCellToHTMLWithFormatting(DataGridView dgv, int col) { SsortingngBuilder sb = new SsortingngBuilder(); sb.Append(""); return sb.ToSsortingng(); } public ssortingng DGVCellToHTMLWithFormatting(DataGridView dgv, int row, int col) { SsortingngBuilder sb = new SsortingngBuilder(); sb.Append(" "); return sb.ToSsortingng(); } public ssortingng DGVCellColorToHTML(Color foreColor, Color backColor) { if (foreColor.Name == "0" && backColor.Name == "0") return ssortingng.Empty; SsortingngBuilder sb = new SsortingngBuilder(); sb.Append(" style=\""); if (foreColor.Name != "0" && backColor.Name != "0") { sb.Append("color:#"); sb.Append(foreColor.R.ToSsortingng("X2") + foreColor.G.ToSsortingng("X2") + foreColor.B.ToSsortingng("X2")); sb.Append("; background-color:#"); sb.Append(backColor.R.ToSsortingng("X2") + backColor.G.ToSsortingng("X2") + backColor.B.ToSsortingng("X2")); } else if (foreColor.Name != "0" && backColor.Name == "0") { sb.Append("color:#"); sb.Append(foreColor.R.ToSsortingng("X2") + foreColor.G.ToSsortingng("X2") + foreColor.B.ToSsortingng("X2")); } else //if (foreColor.Name == "0" && backColor.Name != "0") { sb.Append("background-color:#"); sb.Append(backColor.R.ToSsortingng("X2") + backColor.G.ToSsortingng("X2") + backColor.B.ToSsortingng("X2")); } sb.Append(";\""); return sb.ToSsortingng(); } public ssortingng DGVCellFontAndValueToHTML(ssortingng value,Font font) { //If no font has been set then assume its the default as someone would be expected in HTML or Excel if (font == null || font == this.Font && !(font.Bold | font.Italic | font.Underline | font.Ssortingkeout)) return value; SsortingngBuilder sb = new SsortingngBuilder(); sb.Append(" "); if (font.Bold) sb.Append(""); if (font.Italic) sb.Append(""); if (font.Ssortingkeout) sb.Append(""); //The element was deprecated in HTML 4.01. The new HTML 5 tag is: text-decoration: underline if (font.Underline) sb.Append(""); ssortingng size = ssortingng.Empty; if (font.Size != this.Font.Size) size = "font-size: " + font.Size + "pt;"; //The tag is not supported in HTML5. Use CSS or a span instead. if (font.FontFamily.Name != this.Font.Name) { sb.Append(""); } sb.Append(value); if (font.FontFamily.Name != this.Font.Name) sb.Append(""); if (font.Underline) sb.Append(""); if (font.Ssortingkeout) sb.Append(""); if (font.Italic) sb.Append(""); if (font.Bold) sb.Append(""); return sb.ToSsortingng(); } public ssortingng DGVCellAlignmentToHTML(DataGridViewContentAlignment align) { if (align == DataGridViewContentAlignment.NotSet) return ssortingng.Empty; ssortingng horizontalAlignment = ssortingng.Empty; ssortingng verticalAlignment = ssortingng.Empty; CellAlignment(align, ref horizontalAlignment, ref verticalAlignment); SsortingngBuilder sb = new SsortingngBuilder(); sb.Append(" align='"); sb.Append(horizontalAlignment); sb.Append("' valign='"); sb.Append(verticalAlignment); sb.Append("'"); return sb.ToSsortingng(); } private void CellAlignment(DataGridViewContentAlignment align, ref ssortingng horizontalAlignment, ref ssortingng verticalAlignment) { switch (align) { case DataGridViewContentAlignment.MiddleRight: horizontalAlignment = "right"; verticalAlignment = "middle"; break; case DataGridViewContentAlignment.MiddleLeft: horizontalAlignment = "left"; verticalAlignment = "middle"; break; case DataGridViewContentAlignment.MiddleCenter: horizontalAlignment = "centre"; verticalAlignment = "middle"; break; case DataGridViewContentAlignment.TopCenter: horizontalAlignment = "centre"; verticalAlignment = "top"; break; case DataGridViewContentAlignment.BottomCenter: horizontalAlignment = "centre"; verticalAlignment = "bottom"; break; case DataGridViewContentAlignment.TopLeft: horizontalAlignment = "left"; verticalAlignment = "top"; break; case DataGridViewContentAlignment.BottomLeft: horizontalAlignment = "left"; verticalAlignment = "bottom"; break; case DataGridViewContentAlignment.TopRight: horizontalAlignment = "right"; verticalAlignment = "top"; break; case DataGridViewContentAlignment.BottomRight: horizontalAlignment = "right"; verticalAlignment = "bottom"; break; default: //DataGridViewContentAlignment.NotSet horizontalAlignment = "left"; verticalAlignment = "middle"; break; } }
- pas en mesure de construire dans clearcase snapshot csproject
- Vérifier le fichier de modèle de mot
- tandis que sur IDataReader.Read ne fonctionne pas avec le rendement, mais foreach sur le lecteur ne
- Le nombre maximal de fichiers pouvant être ouverts en C # est-il limité?
- Incorporer un manifeste COM sans enregistrement dans une dll C # avec un environnement natif / géré
- Rechercher par programme une boîte de message et générer un clic sur un bouton
- SDK Foursquare pour .net
- Capture d’écran C # en bitmap 8 bits (256 couleurs)
- Énumération des groupes d’utilisateurs AD nesteds à l’aide de C #
- Exécution d’un processus (WITH GUI) sur l’écran d’ouverture de session Windows XP (.NET / Pinvoke)