如何设置EXCEL打印设置"打印整个工作表"? C#

时间:2016-12-08 11:54:01

标签: c# excel winforms excel-interop

我的程序会将WorkSheets导出到同一个EXCEL文件中。我正在尝试将打印默认设置设置为"打印整个工作簿"。我尝试使用PrintOutEx进行设置,但会在导出worksheets时立即打印出EXCEL或进入预览模式。

我只想将默认设置为Print Entire Workbook。我想让用户先读取excel文件。怎么办?感谢。

这是我的代码:

Excel.Application oApp;
                Excel.Worksheet oSheet;
                Excel.Workbook oBook;

                //Check if the row records are more then the table row.
                if (rowCount > 19)
                    more_than_table = true;

                oApp = new Excel.Application();
                oBook = oApp.Workbooks.Add();

                do {
                    Debug.WriteLine("Page: " + sheet_page);

                oSheet = (Excel.Worksheet)oBook.Worksheets.Add(After: oBook.Sheets[oBook.Sheets.Count]);

                oSheet.Name = invoices_tb.Text + "(" + sheet_page + ")";
                oApp.Application.UserName = invoices_tb.Text;
                oApp.Visible = true;
                //Set up the excel page style, orientation and the center alignment
                //oSheet.PageSetup.Zoom = false;
                oSheet.PageSetup.Zoom = false;
                oSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
                oSheet.PageSetup.CenterHorizontally = true;
                oSheet.PageSetup.FitToPagesWide = 1;

                //Set the page number
                oSheet.PageSetup.CenterFooter = "Page &P of &N";

                //**
                //Many code for set up excel and value
                //**

                    double how_many_page = Convert.ToDouble(rowCount) / 19.0f ;
                    Debug.WriteLine("many page: " + how_many_page);
                    if (how_many_page > sheet_page)
                        sheet_page++;
                    else
                        more_than_table = false;

                } while (more_than_table);

                //Set to preview the page when file export.
                oBook.PrintOutEx(1, sheet_page, 1, true, Type.Missing, true, false, Type.Missing);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Excel Closed.");
            }

1 个答案:

答案 0 :(得分:0)

使用此行使用C#...

打印整个工作簿

excelWorkbook.PrintOut(预览:true,整理:true);