void ExportCell(IXlRow row, int gridRowHandle, GridColumn gridColumn) { using (IXlCell cell = row.CreateCell()) { // Set cell value cell.Value = XlVariantValue.FromObject(this.view.GetRowCellValue(gridRowHandle, gridColumn)); // Get cell appearance AppearanceObject appearance = GetCellAppearance(gridRowHandle, gridColumn); // Apply alignment XlCellAlignment alignment = new XlCellAlignment() { WrapText = appearance.TextOptions.WordWrap.HasFlag(WordWrap.Wrap), VerticalAlignment = ConvertAlignment(appearance.TextOptions.VAlignment), HorizontalAlignment = ConvertAlignment(appearance.TextOptions.HAlignment) }; cell.ApplyFormatting(alignment); // Apply borders Color borderColor = appearance.GetBorderColor(); if (!DXColor.IsTransparentOrEmpty(borderColor)) { cell.ApplyFormatting(XlBorder.OutlineBorders(borderColor)); } // Apply fill if (appearance.Options.UseBackColor) { cell.ApplyFormatting(XlFill.SolidFill(appearance.BackColor)); } // Apply font Font appearanceFont = appearance.Font; XlFont font = XlFont.CustomFont(appearanceFont.Name); font.Size = appearanceFont.SizeInPoints; font.Bold = appearanceFont.Bold; font.Italic = appearanceFont.Italic; font.StrikeThrough = appearanceFont.Strikeout; font.Underline = appearanceFont.Underline ? XlUnderlineType.Single : XlUnderlineType.None; if (appearance.Options.UseForeColor) { font.Color = appearance.ForeColor; } cell.ApplyFormatting(font); } }
static void Borders(Stream stream, XlDocumentFormat documentFormat) { #region #Borders // Specify a two-dimensional array that stores possible line styles for a border. XlBorderLineStyle[,] lineStyles = new XlBorderLineStyle[, ] { { XlBorderLineStyle.Thin, XlBorderLineStyle.Medium, XlBorderLineStyle.Thick, XlBorderLineStyle.Double }, { XlBorderLineStyle.Dotted, XlBorderLineStyle.Dashed, XlBorderLineStyle.DashDot, XlBorderLineStyle.DashDotDot }, { XlBorderLineStyle.SlantDashDot, XlBorderLineStyle.MediumDashed, XlBorderLineStyle.MediumDashDot, XlBorderLineStyle.MediumDashDotDot } }; // Create an exporter instance. IXlExporter exporter = XlExport.CreateExporter(documentFormat); // Create a new document. using (IXlDocument document = exporter.CreateDocument(stream)) { document.Options.Culture = CultureInfo.CurrentCulture; // Create a worksheet. using (IXlSheet sheet = document.CreateSheet()) { for (int i = 0; i < 3; i++) { sheet.SkipRows(1); // Create a worksheet row. using (IXlRow row = sheet.CreateRow()) { for (int j = 0; j < 4; j++) { row.SkipCells(1); // Create a new cell in the row. using (IXlCell cell = row.CreateCell()) { // Set outside borders for the created cell using a particular line style from the lineStyles array. cell.ApplyFormatting(XlBorder.OutlineBorders(Color.SeaGreen, lineStyles[i, j])); } } } } } } #endregion #Borders }
void op_CustomizeCell(CustomizeCellEventArgs e) { long _mark = long.Parse((GVG.GetRowCellValue(e.RowHandle, "ID") ?? 0).ToString()); XlCellFormatting formatting = new XlCellFormatting(); formatting.Font = new XlFont(); formatting.Font.Bold = true; formatting.Font.Name = "Times New Roman"; if (_mark == 0) { formatting.Border = XlBorder.OutlineBorders(Color.FromArgb(216, 228, 188)); if (e.ColumnFieldName == "CongTy") { formatting.Border.RightColor = Color.Black; } formatting.Fill = XlFill.SolidFill(Color.FromArgb(216, 228, 188)); e.Formatting.FormatType = FormatType.None; e.Value = string.Empty; } else { XlCellAlignment alignment = new XlCellAlignment(); alignment.HorizontalAlignment = XlHorizontalAlignment.Center; alignment.VerticalAlignment = XlVerticalAlignment.Center; if (e.AreaType == SheetAreaType.Header) { e.Formatting.Alignment = alignment; formatting.Fill = XlFill.SolidFill(Color.DarkSeaGreen); formatting.Font.Size = 15; } else if (_mark == -1) { formatting.Border = XlBorder.OutlineBorders(Color.DarkSeaGreen); if (!"MaCho HanhTrinhDi MaHD SoVe".Contains(e.ColumnFieldName)) { formatting.Border.RightColor = Color.Black; } formatting.Border.BottomColor = Color.Black; formatting.Fill = XlFill.SolidFill(Color.DarkSeaGreen); formatting.Font.Size = 12; if (e.ColumnFieldName == "HanhTrinhVe") { e.Value = "Tổng"; } if ((e.Value ?? string.Empty).ToString() == "0") { e.Value = string.Empty; } } else { formatting.Fill = XlFill.SolidFill(Color.White); if (e.Value is string) { e.Formatting.Alignment = alignment; } } } e.Formatting.CopyFrom(formatting, FormatType.None); e.Handled = true; }