Exemple #1
0
        public void Open(string fullFilePath)
        {
            mWorkbook = mWorkbooks._Open(fullFilePath,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value);

            mWorksheet = (Worksheet)mWorkbook.ActiveSheet;

            ColCount = 0;
            RowCount = 0;
        }
Exemple #2
0
        /// <summary>
        /// 打开工作簿
        /// </summary>
        /// <param name="fullFilePath"></param>
        public void Open(string fullFilePath)
        {
            mWorkbook = mWorkbooks._Open(fullFilePath,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value,
                                         Missing.Value, Missing.Value);

            mWorksheetArray = new Worksheet[mWorkbook.Sheets.Count];

            for (int i = 0; i < mWorksheetArray.Length; i++)
            {
                mWorksheetArray[i] = (Worksheet)mWorkbook.Sheets[i + 1]; // Excel 的 Sheet 索引由 1 开始算, 故 i + 1
            }

            mWorksheet = (Worksheet)mWorkbook.ActiveSheet;
        }
Exemple #3
0
        public static void PrintSpecial(string printerName,
                                        string filePath,
                                        int?paperSize    = null,
                                        bool isLandscape = false
                                        )
        {
            Microsoft.Office.Interop.Excel.Application mExcelApp       = null;
            Microsoft.Office.Interop.Excel.Workbooks   mWorkbooks      = null;
            Microsoft.Office.Interop.Excel.Workbook    mWorkbook       = null;
            Microsoft.Office.Interop.Excel.Worksheet[] mWorksheetArray = null;

            try
            {
                #region 打开Excel文档 ( 静默模式 )

                mExcelApp               = new Microsoft.Office.Interop.Excel.Application();
                mExcelApp.Visible       = false;
                mExcelApp.DisplayAlerts = false;

                mWorkbooks = mExcelApp.Workbooks;
                mWorkbook  = mWorkbooks._Open
                             (
                    Filename: filePath,
                    UpdateLinks: Missing.Value,
                    ReadOnly: Missing.Value,
                    Format: Missing.Value,
                    Password: Missing.Value,
                    WriteResPassword: Missing.Value,
                    IgnoreReadOnlyRecommended: Missing.Value,
                    Origin: Missing.Value,
                    Delimiter: Missing.Value,
                    Editable: Missing.Value,
                    Notify: Missing.Value,
                    Converter: Missing.Value,
                    AddToMru: Missing.Value
                             );

                mWorksheetArray = new Microsoft.Office.Interop.Excel.Worksheet[mWorkbook.Sheets.Count];

                for (int i = 0; i < mWorksheetArray.Length; i++)
                {
                    mWorksheetArray[i] = (Microsoft.Office.Interop.Excel.Worksheet)mWorkbook.Sheets[i + 1]; // Excel 的 Sheet 索引由 1 开始算, 故 i + 1
                }

                if (mWorkbook.Saved == false) // 打开Excel文件后, 公式会自动运算, 运算结果需要保存
                {
                    mWorkbook.Save();
                }

                #endregion

                #region 设置打印机

                if (printerName.IsNullOrWhiteSpace() == false ||
                    mExcelApp.ActivePrinter.StartsWith(printerName) == false
                    )
                {
                    mExcelApp.ActivePrinter = printerName;

                    // 需要获取这样特殊的打印机名称 Ne04 ==> net 04 号打印机
                    // 修改打印机测试成功
                    // Foxit Reader PDF Printer 在 Ne04:
                    // HP LaserJet Professional P1606dn 在 Ne03:
                    //string p = "Foxit Reader PDF Printer 在 Ne04:";
                    //excelApp.ActivePrinter = p;
                }

                #endregion

                #region 设置纸张大小 & 旋转

                for (int index = 0; index < mWorksheetArray.Length; index++)
                {
                    Microsoft.Office.Interop.Excel.Worksheet wookSheet = mWorksheetArray[index];

                    if (paperSize.HasValue)
                    {
                        wookSheet.PageSetup.PaperSize = (Microsoft.Office.Interop.Excel.XlPaperSize)paperSize.Value;
                    }

                    if (isLandscape == true)
                    {
                        wookSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape; // 横向
                    }
                    else
                    {
                        wookSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlPortrait; // 纵向
                    }
                }

                #endregion

                #region 个性化代码


                #endregion 个性化代码
            }
            catch (Exception ex)
            {
                #region 关闭 Excel 进程 & 释放资源

                try
                {
                    if (mWorksheetArray != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(mWorksheetArray);
                    }
                    mWorksheetArray = null;


                    if (mWorkbook != null)
                    {
                        mWorkbook.Close
                        (
                            SaveChanges: false,
                            Filename: null,
                            RouteWorkbook: null
                        );

                        System.Runtime.InteropServices.Marshal.ReleaseComObject(mWorkbook);
                    }
                    mWorkbook = null;

                    if (mWorkbooks != null)
                    {
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(mWorkbooks);
                    }
                    mWorkbooks = null;

                    if (mExcelApp != null)
                    {
                        mExcelApp.Quit();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(mExcelApp);
                    }
                    mExcelApp = null;
                }
                catch (Exception ex2)
                {
                    Util.LogUtils.LogAsync(ex2.GetFullInfo());
                }

                // 关闭Excel进程 -- 核心代码
                GC.Collect();
                GC.WaitForPendingFinalizers();

                #endregion

                throw ex;
            }
        }