예제 #1
0
        public int PrintCellToPDF(string cellFileName, string outFileName)
        {
            if (!System.IO.File.Exists(cellFileName))
            {
                return(0);
            }

            string FileExt = System.IO.Path.GetExtension(cellFileName);//文件后缀

            try
            {
                if (FileExt.ToLower() != ".cll")
                {
                    if (FileExt.ToLower() == ".pdf" ||
                        FileExt.ToLower() == ".daf")
                    {
                        System.IO.File.Copy(cellFileName, outFileName, true);
                        return(this.MergePDFFilesPages(outFileName));
                    }
                    else
                    {
                        if (this.FileConverToPDF(cellFileName, outFileName))
                        {
                            return(this.MergePDFFilesPages(outFileName));
                        }
                        else
                        {
                            return(0);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MyCommon.WriteLog("50cllPdf 转换pdf异常:" + ex.Message);
            }
            //System.Collections.ArrayList fileList = new System.Collections.ArrayList();
            //Cell2.SetPrinter(PrinterOperate.UsePrinterName);
            //Cell2.PrintPara(1, 1, 0, 0); //单色打印

            if (System.IO.Directory.Exists(tempPath))
            {
                MyCommon.DeleteAndCreateEmptyDirectory(tempPath, false);
                MyCommon.DeleteAndCreateEmptyDirectory(tempPath, true);
            }
            else
            {
                MyCommon.DeleteAndCreateEmptyDirectory(tempPath, true);
            }

            ArrayList fileList = new ArrayList();

            try
            {
                if (Cell2.OpenFile(cellFileName, "") != 1)
                {
                    return(0);
                }

                Cell2.PrintSetAlign(1, 1);
                Cell2.PrintSetMargin(5, 5, 5, 5);
                Cell2.PrintPara(1, 1, 0, 0); //单色打印
                //Cell2.SetCurSheet(j);
                //Cell2.PrintPageBreak(0);
            }
            catch (Exception ex)
            {
                MyCommon.WriteLog("Cell2 打开错误:" + ex.Message);
            }
            //判断当前页 所有的单元是否只读
            // 如果当前页的所有单元格只读,就不转换PDF
            // 否则 就直接转换PDF
            Assembly asm     = Assembly.GetExecutingAssembly();
            string   AppPath = asm.Location;

            try
            {
                bool isToPDF_flg = true;
                for (int j = 0; j < Cell2.GetTotalSheets(); j++)
                {
                    Cell2.SetCurSheet(j);
                    string TittleName = Cell2.GetSheetLabel(j);
                    if (TittleName != null)
                    {
                        if (TittleName.Contains("填表提示") || TittleName.Contains("填表说明") || TittleName.Trim() == "说明")//
                        {
                            continue;
                        }
                    }

                    string tempoutfileName = Guid.NewGuid().ToString() + ".pdf";
                    isToPDF_flg = true;
                    for (int iRow = 1; iRow < Cell2.GetRows(j) - 1; iRow++)
                    {
                        if (!isToPDF_flg)
                        {
                            break;
                        }
                        if (Cell2.IsRowHidden(iRow, j) == false && Cell2.GetRowHeight(1, iRow, j) > 10)
                        {
                            for (int iCol = 1; iCol < Cell2.GetCols(j) - 1; iCol++)
                            {
                                if (!isToPDF_flg)
                                {
                                    break;
                                }
                                if (Cell2.IsColHidden(iCol, j) == false && Cell2.GetColWidth(1, iCol, j) > 10)
                                {
                                    if (Cell2.GetCellInput(iCol, iRow, j) != 5)
                                    {
                                        isToPDF_flg = false;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if ((!isToPDF_flg))
                    {
                        //ConverToPDF.PreparePrint(Assembly.GetExecutingAssembly().FullName, "", tempPath + tempoutfileName);
                        //Cell2.PrintSheet(0, j);
                        //ConverToPDF.Wait4PringFinish("", tempPath + tempoutfileName);
                        if (Cell2.ExportPdfFile(tempPath + tempoutfileName, j, 0, 1))
                        {
                            fileList.Add(tempPath + tempoutfileName);
                        }
                    }
                }
                Cell2.closefile();
            }
            catch (Exception ex)
            {
                MyCommon.WriteLog("Cell2 转换PDF错误:" + ex.Message);
            }
            #region

            /*
             * //Cell2.ExportPdfFile(tempPath + tempoutfileName, -1, 0, Cell2.GetTotalSheets());
             *
             *
             * //for (int j = 0; j < Cell2.GetTotalSheets(); j++)
             * //{
             * //    string TittleName = Cell2.GetSheetLabel(j);
             * //    if (!TittleName.Contains(Globals.Descriptive))
             * //    {
             * //        fileList.Add(tempFile + j.ToString() + ".pdf");
             * //        outFile = tempFile + j.ToString() + ".pdf";
             * //        NewPrint();
             * //        oPrinterMonitor.OnPrinterInit += new _IPrinterMonitorEvents_OnPrinterInitEventHandler(oPrinterMonitor_OnPrinterInit);
             * //        Cell2.PrintSheet(0, j);
             * //        oPrinterMonitor.OnPrinterInit -= new _IPrinterMonitorEvents_OnPrinterInitEventHandler(oPrinterMonitor_OnPrinterInit);
             * //        Application.DoEvents();
             * //    }
             * //}
             *
             * //foreach (object obj in fileList)
             * //{
             * //    if(!System.IO.File.Exists(obj.ToString()))
             * //    {
             * //        fileList.Remove(obj);
             * //    }
             * //}
             * //if (fileList.Count <= 0)
             * //{
             * //    return 0;
             * //}
             * */
            #endregion
            string[] FileName = new string[fileList.Count];
            for (int i = 0; i < fileList.Count; i++)
            {
                FileName[i] = fileList[i].ToString();
            }
            if (FileName.Length <= 0)
            {
                return(0);
            }
            return(MergePDFFilesPages(outFileName));
        }
예제 #2
0
        private bool Printcells(string filepath, string printerName)
        {
            int b = Cell2.OpenFile(filepath, "");

            if (b != 1)
            {
                return(false);
            }

            Cell2.PrintSetAlign(1, 1);
            Cell2.PrintSetMargin(5, 5, 5, 5);

            //判断当前页 所有的单元是否只读
            // 如果当前页的所有单元格只读,就不转换PDF
            // 否则 就直接转换PDF
            bool isToPDF_flg = true;

            for (int j = 0; j < Cell2.GetTotalSheets(); j++)
            {
                Cell2.SetCurSheet(j);
                string TittleName = Cell2.GetSheetLabel(j);
                if (TittleName != null &&
                    TittleName.Contains("填表提示"))//
                {
                    continue;
                }

                isToPDF_flg = true;
                for (int iRow = 1; iRow < Cell2.GetRows(j) - 1; iRow++)
                {
                    if (!isToPDF_flg)
                    {
                        break;
                    }
                    if (Cell2.IsRowHidden(iRow, j) == false && Cell2.GetRowHeight(1, iRow, j) > 10)
                    {
                        for (int iCol = 1; iCol < Cell2.GetCols(j) - 1; iCol++)
                        {
                            if (!isToPDF_flg)
                            {
                                break;
                            }
                            if (Cell2.IsColHidden(iCol, j) == false && Cell2.GetColWidth(1, iCol, j) > 10)
                            {
                                if (Cell2.GetCellInput(iCol, iRow, j) != 5)
                                {
                                    isToPDF_flg = false;
                                    break;
                                }
                            }
                        }
                    }
                }
                if ((!isToPDF_flg))
                {
                    Cell2.PrintSheet(0, j);
                }
            }
            Cell2.closefile();
            return(true);
        }