コード例 #1
0
        private bool XLSConvertToPDF(string sourcePath, string targetPath)
        {
            bool result = false;

            Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
            object missing = Type.Missing;

            Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp  = null;
            Microsoft.Office.Interop.Excel._Workbook        ExcelBook = null;
            try
            {
                object target = targetPath;
                object type   = targetType;
                //workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                //        missing, missing, missing, missing, missing, missing, missing, missing, missing);
                // sourcePath = "C:\\Users\\IBM_ADMIN\\Desktop\\newadd.xlsx";
                System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                System.Reflection.Missing missingValue = System.Reflection.Missing.Value;
                ExcelBook = ExcelApp.Workbooks.Open(sourcePath, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue, missingValue);

                //ExcelApp.Visible = true;
                //ExcelApp.ScreenUpdating = true;

                ////ActiveWindow.SmallScroll Down:=6;
                //ExcelApp.ActiveWindow.View = Excel.XlWindowView.xlPageBreakPreview;
                ////ExcelApp.ActiveWindow.SmallScroll = Excel.;
                //ExcelApp.ActiveWindow.Zoom = 80;
                ////ActiveWindow.SmallScroll Down:=-3
                //// excelRange.WrapText = true;
                ////ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
                //ActiveWindow.SmallScroll Down:=30
                //Set ActiveSheet.HPageBreaks(1).Location = Range("A67")
                //ActiveWindow.SmallScroll Down:=-75

                //Microsoft.Office.Interop.Excel.Worksheet WS2 = (Microsoft.Office.Interop.Excel.Worksheet)ExcelBook.Worksheets[2];

                ////上边距
                //double top = 0;
                ////左边距
                //double left = 0;
                ////右边距
                //double right = 0;
                ////下边距
                //double footer = 0;
                //WS2.DisplayAutomaticPageBreaks = false;//显示分页线
                //WS2.PageSetup.CenterFooter = "第   &P   页,共   &N   页";
                //WS2.PageSetup.TopMargin = ExcelApp.InchesToPoints(top / 2.54);//上
                //WS2.PageSetup.BottomMargin = ExcelApp.InchesToPoints(footer / 15.54);//下
                //WS2.PageSetup.LeftMargin = ExcelApp.InchesToPoints(left / 2.54);//左
                //WS2.PageSetup.RightMargin = ExcelApp.InchesToPoints(right / 2.54);//右
                //WS2.PageSetup.CenterHorizontally = true;//水平居中   xlSheet.PageSetup.PrintTitleRows = "$1:$3";//顶端标题行
                //WS2.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA3;//A3纸张大小   xlSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;//纸张方向.横向


                //Excel.Range excelRange = WS2.get_Range(WS2.Cells[1, 1], WS2.Cells[64, 24]);
                //自动调整列宽
                ////  excelRange.EntireColumn.AutoFit();
                ////   excelRange.WrapText = false;     //文本自动换行
                //excelRange.ShrinkToFit = false;
                ////设置字体在单元格内的对其方式
                //excelRange.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                //// 文本水平居中方式
                //excelRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;

                //////设置为横向打印
                ////WS2.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;


                ////ExcelApp.ActiveWindow.FreezePanes = true;

                ////excelRange.EntireColumn.AutoFit();
                ////WS2.PageSetup.Orientation = 2;
                //WS2.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4;

                //WS2.PageSetup.LeftMargin = ExcelApp.InchesToPoints(0.0);
                //WS2.PageSetup.RightMargin = ExcelApp.InchesToPoints(0.0);
                //WS2.PageSetup.TopMargin = ExcelApp.InchesToPoints(0.0);
                //WS2.PageSetup.BottomMargin = ExcelApp.InchesToPoints(0.0);
                //WS2.PageSetup.HeaderMargin = ExcelApp.InchesToPoints(0.0);
                //WS2.PageSetup.FooterMargin = ExcelApp.InchesToPoints(0.0);
                //WS2.PageSetup.CenterHorizontally = true;
                ////  WS2.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4;
                //WS2.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
                //excelRange = WS2.get_Range(WS2.Cells[1, 1], WS2.Cells[2, 20]);
                //WS2.PageSetup.PrintTitleRows = excelRange.get_Address(excelRange.Row, excelRange.Column, Excel.XlReferenceStyle.xlA1, 1, 1);


                ExcelBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                result = true;
            }

            catch
            {
                result = false;
            }
            finally
            {
                if (ExcelBook != null)
                {
                    ExcelBook.Close(true, missing, missing);
                    ExcelBook = null;
                }
                if (ExcelApp != null)
                {
                    ExcelApp.Quit();
                    ExcelApp = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return(result);
        }