[C#] Excel to PrintPreview
/// <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;
}