How To Use SpreadsheetGear
Search How-To's:
(You can search for multiple words and place phrases in double-quotes) |


(You can search for multiple words and place phrases in double-quotes) | ||
public static IWorkbook OpenWorkbook(System.IO.Stream stream)
{
// Read a workbook from a stream and return it.
return SpreadsheetGear.Factory.GetWorkbookSet()
.Workbooks.OpenFromStream(stream);
}
public static void SaveWorkbook(System.IO.Stream stream)
{
// Create a workbook and write it to the stream.
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
workbook.SaveToStream(
stream, SpreadsheetGear.FileFormat.Excel8);
}
// Create a workbook and get the first worksheet.
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
// Add a hyperlink to cell A1.
worksheet.Hyperlinks.Add(worksheet.Cells["A1"],
@"https://www.spreadsheetgear.com",
null, "My Screen Tip", "My Hyperlink Text");
// Create a workbook.
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
// Get the first default worksheet, name it and add a formula.
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "MyFirstSheet";
worksheet.Cells["A1"].Value = 123.456;
// Add a new 2nd worksheet, name it and add a formula.
worksheet = workbook.Worksheets.Add();
worksheet.Name = "MySecondSheet";
worksheet.Cells["A1"].Formula = "=MyFirstSheet!A1 * 2";
// Create a workbook.
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
// Get the first worksheet and name it
IWorksheet mySheet = workbook.Worksheets[0];
mySheet.Name = "MySheet";
// Create a defined name which references A1.
INames names = workbook.Names;
names.Add("MyName", "=MySheet!$A$1");
// Get the IRange for the defined name.
IRange cell = names["MyName"].RefersToRange;
// Create a workbook and get the first worksheet
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
// Get an IRange and set up random values
IRange rangeValues = worksheet.Range["A1:A10"];
rangeValues.Formula = "=RAND() * 10000";
// Get an IRange and add a formula to sum the values
IRange rangeFormula = worksheet.Range["A11"];
rangeFormula.Formula = "=SUM(" + rangeValues.Address + ")";
// Output the calculated value
System.Console.WriteLine("Sum = " + rangeFormula.Text);
// Create a workbook and get the first worksheet
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
// Set the bottom border of a range of cells
IRange range = worksheet.Cells["A1:D1"];
IBorder border = range.Borders[SpreadsheetGear.BordersIndex.EdgeBottom];
border.LineStyle = SpreadsheetGear.LineStyle.Continous;
border.Color = SpreadsheetGear.Colors.Blue;
border.Weight = SpreadsheetGear.BorderWeight.Thick;
// Create a workbook and get the first worksheet PageSetup
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
IPageSetup pageSetup = worksheet.PageSetup;
// Set the Print Area
pageSetup.PrintArea = "Sheet1!$B$2:$F$20";
// Set the Print Titles
pageSetup.PrintTitleRows = "Sheet1!$1:$1";
pageSetup.PrintTitleColumns = "Sheet1!$A:$A";
// Create a workbook and get the first worksheet
IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets["Sheet1"];
// Create a 10 row by 2 column array of values
object[,] values = new object[10, 2];
for (int i = 0; i < 10; i++)
{
values[i, 0] = "Row=" + i + " Col=0";
values[i, 1] = "Row=" + i + " Col=1";
}
// Set the values in the worksheet
// Notice the range "A1:B10" has to match the size of the array
worksheet.Cells["A1:B10"].Value = values;
// Get the values from the worksheet
object[,] retVals = (object[,])worksheet.Cells["A1:B10"].Value;
Copyright © 2003-2021 SpreadsheetGear LLC. All Rights Reserved.
SpreadsheetGear® is a registered trademark and Spreadsheet Gear is a trademark of SpreadsheetGear LLC.
Microsoft, Microsoft Excel, Visual Studio and the Visual Studio logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.
![]() ![]() |