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
}
}