■ GridControl 클래스에서 데이터 식별 엑셀을 내보내는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
using System; using System.Collections.Generic; using System.Data.OleDb; using System.Diagnostics; using System.Drawing; using System.IO; using System.Reflection; using System.Windows.Forms; using DevExpress.Export; using DevExpress.Export.Xl; using DevExpress.Printing.ExportHelpers; using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraPrinting; using DevExpress.XtraGrid.Views.Base; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 셀 추가 대리자 - AddCellDelegate(e, formattingObject1, formattingObject2) /// <summary> /// 셀 추가 대리자 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="formattingObject1">포맷 객체 1</param> /// <param name="formattingObject2">포맷 객체 2</param> private delegate void AddCellDelegate(ContextEventArgs e, XlFormattingObject formattingObject1, XlFormattingObject formattingObject2); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// Discontinued 셀 정렬 /// </summary> private XlCellAlignment discontinuedCellAlignment = new XlCellAlignment() { HorizontalAlignment = XlHorizontalAlignment.Center, VerticalAlignment = XlVerticalAlignment.Center }; /// <summary> /// 셀 추가 대리자 딕셔너리 /// </summary> private Dictionary<int, AddCellDelegate> addCellDelegateDictionary; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); string filePath = FilesHelper.FindingFileName(Application.StartupPath, "nwind.mdb"); string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath; OleDbDataAdapter adapter1 = new OleDbDataAdapter("SELECT * FROM Products" , connectionString); OleDbDataAdapter adapter2 = new OleDbDataAdapter("SELECT * FROM Categories", connectionString); this.gridView.OptionsView.ColumnAutoWidth = false; adapter1.Fill(nwindDataSet.Products ); adapter2.Fill(nwindDataSet.Categories); this.addCellDelegateDictionary = GetAddCellDelegateDictionary(); this.gridView.ExpandAllGroups(); Load += Form_Load; this.exportButton.Click += exportButton_Click; this.gridView.CustomUnboundColumnData += gridView_CustomUnboundColumnData; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.gridView.BestFitColumns(true); } #endregion #region 내보내기 옵션 시트 설정 커스텀 설정하기 - exportOptionsEx_CustomizeSheetSettings(e) /// <summary> /// 내보내기 옵션 시트 설정 커스텀 설정하기 /// </summary> /// <param name="e">이벤트 인자</param> private void exportOptionsEx_CustomizeSheetSettings(CustomizeSheetEventArgs e) { const int lastHeaderRowIndex = 15; e.ExportContext.SetFixedHeader(lastHeaderRowIndex); e.ExportContext.AddAutoFilter ( new XlCellRange ( new XlCellPosition(0, lastHeaderRowIndex), new XlCellPosition(5, 100) ) ); } #endregion #region 내보내기 옵션 시트 헤더 커스텀 설정하기 - exportOptionsEx_CustomizeSheetHeader(e) /// <summary> /// 내보내기 옵션 시트 헤더 커스텀 설정하기 /// </summary> /// <param name="e">이벤트 인자</param> private void exportOptionsEx_CustomizeSheetHeader(ContextEventArgs e) { XlFormattingObject formattingObject1 = GetFormattingObject(true, 24); XlFormattingObject formattingObject2 = GetFormattingObject(true, 18); for(int i = 0; i < 15; i++) { AddCellDelegate addCellDelegate; if(addCellDelegateDictionary.TryGetValue(i, out addCellDelegate)) { addCellDelegate(e, formattingObject1, formattingObject2); } else { e.ExportContext.AddRow(); } } MergeCells(e); Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("TestProject.RESOURCE.1.jpg"); if(stream != null) { Bitmap bitmap = new Bitmap(Image.FromStream(stream)); XlCellRange cellRange = new XlCellRange ( new XlCellPosition(0, 0), new XlCellPosition(5, 7) ); e.ExportContext.MergeCells(cellRange); e.ExportContext.InsertImage(bitmap, cellRange); } e.ExportContext.MergeCells ( new XlCellRange ( new XlCellPosition(0, 8), new XlCellPosition(5, 8) ) ); } #endregion #region 내보내기 옵션 셀 커스텀 설정하기 - exportOptionsEx_CustomizeCell(e) /// <summary> /// 내보내기 옵션 셀 커스텀 설정하기 /// </summary> /// <param name="e">이벤트 인자</param> private void exportOptionsEx_CustomizeCell(CustomizeCellEventArgs e) { if(e.ColumnFieldName == "Discontinued") { if(e.Value is bool) { e.Handled = true; e.Formatting.Alignment = this.discontinuedCellAlignment; e.Value = ((bool)e.Value) ? "☑" : "☐"; } } } #endregion #region 내보내기 옵션 시트 FOOTER 커스텀 설정하기 - exportOptionsEx_CustomizeSheetFooter(e) /// <summary> /// 내보내기 옵션 시트 FOOTER 커스텀 설정하기 /// </summary> /// <param name="e">이벤트 인자</param> private void exportOptionsEx_CustomizeSheetFooter(ContextEventArgs e) { e.ExportContext.AddRow(); XlFormattingObject rowFormattingObject = GetFormattingObject(true, 18); rowFormattingObject.Alignment.HorizontalAlignment = XlHorizontalAlignment.Left; CellObject firstCellObject = new CellObject(); firstCellObject.Value = "The report is generated from the NorthWind database."; firstCellObject.Formatting = rowFormattingObject; e.ExportContext.AddRow(new[]{ firstCellObject }); CellObject secondCellObject = new CellObject(); secondCellObject.Value = "The addresses and phone numbers are fictitious."; rowFormattingObject.Font.Size = 14; rowFormattingObject.Font.Bold = false; rowFormattingObject.Font.Italic = true; secondCellObject.Formatting = rowFormattingObject; e.ExportContext.AddRow(new[]{ secondCellObject }); } #endregion #region 내보내기 옵션 행 추가 후 처리하기 - exportOptionsEx_AfterAddRow(e) /// <summary> /// 내보내기 옵션 행 추가 후 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> private void exportOptionsEx_AfterAddRow(AfterAddRowEventArgs e) { if(e.DataSourceRowIndex < 0) { e.ExportContext.MergeCells ( new XlCellRange ( new XlCellPosition(0, e.DocumentRow - 1), new XlCellPosition(5, e.DocumentRow - 1) ) ); } } #endregion #region 엑셀 XLSX 포맷 내보내기 버튼 클릭시 처리하기 - exportButton_Click(sender, e) /// <summary> /// 엑셀 XLSX 포맷 내보내기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void exportButton_Click(object sender, EventArgs e) { ExportSettings.DefaultExportType = ExportType.DataAware; XlsxExportOptionsEx exportOptionsEx = new XlsxExportOptionsEx(); exportOptionsEx.SheetName = "DevAV price"; exportOptionsEx.CustomizeSheetSettings += exportOptionsEx_CustomizeSheetSettings; exportOptionsEx.CustomizeSheetHeader += exportOptionsEx_CustomizeSheetHeader; exportOptionsEx.CustomizeCell += exportOptionsEx_CustomizeCell; exportOptionsEx.CustomizeSheetFooter += exportOptionsEx_CustomizeSheetFooter; exportOptionsEx.AfterAddRow += exportOptionsEx_AfterAddRow; this.gridControl.ExportToXlsx("grid-export.xlsx", exportOptionsEx); Process.Start("grid-export.xlsx"); } #endregion #region 그리드 뷰 언바운드 컬럼 데이터 커스텀 처리하기 - gridView_CustomUnboundColumnData(sender, e) /// <summary> /// 그리드 뷰 언바운드 컬럼 데이터 커스텀 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridView_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e) { if(e.Column == this.categoryNameColumn) { if(e.IsGetData) { e.Value = nwindDataSet.Categories.FindByCategoryID ( (int)(gridView.GetRowCellValue(gridView.GetRowHandle(e.ListSourceRowIndex), categoryIDColumn)) ).CategoryName; } } } #endregion //////////////////////////////////////////////////////////////////////////////// Event #region 셀 객체 구하기 - GetCellObject(value, formattingObject) /// <summary> /// 셀 객체 구하기 /// </summary> /// <param name="value">값</param> /// <param name="formattingObject">포맷 객체</param> /// <returns>셀 객체</returns> private CellObject GetCellObject(object value, XlFormattingObject formattingObject) { return new CellObject{ Value = value, Formatting = formattingObject }; } #endregion #region 주소 행 추가하기 1 - AddAddressRow1(e, formattingObjec1, formattingObjec2) /// <summary> /// 주소 행 추가하기 1 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="formattingObjec1">포맷 객체 1</param> /// <param name="formattingObjec2">포맷 객체 2</param> private void AddAddressRow1(ContextEventArgs e, XlFormattingObject formattingObjec1, XlFormattingObject formattingObjec2) { CellObject labelCellObject = GetCellObject("Address : " , formattingObjec1); CellObject valueCellObject = GetCellObject("807 West Paseo Del Mar", formattingObjec2); e.ExportContext.AddRow(new[]{ labelCellObject, null, valueCellObject }); } #endregion #region 주소 행 추가하기 2 - AddAddressRow2(e, formattingObject1, formattingObject2) /// <summary> /// 주소 행 추가하기 2 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="formattingObject1">포맷 객체 1</param> /// <param name="formattingObject2">포맷 객체 2</param> private void AddAddressRow2(ContextEventArgs e, XlFormattingObject formattingObject1, XlFormattingObject formattingObject2) { CellObject valueCellObject = GetCellObject("Los Angeles CA 90731 USA", formattingObject2); e.ExportContext.AddRow(new[]{ null, null, valueCellObject }); } #endregion #region 전화 행 추가하기 - AddPhoneRow(e, formattingObject1, formattingObject2) /// <summary> /// 전화 행 추가하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="formattingObject1">포맷 객체 1</param> /// <param name="formattingObject2">포맷 객체 2</param> private void AddPhoneRow(ContextEventArgs e, XlFormattingObject formattingObject1, XlFormattingObject formattingObject2) { CellObject labelCellObject = GetCellObject("Phone :" , formattingObject1); CellObject valueCellObject = GetCellObject("+ 1 (213) 555-2828", formattingObject2); e.ExportContext.AddRow(new[]{ labelCellObject, null, valueCellObject }); } #endregion #region 팩스 행 추가하기 - AddFAXRow(e, formattingObject1, formattingObject2) /// <summary> /// 팩스 행 추가하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="formattingObject1">포맷 객체 1</param> /// <param name="formattingObject2">포맷 객체 2</param> private void AddFAXRow(ContextEventArgs e, XlFormattingObject formattingObject1, XlFormattingObject formattingObject2) { CellObject labelCellObject = GetCellObject("Fax :" , formattingObject1); CellObject valueCellObject = GetCellObject("+ 1 (213) 555-1824", formattingObject2); e.ExportContext.AddRow(new[]{ labelCellObject, null, valueCellObject }); } #endregion #region 이메일 행 추가하기 - AddEMailRow(e, formattingObject1, formattingObject2) /// <summary> /// 이메일 행 추가하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="formattingObject1">포맷 객체 1</param> /// <param name="formattingObject2">포맷 객체 2</param> private void AddEMailRow(ContextEventArgs e, XlFormattingObject formattingObject1, XlFormattingObject formattingObject2) { CellObject labelCellObject = GetCellObject("Email :" , formattingObject1); CellObject valueCellObject = GetCellObject("corpsales@devav.com", formattingObject2); valueCellObject.Hyperlink = "corpsales@devav.com"; e.ExportContext.AddRow(new[]{ labelCellObject, null, valueCellObject }); } #endregion #region 셀 추가 대리자 딕셔너리 구하기 - GetAddCellDelegateDictionary() /// <summary> /// 셀 추가 대리자 딕셔너리 구하기 /// </summary> /// <returns>셀 추가 대리자 딕셔너리</returns> private Dictionary<int, AddCellDelegate> GetAddCellDelegateDictionary() { Dictionary<int, AddCellDelegate> dictionary = new Dictionary<int, AddCellDelegate>(); dictionary.Add(9 , AddAddressRow1); dictionary.Add(10, AddAddressRow2); dictionary.Add(11, AddPhoneRow ); dictionary.Add(12, AddFAXRow ); dictionary.Add(13, AddEMailRow ); return dictionary; } #endregion #region 포맷 객체 구하기 - GetFormattingObject(bold, size) /// <summary> /// 포맷 객체 구하기 /// </summary> /// <param name="bold">볼드체 여부</param> /// <param name="size">크기</param> /// <returns>포맷 객체</returns> private XlFormattingObject GetFormattingObject(bool bold, double size) { XlFormattingObject formattingObject = new XlFormattingObject { Font = new XlCellFont { Bold = bold, Size = size }, Alignment = new XlCellAlignment { RelativeIndent = 10, HorizontalAlignment = XlHorizontalAlignment.Center, VerticalAlignment = XlVerticalAlignment.Center } }; return formattingObject; } #endregion #region 셀 병합하기 - MergeCells(e, left, top, right, bottom) /// <summary> /// 셀 병합하기 /// </summary> /// <param name="e">이벤트 인자</param> /// <param name="left">왼쪽</param> /// <param name="top">위쪽</param> /// <param name="right">오른쪽</param> /// <param name="bottom">아래쪽</param> private void MergeCells(ContextEventArgs e, int left, int top, int right, int bottom) { e.ExportContext.MergeCells ( new XlCellRange ( new XlCellPosition(left , top ), new XlCellPosition(right, bottom) ) ); } #endregion #region 셀 병합하기 - MergeCells(e) /// <summary> /// 셀 병합하기 /// </summary> /// <param name="e">이벤트 인자</param> private void MergeCells(ContextEventArgs e) { MergeCells(e, 2, 9 , 5, 9 ); MergeCells(e, 0, 9 , 1, 10); MergeCells(e, 2, 10, 5, 10); MergeCells(e, 0, 11, 1, 11); MergeCells(e, 2, 11, 5, 11); MergeCells(e, 0, 12, 1, 12); MergeCells(e, 2, 12, 5, 12); MergeCells(e, 0, 13, 1, 13); MergeCells(e, 2, 13, 5, 13); MergeCells(e, 0, 14, 5, 14); } #endregion } } |