首页 > 代码库 > Excel转换成PDF

Excel转换成PDF

public  class Office2Pdf    {        public bool DOCConvertToPDF(string sourcePath, string targetPath)        {            //StreamReader reader = new StreamReader("temp.doc", Encoding.GetEncoding("gb2312"));            //string text = reader.ReadToEnd();            //Aspose.Words.Document doc = new Aspose.Words.Document();            //Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);            //builder.Write(text);            //doc.Save("temp.pdf", SaveFormat.Pdf);            //reader.Close();            bool result = false;            Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;            object paramMissing = Type.Missing;            Word.ApplicationClass wordApplication = new Word.ApplicationClass();            Word.Document wordDocument = null;            try            {                object paramSourceDocPath = sourcePath;                string paramExportFilePath = targetPath;                Word.WdExportFormat paramExportFormat = exportFormat;                bool paramOpenAfterExport = false;                Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;                int paramStartPage = 0;                int paramEndPage = 0;                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;                bool paramIncludeDocProps = true;                bool paramKeepIRM = true;                Word.WdExportCreateBookmarks paramCreateBookmarks =                    Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;                bool paramDocStructureTags = true;                bool paramBitmapMissingFonts = true;                bool paramUseISO19005_1 = false;                wordDocument = wordApplication.Documents.Open(                    ref paramSourceDocPath, ref paramMissing, ref paramMissing,                    ref paramMissing, ref paramMissing, ref paramMissing,                    ref paramMissing, ref paramMissing, ref paramMissing,                    ref paramMissing, ref paramMissing, ref paramMissing,                    ref paramMissing, ref paramMissing, ref paramMissing,                    ref paramMissing                    );                if (wordDocument != null)                    wordDocument.ExportAsFixedFormat(                        paramExportFilePath, paramExportFormat, paramOpenAfterExport,                        paramExportOptimizeFor, paramExportRange, paramStartPage,                        paramEndPage, paramExportItem, paramIncludeDocProps,                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,                        paramBitmapMissingFonts, paramUseISO19005_1, ref paramMissing                        );                return true;            }            catch (Exception ex)            {                ex.WriteLog(ex.ToString());                return false;            }            finally            {                if (wordDocument != null)                {                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);                    wordDocument = null;                }                if (wordApplication != null)                {                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);                    wordApplication = null;                }                GC.Collect();                GC.WaitForPendingFinalizers();                GC.Collect();                GC.WaitForPendingFinalizers();            }            return result;        }    }

 

Excel转换成PDF