-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from SyncfusionExamples/PrintExcelDocuments
C# Example for printing Excel documents
- Loading branch information
Showing
7 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
Real Time Examples in Excel/PrintExcelDocuments/PrintExcelDocuments.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.9.34414.90 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrintExcelDocuments", "PrintExcelDocuments\PrintExcelDocuments.csproj", "{FF3AC065-645E-40C3-8CBF-A6FF69E4A6EB}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{FF3AC065-645E-40C3-8CBF-A6FF69E4A6EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{FF3AC065-645E-40C3-8CBF-A6FF69E4A6EB}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{FF3AC065-645E-40C3-8CBF-A6FF69E4A6EB}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{FF3AC065-645E-40C3-8CBF-A6FF69E4A6EB}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {54807700-8BBC-4F7A-A107-DF6BBA481AC3} | ||
EndGlobalSection | ||
EndGlobal |
9 changes: 9 additions & 0 deletions
9
Real Time Examples in Excel/PrintExcelDocuments/PrintExcelDocuments/App.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Application x:Class="PrintExcelDocuments.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:PrintExcelDocuments" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
14 changes: 14 additions & 0 deletions
14
Real Time Examples in Excel/PrintExcelDocuments/PrintExcelDocuments/App.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Windows; | ||
|
||
namespace PrintExcelDocuments | ||
{ | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
Real Time Examples in Excel/PrintExcelDocuments/PrintExcelDocuments/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly: ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
23 changes: 23 additions & 0 deletions
23
Real Time Examples in Excel/PrintExcelDocuments/PrintExcelDocuments/MainWindow.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Window x:Name="Print_Excel" x:Class="PrintExcelDocuments.MainWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:local="clr-namespace:PrintExcelDocuments" | ||
mc:Ignorable="d" | ||
Title="MainWindow" Height="250" Width="532"> | ||
<Grid Margin="3,-1,0,-6" Background="#FFE0E4EB"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="37*"/> | ||
<ColumnDefinition Width="21*"/> | ||
<ColumnDefinition Width="81*"/> | ||
<ColumnDefinition Width="68*"/> | ||
<ColumnDefinition Width="278*"/> | ||
<ColumnDefinition Width="44*"/> | ||
</Grid.ColumnDefinitions> | ||
<Button Click="PrintExcel" Content="Print" HorizontalAlignment="Left" VerticalAlignment="Top" Width="110" Height="27" Margin="47,111,0,0" FontWeight="Bold" Grid.Column="3" Grid.ColumnSpan="2"/> | ||
<Label Content="Select Excel File" HorizontalAlignment="Left" Margin="24,60,0,0" VerticalAlignment="Top" Width="114" Height="25" FontSize="14" Grid.ColumnSpan="3"/> | ||
<TextBox Name="filePath" HorizontalAlignment="Left" Margin="4,61,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="195" Height="24" Grid.Column="3" Grid.ColumnSpan="2"/> | ||
<Button Content="Browse" HorizontalAlignment="Left" Margin="152,61,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Click="SelectFile" Width="68" Height="24" FontWeight="Bold" FontStyle="Italic" Grid.Column="4"/> | ||
</Grid> | ||
</Window> |
58 changes: 58 additions & 0 deletions
58
Real Time Examples in Excel/PrintExcelDocuments/PrintExcelDocuments/MainWindow.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Drawing.Printing; | ||
using System.Windows; | ||
using Syncfusion.ExcelToPdfConverter; | ||
using Syncfusion.XlsIO; | ||
|
||
namespace PrintExcelDocuments | ||
{ | ||
/// <summary> | ||
/// Interaction logic for MainWindow.xaml | ||
/// </summary> | ||
public partial class MainWindow : Window | ||
{ | ||
public MainWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
private void SelectFile(object sender, RoutedEventArgs e) | ||
{ | ||
//Initializes FileSavePicker. | ||
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog(); | ||
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm,*.xltm,*.csv,*.tsv"; | ||
openFileDialog.Title = "Select a Excel File"; | ||
openFileDialog.ShowDialog(); | ||
|
||
//Gets the path of specified file. | ||
filePath.Text = openFileDialog.FileName; | ||
} | ||
|
||
private void PrintExcel(object sender, RoutedEventArgs e) | ||
{ | ||
using (ExcelEngine excelEngine = new ExcelEngine()) | ||
{ | ||
IApplication application = excelEngine.Excel; | ||
application.DefaultVersion = ExcelVersion.Xlsx; | ||
|
||
//Loads or open an existing workbook through Open method of IWorkbooks | ||
IWorkbook workbook = application.Workbooks.Open(filePath.Text); | ||
|
||
//Initialize the printer settings | ||
PrinterSettings printerSettings = new PrinterSettings(); | ||
|
||
//customizing the printer settings | ||
printerSettings.PrinterName = "HP LaserJet Pro MFP M127-M128 PCLmS"; | ||
printerSettings.Copies = 2; | ||
printerSettings.FromPage = 2; | ||
printerSettings.ToPage = 3; | ||
printerSettings.DefaultPageSettings.Color = true; | ||
printerSettings.Duplex = Duplex.Vertical; | ||
printerSettings.Collate = true; | ||
|
||
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook); | ||
|
||
converter.Print(); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...Time Examples in Excel/PrintExcelDocuments/PrintExcelDocuments/PrintExcelDocuments.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net6.0-windows</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UseWPF>true</UseWPF> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Syncfusion.ExcelToPdfConverter.Wpf" Version="24.1.45" /> | ||
<PackageReference Include="System.Drawing.Common" Version="8.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |