C#Excel打印

方试一:使用Aspose.Cells组件(可以使用破解版),不需要按装Office,直接调用默认打印机打印,不会显示打印预览。

Workbook workbook = new Workbook("C:/abc.xlsx");
           
            //Get the worksheet to be printed
            Worksheet worksheet = workbook.Worksheets[0];
            //PageSetup pageSetup = worksheet.PageSetup;
            //pageSetup.Orientation = PageOrientationType.Landscape;
            //pageSetup.LeftMargin = 0;
            //pageSetup.RightMargin = 0.1;          
            //pageSetup.BottomMargin = 0.3;
            //pageSetup.PrintArea = "A2:J29";
            //Apply different Image / Print options.
            Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();

            //Set the Printing page property
            //options.PrintingPage = PrintingPageType.IgnoreStyle;
            //Render the worksheet
            SheetRender sr = new SheetRender(worksheet, options);

            System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
            string strPrinterName = printSettings.PrinterName;

            //send to printer
            //System.Drawing.Image map = sr.ToImage(0);

            sr.ToPrinter(strPrinterName);

方式二:使用Microsoft.Office.Interop.Excel(可在Nuget中按装),可显示打印预览界面,但需要按装Office。

void Print()
        {
            Excel.Application excelApp = new Excel.Application();

            //不可或缺
            excelApp.Visible = true;

            Excel.Workbook wb = excelApp.Workbooks.Open("C:/abc.xlsx", Type.Missing, Type.Missing,
                                                                                        Type.Missing, Type.Missing,
                                                                                        Type.Missing, Type.Missing,
                                                                                        Type.Missing, Type.Missing,
                                                                                        Type.Missing, Type.Missing,
                                                                                        Type.Missing, Type.Missing);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
            try
            {
                bool userDidntCancel = excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrintPreview].Show(
                                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                          Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            }
            catch (Exception en)
            {
            }


            GC.Collect();
            GC.WaitForPendingFinalizers();
            wb.Close(false, Type.Missing, Type.Missing);
            excelApp.Quit();
        }

方式三:需要按装Office,启动Excel进程直接打印文件,但会显示Excel程序。

void Print()
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process(); //系统进程
            process.StartInfo.CreateNoWindow = true; //不显示调用程序窗口
            process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//
            process.StartInfo.UseShellExecute = true; //采用操作系统自动识别模式
            process.StartInfo.FileName = "D:/abc.xlsx"; //要打印的文件路径
            process.StartInfo.Verb = "Print"; //指定执行的动作,打印:print 打开:open …………
            process.Start(); //开始打印
        }

方式四:使用dsoframerocx控件

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C是一种编程语言,它是由美国贝尔实验室的丹尼斯·里奇和肯·汤普逊在20世纪70年代早期开发的。C语言是一种通用的高级编程语言,具有简洁、可读性强以及功能强大的特点。 C语言在软件开发领域有着广泛的应用,很多操作系统、嵌入式系统、编译器和数据库系统都是使用C语言开发的。它不仅性能优越,而且可以直接访问底层硬件,因此在开发高效、可靠的应用程序时非常有优势。 C语言具有严格的语法和语义,充分利用计算机的硬件资源。它支持面向过程的编程方式,使得程序员可以按照顺序编写代码,逐步实现程序的功能。与其他高级语言相比,C语言的执行速度更快,效率更高。 C语言还提供了丰富的数据类型和运算符,使得程序员能够灵活地操作数据。它支持整数、浮点数、字符、字符串等多种数据类型,能够处理各种复杂的计算和数据操作。同时,C语言还提供了逻辑运算符、算术运算符、关系运算符等常用的运算符,以及循环语句、条件语句等控制结构,使得程序员能够编写出复杂的逻辑和算法。 总的来说,C语言是一种强大的编程语言,能够满足各种不同的编程需求。它已经成为计算机科学领域中的基础,为后续的编程语言发展奠定了基础。无论是初学者还是有经验的程序员,学习和使用C语言都是非常有价值的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值