Skip to content

Commit

Permalink
Merge pull request #71 from SyncfusionExamples/PrintExcelDocuments
Browse files Browse the repository at this point in the history
C# Example for printing Excel documents
  • Loading branch information
Mohan2401 authored Jan 16, 2024
2 parents 3f9fba5 + 2cd9d53 commit 08fa30e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
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
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>
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
{
}

}
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)
)]
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>
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();
}
}
}
}
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>

0 comments on commit 08fa30e

Please sign in to comment.