/// <summary>
/// 엑셀파일을 출력한다.
/// </summary>
/// <param name="sFilePath">엑셀파일의 경로</param>
/// <param name="sSheetName">엑셀파일의 Sheet Name</param>
/// <param name="printer">프린터명</param>
/// <returns>성공하면 에러메세지가 없고 그렇지않으면 발생한 에러 메세지를 반환함</returns>
string ExcelToPrint(string sFilePath, string sSheetName = "", object printer = null)
{
string sErrorMsg = string.Empty;
Excel.Application excelApp = new Excel.Application();
Excel.Workbooks wbs = excelApp.Workbooks;
Excel._Workbook wb = null;
Excel.Worksheet ws = null;
try
{
excelApp.DisplayAlerts = false;
wb = wbs.Open(sFilePath, 0, true, 5, "", "", true,
Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, true, true);
if (sSheetName.Length > 0)
ws = (Excel.Worksheet)wb.Sheets[sSheetName];
else
ws = (Excel.Worksheet)wb.ActiveSheet;
//_Workbook.PrintOut(object From /// 인쇄를 시작할 페이지 번호입니다. 이 인수를 생략하면 인쇄가 처음부터 시작됩니다.
// , Object To /// 인쇄할 마지막 페이지 번호입니다. 이 인수를 생략하면 마지막 페이지까지 인쇄됩니다.
// , object Copies /// 인쇄할 매수입니다. 이 인수를 생략하면 한 부만 인쇄됩니다.
// , object Preview /// Microsoft Office Excel에서 개체를 인쇄하기 전에 인쇄 미리 보기를 호출하려면 true이고, 개체를 즉시 인쇄하려면 false(또는 생략)입니다.
// , object ActivePrinter /// 활성 프린터의 이름을 설정합니다
// , object PrintToFile /// 파일로 인쇄하는 경우 true입니다. PrToFileName이 지정되지 않으면 Excel에서 출력 파일의 이름을 입력하라는 메시지를 표시합니다.
// , object Collate /// 여러 장을 한 부씩 인쇄하는 경우 true입니다.
// , object PrToFileName /// PrintToFile이 true로 설정되면 이 인수는 인쇄할 파일의 이름을 지정합니다.
//);
ws.PrintOut(1, Type.Missing, 1, false, (printer == null) ? Type.Missing : printer, false, false, Type.Missing);
return sErrorMsg;
}
catch (Exception ex)
{
sErrorMsg = string.Format("Error: {0} Line: {1}", ex.Message, ex.Source);
}
finally
{
QuitExcel(excelApp, wb, ws);
}
return sErrorMsg;
}
'유용한 정보' 카테고리의 다른 글
[C#] Import Excel File To DataGridView (0) | 2025.04.16 |
---|---|
[C#] Excel to PrintPreview (0) | 2025.04.16 |
[C#] 데이터셋 연동하기 (0) | 2025.04.16 |
[C#] DataGridView 컨트롤에서 열 고정하기 (0) | 2025.04.16 |
[C#] 문자열을 속성으로 갖는 클래스 만들기 (0) | 2025.04.16 |