page 280

Insert Background Image in WPF

2012-10-22 02:51:53 Written by Koohji

Excel Background Image, one kind of page layout setting, is used to beautify files. Actually, with a beautiful background image, the Excel file will be more attractive to readers. Also, different from inserting image in Excel directly, background image will not cover data information. It means that all the data in Excel can be displayed even though background image is inserted.

Spire.XLS for WPF, a professional component to operate Excel files in WPF applications, enables users to insert background image in Excel. This guide will focus on how to realize this function by using C#, VB.NET.

Assign value for BackgroundImage property of PageSetup in Worksheet class to insert background image. Because the type of BackgroundImage is Bitmap, so the assigned value must be bitmap image. The following screenshot shows result after inserting background image.

Insert Excel Background Image

Download and install Spire.XLS for WPF. Then add a button in MainWindow. Double click the button to use the following code to insert background image in Excel.

[C#]
         Bitmap bm = new Bitmap(Image.FromFile(@"E:\Work\Documents\SampleImage\Flower.jpg"));
            sheet.PageSetup.BackgoundImage = bm;
[VB.NET]
         Dim bm As New Bitmap(Image.FromFile("E:\Work\Documents\SampleImage\Flower.jpg"))
        sheet.PageSetup.BackgoundImage = bm

Spire.XLS allows user to operate Excel document directly such as save to stream, save as web response, copy, lock/unlock worksheet, set up workbook properties, etc. As a professional WPF/.NET/Silverlight Excel component, it owns the ability of inserting content into Excel document, formatting cells and converting Excel documents to popular office file formats. Spire.XLS for WPF supports Excel 97-2003, Excel 2007 and Excel 2010.

Convert RTF to HTML in WPF

2012-10-15 07:24:19 Written by Koohji

This section will show you a detail solution to easily convert RTF to HTML in your WPF application via a .NET Word component. Only two lines of core code in total will be used to realize your RTF to HTML task in this solution.

Spire.Doc for WPF, as a professional MS Word component on WPF, enables you to accomplish RTF to HTML task through following two methods: Document.LoadFromFile(string fileName, FileFormat fileFormat) called to load your RTF file from system and Document. SaveToFile(string ilename, FileFormat fileFormat) is used to save the RTF file as HTML.

Now, you can download Spire.Doc for WPF and then, view the effect of RTF to HTML task as below picture:

RTF to HTML

Sample Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;

namespace wpfrtftohtml
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load RTF file
            Document document = new Document();
            document.LoadFromFile(@"..\wpfrtftohtml.rtf", FileFormat.Rtf);
            //Convert rtf to html
            document.SaveToFile("rtftohtml.html", FileFormat.Html);
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace wpfrtftohtml
    
    Public Class MainWindow
        Inherits Window
        Public Sub New()
            MyBase.New
            InitializeComponent
        End Sub
        
        Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
            'Load RTF file
            Dim document As Document = New Document
            document.LoadFromFile("..\wpfrtftohtml.rtf", FileFormat.Rtf)
            'Convert rtf to html
            document.SaveToFile("rtftohtml.html", FileFormat.Html)
        End Sub
    End Class
End Namespace

A CSV (Comma Separated Values) file is a plain text file that contains data separated by commas. It is widely used to import or export data from one application to another. In some cases, you might need to do conversions between CSV and Excel. In this article, you will learn how to implement this function programmatically in C# and VB.NET using Spire.XLS for .NET library.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS 

Convert Excel to CSV in C# and VB.NET

The following are the steps to convert Excel to CSV:

  • Create an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the desired worksheet by its index using Workbook.Worksheets[index] property.
  • Save the worksheet as CSV using XlsWorksheet.SaveToFile() method. You can choose one of the following overloaded SaveToFile() methods:
    • SaveToFile(string fileName, string separator)
    • SaveToFile(string fileName, string separator, Encoding encoding)
    • SaveToFile(string fileName, string separator, bool retainHiddenData)
  • C#
  • VB.NET
using Spire.Xls;
using System.Text;

namespace ConvertAWorksheetToCsv
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();
            //Load an Excel file
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Save the worksheet as CSV
            sheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8);
        }
    }
}

C#/VB.NET: Convert Excel to CSV and Vice Versa

Convert CSV to Excel in C# and VB.NET

The following are the steps to convert CSV to Excel:

  • Create an instance of Workbook class.
  • Load a CSV file using Workbook.LoadFromFile(string fileName, string separator, int startRow, int startColumn) method.
  • Get the desired worksheet by its index using Workbook.Worksheets[index] property.
  • Access the used range of the worksheet using Worksheet.AllocatedRange property. Then set CellRange.IgnoreErrorOptions property as IgnoreErrorType.NumberAsText to ignore possible errors while saving the numbers in the range as text.
  • Autofit columns and rows using CellRange.AutoFitColumns() and CellRange.AutoFitRows() methods.
  • Save the CSV to Excel using Workbook.SaveToFile(string fileName, ExcelVersion version) method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ConvertCsvToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();

            //Load a CSV file
            workbook.LoadFromFile(@"ExcelToCSV.csv", ",", 1, 1);

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Access the used range in the worksheet
            CellRange usedRange = sheet.AllocatedRange;
            //Ignore errors when saving numbers in the range as text
            usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText;
            //Autofit columns and rows
            usedRange.AutoFitColumns();
            usedRange.AutoFitRows();

            //Save the result file
            workbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013);
        }
    }
}

C#/VB.NET: Convert Excel to CSV and CSV to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 280