日期:2014-05-20 浏览次数:20536 次
   /// <summary>
        /// excel转换为pdf
        /// </summary>
        /// <param name="file_inputname"></param>
        public void PrintSheetToPDF(string fileInputName)
        {
            try
            {
                GetPrinterName(ref PrinterName, ref obj_printer_settings, ref oType);
                PrintDocument prtdoc = new PrintDocument();
                currentPrinterName = prtdoc.PrinterSettings.PrinterName;   //获取默认的打印机名 
                //更改默认打印机
                SetDefaultPrinter(PrinterName.Trim());
                //通知更改
                SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows");
                if (fileInputName == "")
                {
                    return;
                }
                filePath = fileSavePath + "\\" + GetFileName(fileInputName) + ".pdf";
                oType.InvokeMember("Init", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, null);
                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "output", filePath });
                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "showsettings", "never" });
                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ShowPDF", "no" });
                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ShowProgress", "no" });
                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ShowProgressFinished", "no" });
                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "SuppressErrors", "yes" });
                oType.InvokeMember("SetValue", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { "ConfirmOverwrite", "no" });
                oType.InvokeMember("WriteSettings", System.Reflection.BindingFlags.InvokeMethod, null, obj_printer_settings, new object[] { true });
                if (File.Exists(filePath) == true)
                {
                    File.Delete(filePath);
                }
                object objMissing = System.Type.Missing;
                object objValue = true;
                Microsoft.Office.Interop.Excel.Application objExcel = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook objWorkbook = objExcel.Workbooks.Open(fileSavePath+"\\"+fileInputName, objMissing, objValue, objMissing, objMissing,objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
                objExcel.Visible = false;
                for (int i = 1; i < objWorkbook.Worksheets.Count; i++)
                {
                    _Worksheet active_workbook = (_Worksheet)objExcel.Worksheets.get_Item(i);
                    active_workbook.PageSetup.Zoom = false;
                    active_workbook.PageSetup.FitToPagesWide = 1;
                    active_workbook.PageSetup.FitToPagesTall = 10;
                }
                objExcel.ActiveWorkbook.PrintOut(objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
                for (int f_intStep = 1; f_intStep < 20000; f_intStep++)
                {
                    System.Threading.Thread.Sleep(100);
                    if (File.Exists(filePath) == true)
                    {
                        break;
                    }
                }
                objWorkbook.Close(false, fileInputName, false);
                objExcel.Quit();
                objWorkbook = null;
                objExcel = null;
                int k = 0;
                IntPtr t = new IntPtr(objExcel.Hwnd);
                GetWindowThreadProcessId(t, out   k);
                System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                p.Kill();
                obj_printer_settings = null;
                //更改默认打印机
                SetDefaultPrinter(currentPrinterName.Trim());
                //通知更改
                SendNotifyMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows");
            }
            catch (Exception e)
            {
                obj_printer_settings = null;
                //throw e;
            }
        }