유용한 정보

[C#] Excel to PrintPreview

DevReff 2025. 4. 16. 21:31
728x90
728x90
SMALL


/// <summary>
/// 엑셀파일을 미리보기한다.
/// </summary>
/// <param name="sFilePath">엑셀파일의 경로</param>
/// <returns>성공하면 에러메세지가 없고 그렇지않으면 발생한 에러 메세지를 반환함</returns>
string ExcelToPrintPreview(string sFilePath)
{
string sErrorMsg = string.Empty;

Excel.Application xlApp = null;
Excel._Workbook wb = null;
Excel.Worksheet ws = null;

try
{
xlApp = new Excel.Application();
//excelApp.ActivePrinter = PrinterSettings.InstalledPrinters[3];
wb = xlApp.Workbooks.Add(sFilePath);
if (wb == null)
return sErrorMsg;

xlApp.Visible = true;
xlApp.Sheets.PrintPreview(true);
}
catch (InvalidPrinterException pex)
{
sErrorMsg += string.Format("Error: {0} Line: {1}", pex.Message, pex.Source);
}
catch (Exception ex)
{
sErrorMsg += string.Format("Error: {0} Line: {1}", ex.Message, ex.Source);
}
finally
{
ExcelX.QuitExcel(xlApp, wb, ws);
}

return sErrorMsg;
}

728x90
728x90
LIST

'유용한 정보' 카테고리의 다른 글

[C#] Get MAC address  (0) 2025.04.16
[C#] Import Excel File To DataGridView  (0) 2025.04.16
[C#] Excel To Printer  (0) 2025.04.16
[C#] 데이터셋 연동하기  (0) 2025.04.16
[C#] DataGridView 컨트롤에서 열 고정하기  (0) 2025.04.16