마이크로소프트를 이용하다.Office.Interop.Excel에서 웹 페이지를 PDF로 변환
8044 단어 Microsoft
우선 웹 페이지 html를 EXCEL 파일로 저장합니다. (이 절차는 여러 가지 방법이 있기 때문에 상세하게 검토하지 않겠습니다. 웹 페이지를 EXCEL 파일로 바꿀 수만 있다면 나머지는 말할 수 있습니다.)
StringWriter html = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(html);
base.Render(tw);
//excelName Excel
FileStream fs = new FileStream(excelName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("utf-8"));
//
sw.Write(html);
//
sw.Flush();
//
sw.Close();
fs.Close();
그리고 마이크로소프트를 이용해서Office.Interop.Excel에서 EXCEL을 PDF로 변환
먼저 Microsoft를 참조하십시오.Office.Interop.Excel.dll, dll의 속성 값 설정
삽입할 수 없는 상호작용 형식은false입니다.그렇지 않으면 보고할 것이다
유형 "Microsoft.Office.Interop.Excel.ApplicationClass"정의되지 않은 구조 함수는 상호작용 유형 "Microsoft.Office.Interop.Excel.ApplicationClass"를 포함할 수 없습니다.적용 가능한 인터페이스 오류를 바꾸십시오.
그런 다음 SaveAsPDFandXPS를 설치합니다.exe (이 플러그인을 설치해야 excel을 pdf로 저장할 수 있습니다)
소스 코드는 다음과 같습니다.
/// <summary>
/// Excel PDF
/// </summary>
/// <param name="excelPath"> EXCEL </param>
/// <param name="pdfPath"> PDF </param>
/// <returns></returns>
public static bool CovertExcelToPDF( string excelPath, string pdfPath)
{
object missing = Type .Missing;
//// excel
ApplicationClass application = null ;
////
Workbook workBook = null ;
try
{
application = new ApplicationClass ();
////
workBook = application.Workbooks.Open(excelPath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
//// sheet
Worksheet ws = (Worksheet )workBook.Worksheets.Item[1];
////
ws.PageSetup.Orientation = XlPageOrientation .xlPortrait;
//// excel 。Zoom false
ws.PageSetup.Zoom = false ;
ws.PageSetup.FitToPagesTall = 1;
ws.PageSetup.FitToPagesWide = 1;
//// PDF XPS
ws.ExportAsFixedFormat( XlFixedFormatType .xlTypePDF, pdfPath
, XlFixedFormatQuality .xlQualityStandard
, true
, false ////
, missing, missing, missing, missing);
return true ;
}
catch
{
throw ;
}
finally
{
////
if (workBook != null )
{
workBook.Close( true , missing, missing);
workBook = null ;
}
//// excel
if (application != null )
{
application.Quit();
application = null ;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
[코드 예제]
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Microsoft에 인증되지 않은 취약점? 이야기Microsoft는 제품이나 서비스의 취약성을 발견하면 보상금을 받을 수 있는 제도 「Microsoft Bug Bounty Program」이 있다. 본 기사에서는 Microsoft에 인정되지 않았지만, 유저에게의 리...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.