public override void ClickPreviewButton()
{
ExcelMailMerge.Preview(CreatePrintout());
}
public override void ClickPrintButton()
{
ExcelMailMerge.Print(CreatePrintout());
}
private IWorkbook CreatePrintout()
{
if (gvwList.FocusedRowHandle < 0)
{
ShowMessageBox(Messages.NoDataSelected);
return null;
}
Dictionary<string, object> queryParams = new Dictionary<string, object>();
queryParams.Add("@p_orgdiv", SessionInfo.orgdiv);
queryParams.Add("@p_datnum", txtDatnum1.Text);
queryParams.Add("@p_login_user", SessionInfo.UserId);
try
{
WaitFormEx.Show();
IWorkbook mainWorkbook = ExcelMailMerge.GenerateMailMergeDocument("QC_A4000", queryParams);
mainWorkbook.Unit = DevExpress.Office.DocumentUnit.Millimeter;
// 첨부파일 정보 조회
string query = $@"
SELECT savenm, realnm
FROM sysAttachments
WHERE attdatnum = ISNULL(
( SELECT attdatnum
FROM QC040T
WHERE orgdiv = '{SessionInfo.orgdiv}' AND datnum = '{txtDatnum1.Text}'), '') ";
DataTable attachDatas = ExecuteSql(query, false)?[0];
// 이미지 파일 처리
var images = new List<Image>();
if ((attachDatas?.Rows?.Count ?? 0) > 0)
{
foreach (DataRow row in attachDatas.Rows)
{
string savenm = row.GetValue("savenm")?.ToString() ?? string.Empty;
string realnm = row.GetValue("realnm")?.ToString() ?? string.Empty;
bool isImageFile = Util.IsRecognisedImageFile(realnm);
byte[] fileBytes = null;
Image image = null;
// 이미지 or PDF 파일만 등록
if (isImageFile || Path.GetExtension(realnm).Equals(".pdf", StringComparison.OrdinalIgnoreCase))
{
if (isImageFile)
{
fileBytes = FileDownload(Path.Combine(AttachmentHelper.BaseUploadDirectory, savenm));
using (MemoryStream ms = new MemoryStream(fileBytes))
{
image = Image.FromStream(ms);
}
}
else
{
var fileBytesOfPdf = FileDownload(Path.Combine(AttachmentHelper.BaseUploadDirectory, savenm));
using (MemoryStream stream = new MemoryStream(fileBytesOfPdf))
{
PdfDocumentProcessor processor = new PdfDocumentProcessor();
processor.LoadDocument(stream);
image = processor.CreateBitmap(1, 1000);
fileBytes = image.ConvertImageToByteArray();
processor.CloseDocument();
}
}
images.Add(image);
}
}
}
foreach (Image image in images)
{
if (image == null)
continue;
Worksheet newWorksheet = mainWorkbook.Worksheets.Add();
newWorksheet.ActiveView.Margins.Top = 12F;
newWorksheet.ActiveView.Margins.Bottom = 12F;
newWorksheet.ActiveView.Margins.Left = 6F;
newWorksheet.ActiveView.Margins.Right = 6F;
Range range;
// 용지 가로 설정
if (image.Width > image.Height)
{
range = newWorksheet["A1:O36"];
newWorksheet.ActiveView.Orientation = PageOrientation.Landscape;
}
// 용지 세로 설정
else
{
range = newWorksheet["A1:L48"];
newWorksheet.ActiveView.Orientation = PageOrientation.Portrait;
}
newWorksheet.SetPrintRange(range);
// 이미지 추가
Picture picture = newWorksheet.Pictures.AddPicture(image, range, true);
// 이미지 가운데로 이동
float offsetX = (float)((range.ColumnWidth * range.ColumnCount) - picture.Width) / 2F;
float offsetY = (float)((range.RowHeight * range.RowCount) - picture.Height) / 2F;
picture.Move(offsetY, offsetX);
}
WaitFormEx.Close();
return mainWorkbook;
}
catch (Exception ex)
{
ShowErrorMessageBox(ex);
WaitFormEx.Close();
return null;
}
}