public static void SetColumnWidth(this GridBandColumnCollection gridColumns, int columnWidth) { if (gridColumns.View == null || gridColumns.Band == null) { return; } var maxRowCount = GetMaxRowCount(gridColumns); if (maxRowCount == 0) { return; } var newBandWidth = gridColumns.Count / maxRowCount * columnWidth; gridColumns.Band.Width = newBandWidth; foreach (BandedGridColumn bandedGridColumn in gridColumns) { // Bei der letzten Spalte die Breite nicht setzen! if (bandedGridColumn == gridColumns[gridColumns.Count / maxRowCount - 1] || bandedGridColumn == gridColumns[gridColumns.Count - 1]) { continue; } bandedGridColumn.Width = columnWidth; } }
private static int GetMaxRowCount(GridBandColumnCollection gridColumns) { var maxRowCount = 1; foreach (BandedGridColumn bandedGridColumn in gridColumns) { if (bandedGridColumn.RowIndex + 1 > maxRowCount) { maxRowCount = bandedGridColumn.RowIndex + 1; } } return(maxRowCount); }
/// <summary> /// Tạo cột cho GridBand /// </summary> /// <param name="gcc"></param> /// <param name="field"></param> /// <param name="caption"></param> /// <param name="header"></param> /// <param name="cell"></param> public static void CreateColumn(this GridBandColumnCollection gcc, string field, string caption, Color header, Color cell, int width = 40) { var gc = new BandedGridColumn { Caption = caption, FieldName = field, Visible = true, Width = width }; gc.AppearanceCell.Options.UseTextOptions = gc.OptionsColumn.FixedWidth = true; gcc.Add(gc); gc.AppearanceHeader.TextOptions.HAlignment = gc.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center; gc.OptionsFilter.AllowFilter = false; gc.OptionsColumn.AllowSort = DefaultBoolean.False; gc.AppearanceHeader.Font = new Font("Tahoma", 8.25F, FontStyle.Bold); if (header != Color.Empty) { gc.AppearanceHeader.BackColor = header; } if (cell != Color.Empty) { gc.AppearanceCell.BackColor = cell; } }
//public void ExportByInfra(int startCol, int startRow, string fileName) //{ // Workbook workbook = new Workbook(WorkbookFormat.Excel2007); // workbook.CellReferenceMode = CellReferenceMode.A1; // Worksheet worksheet = workbook.Worksheets.Add(GridViewForExcel.Name); // int x = startCol, originX, bandRowCount, headerRowCount; // int y = 0; // int rowCount = GridViewForExcel.DataRowCount; // x = startCol; // bandRowCount = 0; // headerRowCount = GridViewForExcel.OptionsView.ShowColumnHeaders == true ? bandRowCount + 1 : bandRowCount; // GridColumnCollection colCollection = GridViewForExcel.Columns; // foreach (GridColumn col in colCollection) // { // if (col.Visible) // { // worksheet.Rows[startRow].Cells[x].Value = col.Caption; // for (y = 0; y < rowCount; y++) // { // if (ExportCellsAsDisplayText == true && (!(GridViewForExcel.GetRowCellValue(y, col) is bool) || (col.ColumnEdit != null && col.ColumnEdit.GetType().Name == "RepositoryItemComboBox")))//if type of col's value is bool and editor is't cbo, then export cell value.[Add by ZhangXiChun 2010-12-03] // { // if ((GridViewForExcel.GetRowCellValue(y, col)) is decimal || (GridViewForExcel.GetRowCellValue(y, col) is int)) // { // if (GridViewForExcel.GetRowCellDisplayText(y, col).Contains('%')) // { // worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellDisplayText(y, col).ToString(); // } // else // { // worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellValue(y, col); // } // } // else // { // worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellDisplayText(y, col).ToString(); // } // } // else // { // worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellValue(y, col).ToString(); // } // } // //if (GridViewForExcel.OptionsView.ShowFooter == true) // //{ // // if (col.SummaryItem.SummaryType != DevExpress.Data.SummaryItemType.None) // // { // // // modi zhang.amei 2012-7-5 13:11:14 数字类型的不需要处理格式 // // // XlsProvider.SetCellString(x, y + startRow + headerRowCount, col.SummaryText); // // if (col.SummaryItem.SummaryValue is int || col.SummaryItem.SummaryValue is decimal) // // XlsProvider.SetCellData(x, y + startRow + headerRowCount, col.SummaryItem.SummaryValue); // // else XlsProvider.SetCellString(x, y + startRow + headerRowCount, col.SummaryText); // // } // // XlsProvider.SetCellStyle(x, y + startRow + headerRowCount, GetColumnHeaderStyle()); // //} // x++; // } // } // /* modified by sun.zeyu 20130111 end */ // workbook.Save(fileName); //} public void Export(int startCol, int startRow, string fileName) { Workbook workbook = new Workbook(WorkbookFormat.Excel2007); workbook.CellReferenceMode = CellReferenceMode.A1; Worksheet worksheet = workbook.Worksheets.Add(GridViewForExcel.Name); int x = startCol, originX, bandRowCount, headerRowCount; int y = 0; int rowCount = GridViewForExcel.DataRowCount; if (GridViewForExcel is BandedGridView) { x = startCol; GridBandCollection bandCollection = ((BandedGridView)GridViewForExcel).Bands; bandRowCount = GetBandRowCount(bandCollection); //get MaxBandRowCount.[Add By ZhangXiChun 2010-08-09] headerRowCount = GridViewForExcel.OptionsView.ShowColumnHeaders == true ? bandRowCount + 1 : bandRowCount; //get HeaderRowCount.[Add By ZhangXiChun 2010-09-08] if (bandRowCount == 1) //if isn't multi-row band.[Add By ZhangXiChun 2010-08-09] { if (((BandedGridView)GridViewForExcel).OptionsView.ShowBands == false) { bandRowCount = 0; headerRowCount = headerRowCount - 1; } foreach (GridBand band in bandCollection) { if (band.ReallyVisible) { originX = x; if (band.Visible && ((BandedGridView)GridViewForExcel).OptionsView.ShowBands) { worksheet.Rows[startRow].Cells[x].Value = band.Caption; } GridBandColumnCollection colCollection = band.Columns; foreach (GridColumn col in colCollection) { if (col.Visible) { if (GridViewForExcel.OptionsView.ShowColumnHeaders == true) //if ShowColumnHeaders.[Add By ZhangXiChun 2010-09-08] { worksheet.Rows[startRow + bandRowCount].Cells[x].Value = col.Caption ?? string.Empty; } for (y = 0; y < rowCount; y++) { if (ExportCellsAsDisplayText == true && (!(GridViewForExcel.GetRowCellValue(y, col) is bool) || (col.ColumnEdit != null && col.ColumnEdit.GetType().Name == "RepositoryItemComboBox")))//if type of col's value is bool and editor is't cbo, then export cell value.[Add by ZhangXiChun 2010-12-03] { if ((GridViewForExcel.GetRowCellValue(y, col)) is decimal || (GridViewForExcel.GetRowCellValue(y, col) is int)) { if (GridViewForExcel.GetRowCellDisplayText(y, col).Contains('%')) { worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellDisplayText(y, col).ToString(); } else { worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellValue(y, col); } } worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellDisplayText(y, col).ToString(); } else { worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = GridViewForExcel.GetRowCellValue(y, col).ToString(); } } if (GridViewForExcel.OptionsView.ShowFooter == true) { if (col.SummaryItem.SummaryType != DevExpress.Data.SummaryItemType.None) { if (col.SummaryItem.SummaryValue is int || col.SummaryItem.SummaryValue is decimal) { if (col.SummaryText.Contains('%')) { worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = col.SummaryText; } else { worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = col.SummaryItem.SummaryValue; } } else { worksheet.Rows[y + startRow + headerRowCount].Cells[x].Value = col.SummaryText; } } } x++; } } if (((BandedGridView)GridViewForExcel).OptionsView.ShowBands) { //XlsProvider.SetCellStyleAndUnion(originX, startRow, x - originX, bandRowCount, GetBandHeaderStyle()); worksheet.MergedCellsRegions.Add(startRow, originX, startRow + bandRowCount, x); } } } } else //if is multi-row band.[Add By ZhangXiChun 2010-08-09] { int floor = 0; ExportForBand(x, y, startRow++, ((BandedGridView)GridViewForExcel).Bands, bandRowCount, floor, worksheet); } /* modified by sun.zeyu 20130111 end */ workbook.Save(fileName); } else if (GridViewForExcel is GridView) { x = startCol; GridColumnCollection colCollection = GridViewForExcel.Columns; foreach (GridColumn col in colCollection) { if (col.Visible) { worksheet.Rows[startRow].Cells[x].Value = col.Caption; for (y = 0; y < rowCount; y++) { if (ExportCellsAsDisplayText == true && (!(GridViewForExcel.GetRowCellValue(y, col) is bool) || (col.ColumnEdit != null && col.ColumnEdit.GetType().Name == "RepositoryItemComboBox"))) { if ((GridViewForExcel.GetRowCellValue(y, col)) is decimal || (GridViewForExcel.GetRowCellValue(y, col) is int)) { if (GridViewForExcel.GetRowCellDisplayText(y, col).Contains('%')) { worksheet.Rows[y + startRow + 1].Cells[x].Value = GridViewForExcel.GetRowCellDisplayText(y, col).ToString(); } else { worksheet.Rows[y + startRow + 1].Cells[x].Value = GridViewForExcel.GetRowCellValue(y, col); } } else { worksheet.Rows[y + startRow + 1].Cells[x].Value = GridViewForExcel.GetRowCellDisplayText(y, col).ToString(); } } else { worksheet.Rows[y + startRow + 1].Cells[x].Value = GridViewForExcel.GetRowCellValue(y, col).ToString(); } } if (GridViewForExcel.OptionsView.ShowFooter == true) { if (col.SummaryItem.SummaryType != DevExpress.Data.SummaryItemType.None) { if (col.SummaryItem.SummaryValue is int || col.SummaryItem.SummaryValue is decimal) { if (col.SummaryText.Contains('%')) { worksheet.Rows[y + startRow + 1].Cells[x].Value = col.SummaryText; } else { worksheet.Rows[y + startRow + 1].Cells[x].Value = col.SummaryItem.SummaryValue; } } else { worksheet.Rows[y + startRow + 1].Cells[x].Value = col.SummaryText; } } } x++; } } /* modified by sun.zeyu 20130111 end */ workbook.Save(fileName); } }
public virtual void Export(int startCol, int startRow) { int x = startCol, originX; int y; int rowCount = _gridView.DataRowCount; if (_gridView is BandedGridView) { x = startCol; GridBandCollection bandCollection = ((BandedGridView)_gridView).Bands; foreach (GridBand band in bandCollection) { if (band.ReallyVisible) { originX = x; _xlsProvider.SetCellString(x, startRow, band.Caption); GridBandColumnCollection colCollection = band.Columns; foreach (GridColumn col in colCollection) { if (col.Visible) { _xlsProvider.SetCellString(x, startRow + band.RowCount, col.Caption ?? string.Empty); //_xlsProvider.SetCellStyle(x, startRow + band.RowCount, ConvertAppearanceToCellStyle(col.AppearanceHeader)); _xlsProvider.SetCellStyle(x, startRow + band.RowCount, GetColumnHeaderStyle()); for (y = 0; y < rowCount; y++) { if (_exportCellsAsDisplayText == true) { _xlsProvider.SetCellString(x, y + startRow + band.RowCount + 1, _gridView.GetRowCellDisplayText(y, col).ToString()); } else { _xlsProvider.SetCellString(x, y + startRow + band.RowCount + 1, _gridView.GetRowCellValue(y, col).ToString()); } _xlsProvider.SetCellStyle(x, y + startRow + band.RowCount + 1, ConvertAppearanceToCellStyle(col.AppearanceCell)); } if (_gridView.OptionsView.ShowFooter == true) { if (col.SummaryItem.SummaryType != DevExpress.Data.SummaryItemType.None) { _xlsProvider.SetCellString(x, y + startRow + band.RowCount + 1, col.SummaryText); } //_xlsProvider.SetCellStyle(x, y + startRow + band.RowCount + 1, ConvertAppearanceToCellStyle(col.AppearanceHeader)); _xlsProvider.SetCellStyle(x, y + startRow + band.RowCount + 1, GetColumnHeaderStyle()); } x++; } } //_xlsProvider.SetCellStyleAndUnion(originX, startRow, x - originX, band.RowCount, ConvertAppearanceToCellStyle(band.AppearanceHeader)); _xlsProvider.SetCellStyleAndUnion(originX, startRow, x - originX, band.RowCount, GetBandHeaderStyle()); } } } else if (_gridView is GridView) { x = startCol; GridColumnCollection colCollection = _gridView.Columns; foreach (GridColumn col in colCollection) { if (col.Visible) { _xlsProvider.SetCellString(x, startRow, col.Caption); //_xlsProvider.SetCellStyle(x, startRow, ConvertAppearanceToCellStyle(col.AppearanceHeader)); _xlsProvider.SetCellStyle(x, startRow, GetColumnHeaderStyle()); for (y = 0; y < rowCount; y++) { if (_exportCellsAsDisplayText == true) { _xlsProvider.SetCellString(x, y + startRow + 1, _gridView.GetRowCellDisplayText(y, col).ToString()); } else { _xlsProvider.SetCellString(x, y + startRow + 1, _gridView.GetRowCellValue(y, col).ToString()); } _xlsProvider.SetCellStyle(x, y + startRow + 1, ConvertAppearanceToCellStyle(col.AppearanceCell)); } if (_gridView.OptionsView.ShowFooter == true) { if (col.SummaryItem.SummaryType != DevExpress.Data.SummaryItemType.None) { _xlsProvider.SetCellString(x, y + startRow + 1, col.SummaryText); } //_xlsProvider.SetCellStyle(x, y + startRow + 1, ConvertAppearanceToCellStyle(col.AppearanceHeader)); _xlsProvider.SetCellStyle(x, y + startRow + 1, GetColumnHeaderStyle()); } x++; } } } }