page 216

How to Print Excel File in WPF

2016-04-25 02:31:52 Written by Koohji

Spire.XLS supports to silently print an Excel file as well as print document with a print dialog, which is provided by System.Windows.Controls namespace in WPF, allowing users to select a specified printer and also the print pages. This article demonstrates how to print Excel file from a WPF application by invoking the print dialog in C# and VB.NET.

Necessary Namespaces:

using System.Windows;
using System.Windows.Controls;
using System.Drawing.Printing;
using Spire.Xls;

Code Snippet:

Step 1: Initialize an instance of Workbook and load a sample Excel file that you want to print.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");

Step 2: Create a new object of PrintDialog and set its properties such as PageRangeSelection and UserPageRangeEnabled.

PrintDialog dialog = new PrintDialog();     
dialog.UserPageRangeEnabled = true;
PageRange rang = new PageRange(1, 3);
dialog.PageRange = rang;            
PageRangeSelection seletion = PageRangeSelection.UserPages;
dialog.PageRangeSelection =seletion;

Step 3: Get the print document and invoke the print dialog to print.

PrintDocument pd = workbook.PrintDocument;
if (dialog.ShowDialog() == true)
{
    pd.Print();
}

Output:

How to Print Excel File in WPF

Full Code:

[C#]
using Spire.Xls;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("sample.xlsx");
            PrintDialog dialog = new PrintDialog();
            dialog.UserPageRangeEnabled = true;
            PageRange rang = new PageRange(1, 3);
            dialog.PageRange = rang;
            PageRangeSelection seletion = PageRangeSelection.UserPages;
            dialog.PageRangeSelection = seletion;
            PrintDocument pd = workbook.PrintDocument;
            if (dialog.ShowDialog() == true)
            {
                pd.Print();
            }
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Windows
Imports System.Windows.Controls

Namespace WpfApplication1
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim workbook As New Workbook()
			workbook.LoadFromFile("sample.xlsx")
			Dim dialog As New PrintDialog()
			dialog.UserPageRangeEnabled = True
			Dim rang As New PageRange(1, 3)
			dialog.PageRange = rang
			Dim seletion As PageRangeSelection = PageRangeSelection.UserPages
			dialog.PageRangeSelection = seletion
			Dim pd As PrintDocument = workbook.PrintDocument
			If dialog.ShowDialog() = True Then
				pd.Print()
			End If
		End Sub
	End Class
End Namespace

It's well known that we can add a variety of bullet styles to paragraphs in Word, such as various symbols, pictures, fonts and numbers. Bullet makes these paragraphs to be a bulleted list, so that we can easily create a well-structured and professional document.

This article will demonstrate how to set bullet style for word paragraphs in WPF applications using Spire.Doc for WPF.

Below is the effective screenshot after setting bullet style:

How to Set Bullet Style for Word Paragraphs in WPF

Code snippets:

Use namespace:

using System.Windows;
using Spire.Doc;
using Spire.Doc.Documents;

Step 1: Create a new instance of Document class and load the sample word document from file.

Document doc = new Document();
doc.LoadFromFile("Instruction.docx");

Step 2: Create a bulleted list.

Get the first section, then apply bullet style to paragraphs from 5th to 13th and set bullet position.

Section sec = doc.Sections[0];
for (int i = 4; i < 13; i++)
{
    Paragraph paragraph = sec.Paragraphs[i];
    paragraph.ListFormat.ApplyBulletStyle();
    paragraph.ListFormat.CurrentListLevel.NumberPosition = -20;
}

In addition, we can also use following code to create a numbered list:

paragraph.ListFormat.ApplyNumberedStyle();

Step 3: Save and launch the file.

doc.SaveToFile("Bullet.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bullet.docx");

Full codes:

private void button1_Click(object sender, RoutedEventArgs e)
{
    //Load Document
    Document doc = new Document();
    doc.LoadFromFile("Instruction.docx");

    //Set Bullet Style
    Section sec = doc.Sections[0];
    for (int i = 4; i < 13; i++)
    {
    Paragraph paragraph = sec.Paragraphs[i];

    //Create a bulleted list
    paragraph.ListFormat.ApplyBulletStyle();
    
        //Create a numbered list
    // paragraph.ListFormat.ApplyNumberedStyle();

        paragraph.ListFormat.CurrentListLevel.NumberPosition = -20;
    }

    //Save and Launch
    doc.SaveToFile("Bullet.docx", FileFormat.Docx);
    System.Diagnostics.Process.Start("Bullet.docx");
}

Renaming excel worksheet can makes readers clearly understand what we want to present at their first sight, at the same time, we can also set tab color for a specific worksheet, in order to distinguish it from others and find it in a very short time.

This section will demonstrate how to rename and set tab color for excel worksheet in WPF using Spire.XLS for WPF.

Please follow the detail steps and code snippets below:

Use namespace:

using System.Drawing;
using System.Windows;
using Spire.Xls;

Step 1: Initialize a new instance of Workbook class and load the sample excel document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the worksheets that need to be renamed and set tab color for. Here we choose the first and the second worksheets.

Worksheet sheet = workbook.Worksheets[0];
Worksheet sheet1 = workbook.Worksheets[1];

Step 3: Rename and set tab color.

Rename the first and the second worksheets.

sheet.Name = "Sales Report";
sheet1.Name = "Vendors Info";

Set tab color of the second worksheet as Lawn Green.

sheet1.TabColor = Color.LawnGreen;

Step 4: Save the changes and launch the file.

workbook.SaveToFile("Rename.xlsx");
System.Diagnostics.Process.Start("Rename.xlsx");

Result:

How to Rename and Set Tab color for Excel Worksheet in WPF

Full codes:

using Spire.Xls;
using System.Drawing;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load the excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[0];
            Worksheet sheet1 = workbook.Worksheets[1];

            //rename worksheets
            sheet.Name = "Sales Report";
            sheet1.Name = "Vendors Info";

            //set tab color
            sheet1.TabColor = Color.LawnGreen;

            //save and launch the file
            workbook.SaveToFile("Rename.xlsx");
            System.Diagnostics.Process.Start("Rename.xlsx");
        }
    }
}
page 216