Can't print an Excel document from a console application

Hello.

I want to silently print Excel files via a C# console application. However, when attempting to follow this example program to silently print Excel files in C# I get an error message like the one shown in the attached image.

This is the example program I was following:

Print Excel files in C# and VB.NET (gemboxsoftware.com)

Hi! Which framework are you targeting? If it’s .NET you need to use the Windows specific TFM like net8.0-windows to be able to use printing. You can find more information here: Supported Platforms | GemBox.Spreadsheet

Hi marko.

I’m targeting .NET 5.0

Hi Aden,

I’m afraid that GemBox components support printing on .NET 6.0 and above.
In other words, you need to target .NET 6.0 on Windows:

<TargetFramework>net6.0-windows</TargetFramework>

Regards,
Mario

Hi Mario,

Thanks for letting me know. Since you said .NET 6.0 and above, I assume that GemBox will work on .NET 8.0 then? I just upgraded to using Visual Studio 2022 with .NET 8.0 and will be doing some testing of it in the coming days.

Yes, it will work on .NET 8.0 as well:

<TargetFramework>net8.0-windows</TargetFramework>

Hi Mario.

Unfortunately, it doesn’t seem to be working for me. I’m using .NET 8.0 with Visual Studio 2022 and I get the same error message. Keep in mind that this is a console application so maybe that sample code won’t work with a console app:

Here’s the other image I wanted to attach in my previous post but couldn’t since I’m a new member:

Double-click on your project:
project-file

It will open your project file. There you will find this:

<TargetFramework>net8.0</TargetFramework>

Change that to this:

<TargetFramework>net8.0-windows</TargetFramework>

Does this solve your issue?

1 Like

Hi Mario.

That solved my issue; thank you for the assistance. However, now I have another issue which is that nothing prints when the program runs. Meaning the sample program I was using:

I set the printer name to a network printer and made sure that the file being loaded exists. Nothing prints. If I change the printer name to a printer name that doesn’t exist, I get an error message which is good. However, nothing happens when I have it set to a printer that exists. The program just finishes running and nothing prints.

Unfortunately, I was unable to reproduce this issue.
Can you try using a different printer, perhaps a local printer?
Also, can you try using a virtual printer (for example, “Microsoft Print to PDF”)?

Hi Mario,

I tried using a different network printer and that didn’t work. However, Microsoft Print to PDF did work. Unfortunately, I need the document to actually print rather than just saving it as a PDF.

Can you show us how you specify a network printer?
Also, have you tried using a local printer?

Unfortunately, without reproducing this issue I’m not sure how I can help you with it.

Have you printed to a network printer using GemBox? Also, thanks for trying. :slight_smile:

Yes, I’ve printed to a network printer using GemBox.Spreadsheet.

Hi Mario.

I’m happy to report that I was able to get the sample code to work. I had previously added content to a single cell in the Excel file that I was using to test on. Once I replaced that with a file that had more content, the page printed out. :raised_hands:

However, the row numbers are included on the printed page. I didn’t see an option in the worksheet PrintOptions to remove those. It makes the printed page look uglier. Do you happen to know how to remove them?

Are both row numbers and column letters included?
In that case, try setting the ExcelWorksheet.PrintOptions.PrintHeadings to false.

No. Just the row numbers on the side are included. When I try that, the top and bottom portions of the sheet get cut off.

Please send us your Excel file so that we can investigate this.
You can send the file via email or support ticket, see the Contact page.