M
M
Michael2016-01-27 22:50:29
C++ / C#
Michael, 2016-01-27 22:50:29

How to send an .xlsx file for printing next to the c# executable?

In general, there is a file, let's say test.xlsx, and you need to send it to print by pressing a button in the program (so that a printer choice appears, etc.)
I use the spreadsheetlight library to generate a document (and ideally I would print through it), but you can also print it here so Microsoft.Office.Interop.Excel
How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pilartser, 2016-01-29
@pilartser

I ripped it out of my old console project, written in a hurry.

internal static string SelectPrinter()
        {
            System.Windows.Forms.PrintDialog prntDlg = new System.Windows.Forms.PrintDialog();
            if (prntDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (prntDlg.PrinterSettings.IsValid)
                    return prntDlg.PrinterSettings.PrinterName;
            }
            return "";
        }

        internal static string GetPort(string printerName)
        {
            var devices = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Devices"); //Read-accessible even when using a locked-down account
            try
            {

                foreach (string name in devices.GetValueNames())
                {
                    if (name == printerName)
                    {
                        var value = (String)devices.GetValue(name);  
                        var port = Regex.Match(value, @"(,\w+:)", RegexOptions.IgnoreCase).Value;
                        port = port.Replace(",", "");
                        return port;
                    }
                }
            }
            catch
            {
                throw;
            }
            return "";
        }

        internal static string GetActivePrinter()
        {
            string printer = SelectPrinter();
            if (printer != "")
            {
                string port = GetPort(printer);
                if (port != "")
                    return printer + " (" + port + ")";
                else
                    return "";
            }
            else
                return "";
        }

        internal static void PrintExcelSheet(Excel.Application app, Excel.Worksheet sheet, String activePrinter)
        {
            try
            {
                app.ActivePrinter = activePrinter;
                sheet.PrintOutEx();
            }
            catch (Exception e)
            {
                Console.WriteLine("Print error:\r\n" + e.Message);
            }
        }

Well, the challenge
string printerName = GetActivePrinter();
if (printerName != "")
{
   Console.WriteLine("Menu send to printer: " + printerName);
   PrintExcelSheet(xlApp, xlWorkSheet, printerName);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question