Monday, 09 December 2013 02:29

Create and Apply Excel Named Range in C#

Named range is always used in formulas when processing data in Excel. Spire.XLS can meet your need here in dealing with named ranges. Spire.XLS can create a named range, edit a named range and use named ranges in formulas.

In this article, we will introduce you how to create and apply named ranges through C# code.

Call the method Add to create a new named range. The parameter NewNamedRange is the name of the named range. Then use the property RefersToRange to set the range of the named range.

[C#]
INamedRange NamedRange = workbook.NameRanges.Add("NewNamedRange");
NamedRange.RefersToRange = sheet.Range["D2:D19"];

Add a formula that uses named range NewNamedRange.

[C#]
sheet.Range["D20"].Formula = "=SUM(NewNamedRange)";

Edit the property RefersToRange to change the range of named range NewNamedRange.

[C#]
INamedRange NamedRange1 = workbook.NameRanges.GetByName("NewNamedRange");
IXLSRange range = NamedRange1.RefersToRange;
range = range.Worksheet.Range["D2:D8"];
NamedRange1.RefersToRange = range;

PS: Since the formula uses NewNamedRange, the change of NewNamedRange affects the value of sheet.Range[“20”].

Here is the full code and effect screenshot:

[C#]
static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"..\..\DataTable.xls");
            Worksheet sheet = workbook.Worksheets[0];

            INamedRange NamedRange = workbook.NameRanges.Add("NewNamedRange");
            NamedRange.RefersToRange = sheet.Range["D2:D19"];

            sheet.Range["D20"].Formula = "=SUM(NewNamedRange)";

            INamedRange NamedRange1 = workbook.NameRanges.GetByName("NewNamedRange");
            IXLSRange range = NamedRange1.RefersToRange;
            range = range.Worksheet.Range["D2:D8"];
            NamedRange1.RefersToRange = range;

            workbook.SaveToFile(@"..\..\result.xlsx", ExcelVersion.Version2007);
            System.Diagnostics.Process.Start(@"..\..\result.xlsx");
        }

excel named range

Thursday, 07 November 2013 07:22

How to Convert Word to Emf in C#

The article will introduce an easy way to convert Word to Emf by a powerful and independent Word .NET component called Spire.Doc, without Microsoft Word installed on the machine. It also offers support for converting Word and HTML to frequently-used image formats like Jpeg, Png, Gif, Bmp and Tiff, etc. Just click here to have a try.

Emf is a file extension for Enhanced MetaFile, used as a graphics language for printer drivers by the Windows operating system. In 1993, a newer version with additional commands 32-bit version of Win32/GDI introduced the Enhanced Metafile (Emf). Microsoft also recommends enhanced-format (Emf) functions to be used instead of rarely being used Windows-format (WMF) functions.

Spire.Doc presents almost the easiest solution to convert Word to Emf through the following 5 lines simple code.

[C#]
// create an instance of Spire.Doc.Document
Document doc = new Document();
// load the file base on a specified file name
doc.LoadFromFile(@"../../Original Word.docx", FileFormat.Docx);
//convert the first page of document to image
System.Drawing.Image image = doc.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile);
// save the document object to Emf file
image.Save(@"../../Convert Word to Image.emf", ImageFormat.Emf);
//close the document
doc.Close();

Check the effect screenshot below:

word to emf c#