c# excel转pdf

07-19 1116阅读

前提条件:

c# excel转pdf
(图片来源网络,侵删)

电脑安装了:Microsoft.Office。如没有安装,则不可以调用此方法。

public string ExceltoPdfs(string excelpath, string pdfpath)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = false;
                Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(excelpath);
                wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, pdfpath);
                wkb.Close();
                app.Quit();
                return pdfpath;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                throw ex;
            }
        }
public string ExceltoPdfss(string excelLocation, string outputLocation)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = false;
                Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(excelLocation);
                //wkb.Worksheets["sheet1"].PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;//横向打印
                wkb.Worksheets["sheet1"].PageSetup.Zoom = false;//不自动缩放
                wkb.Worksheets["sheet1"].PageSetup.FitToPagesWide = 1;//宽度适应页面
                //wkb.Worksheets["sheet1"].PageSetup.FitToPagesTall = false;//高度不适应页面
                wkb.Worksheets["sheet2"].PageSetup.Zoom = false;
                wkb.Worksheets["sheet2"].PageSetup.FitToPagesWide = 1;
                wkb.Worksheets["sheet3"].PageSetup.Zoom = false;
                wkb.Worksheets["sheet3"].PageSetup.FitToPagesWide = 1;
                wkb.Worksheets["sheet4"].PageSetup.Zoom = false;
                wkb.Worksheets["sheet4"].PageSetup.FitToPagesWide = 1;
                wkb.Worksheets["sheet5"].PageSetup.Zoom = false;
                wkb.Worksheets["sheet5"].PageSetup.FitToPagesWide = 1;
                wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputLocation);
                wkb.Close();
                app.Quit();
                app.DisplayAlerts = false;
                return outputLocation;
            }
            catch (Exception ex)
            {
                MessageBox.Show("转换失败");
                throw ex;
            }
        }
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]