|
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using GemBox.Spreadsheet;
namespace AdvancedFeaturesCS
{
class AdvancedFeaturesCS
{
[STAThread]
static void Main(string[] args)
{
// TODO: If using GemBox.Spreadsheet Professional, put your serial key below.
// Otherwise, if you are using GemBox.Spreadsheet Free, comment out the
// following line (Free version doesn't have SetLicense method).
// SpreadsheetInfo.SetLicense("YOUR-SERIAL-KEY-HERE");
ExcelFile excelFile = new ExcelFile();
ExcelWorksheetCollection worksheets = excelFile.Worksheets;
ImagesSample(worksheets.Add("Images"));
CommentsSample(worksheets.Add("Comments"));
PrintViewOptionsSample(worksheets.Add("PrintViewOptions"));
HyperlinkSample(worksheets.Add("Hyperlinks"));
string fileName = "AdvancedFeatures.xlsx";
// Images, Comments and Hyperlinks are only supported in XLSX.
excelFile.SaveXlsx(fileName);
TryToDisplayGeneratedFile(fileName);
}
static void ImagesSample(ExcelWorksheet ws)
{
ws.Cells[0, 0].Value = "Image examples:";
// Small BMP added by using rectangle.
ws.Pictures.Add(@"..\..\SmallImage.bmp", new Rectangle(50, 50, 48, 48));
// Large JPG added by using rectangle.
ws.Pictures.Add(@"..\..\FragonardReader.jpg", new Rectangle(50, 150, 478, 600));
// PNG added by using anchors.
ws.Pictures.Add(@"..\..\Dices.png",
PositioningMode.FreeFloating, new AnchorCell(ws.Columns[8], ws.Rows[16], true),
new AnchorCell(ws.Columns[9], ws.Rows[20], false));
// GIF added by using anchors. Notice that animation is lost in MS Excel.
ws.Pictures.Add(@"..\..\Zahnrad.gif",
PositioningMode.Move, new AnchorCell(ws.Columns[8], ws.Rows[22], 100000, 100000),
new AnchorCell(ws.Columns[9], ws.Rows[24], 50000, 50000));
// WMF added by using anchors.
ws.Pictures.Add(@"..\..\Graphics1.wmf",
PositioningMode.MoveAndSize, new AnchorCell(ws.Columns[10], ws.Rows[10], true),
new AnchorCell(ws.Columns[15], ws.Rows[15], false));
}
static void CommentsSample(ExcelWorksheet ws)
{
ws.Cells[0, 0].Value = "Comment examples:";
ws.Cells[2, 1].Comment.Text = "Empty cell.";
ws.Cells[4, 1].Value = 5;
ws.Cells[4, 1].Comment.Text = "Cell with a number.";
ws.Cells["B7"].Value = "Cell B7";
ws.Cells["B7"].Comment.Text = "Some text. Comment is:\na) multiline,\nb) large, and \nc) visible.";
ws.Cells["B7"].Comment.IsVisible = true;
}
static void PrintViewOptionsSample(ExcelWorksheet ws)
{
ws.Cells["M1"].Value = "This worksheet shows how to set various print related and view related options.";
ws.Cells["M2"].Value = "To see results of print options, go to Print and Page Setup dialogs in MS Excel.";
ws.Cells["M3"].Value = "Notice that print and view options are worksheet based, not workbook based.";
// Print options:
ws.PrintOptions.Portrait = false;
ws.PrintOptions.PaperSize = 8;
ws.PrintOptions.NumberOfCopies = 5;
// View options:
ws.ViewOptions.FirstVisibleColumn = 3;
ws.ViewOptions.ShowColumnsFromRightToLeft = true;
ws.ViewOptions.Zoom = 123;
}
static void HyperlinkSample(ExcelWorksheet ws)
{
ws.Cells["B16"].Value = "GemboxSoftware";
ws.Cells["B16"].Style.Font.UnderlineStyle = UnderlineStyle.Single;
ws.Cells["B16"].Style.Font.Color = Color.Blue;
ws.Cells["B16"].Hyperlink.Location = "http://www.gemboxsoftware.com";
ws.Cells["B16"].Hyperlink.IsExternal = true;
ws.Cells["B20"].Value = "Jump";
ws.Cells["B20"].Hyperlink.ToolTip = "This is tool tip! This hyperlink jumps to A1!";
ws.Cells["B20"].Hyperlink.Location = ws.Name + "!A1";
ws.Cells["A1"].Value = "Jump Here";
}
static void TryToDisplayGeneratedFile(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch (Exception)
{
Console.WriteLine(fileName + " created in application folder.");
}
}
}
}
| |