Knowledgebase (2343)
Children categories
Retrieve a list of the hidden worksheets in a spreadsheet document
2016-01-15 06:50:07 Written by Koohji
class Program
{
static void Main(string[] args)
{
// Revise this path to the location of a file that contains hidden worksheets.
const string DEMOPATH = @"..\..\Documents\Sheets11.xlsx";
List sheets = GetHiddenSheets(DEMOPATH);
foreach (var sheet in sheets)
{
Console.WriteLine(sheet.Name);
}
Console.ReadLine();
}
public static List GetHiddenSheets(string fileName)
{
List returnVal = new List();
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart wbPart = document.WorkbookPart;
var sheets = wbPart.Workbook.Descendants();
// Look for sheets where there is a State attribute defined,
// where the State has a value,
// and where the value is either Hidden or VeryHidden.
var hiddenSheets = sheets.Where((item) => item.State != null &&
item.State.HasValue &&
(item.State.Value == SheetStateValues.Hidden ||
item.State.Value == SheetStateValues.VeryHidden));
returnVal = hiddenSheets.ToList();
}
return returnVal;
}
}
Published in
OpenXML
Retrieve a dictionary of all named ranges in a spreadsheet document
2016-01-15 06:49:26 Written by Koohji
class Program
{
static void Main(string[] args)
{
var result = GetDefinedNames(@"..\..\Documents\Sheet10.xlsx");
foreach (var dn in result)
Console.WriteLine("{0} {1}", dn.Key, dn.Value);
Console.ReadLine();
}
public static Dictionary GetDefinedNames(String fileName)
{
// Given a workbook name, return a dictionary of defined names.
// The pairs include the range name and a string representing the range.
var returnValue = new Dictionary();
// Open the spreadsheet document for read-only access.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
// Retrieve a reference to the workbook part.
var wbPart = document.WorkbookPart;
// Retrieve a reference to the defined names collection.
DefinedNames definedNames = wbPart.Workbook.DefinedNames;
// If there are defined names, add them to the dictionary.
if (definedNames != null)
{
foreach (DefinedName dn in definedNames)
returnValue.Add(dn.Name.Value, dn.Text);
}
}
return returnValue;
}
}
Published in
OpenXML
class Program
{
static void Main(string[] args)
{
string strDoc = @"..\..\Documents\Sheet9.xlsx";
Stream stream = File.Open(strDoc, FileMode.Open);
OpenAndAddToSpreadsheetStream(stream);
stream.Close();
}
public static void OpenAndAddToSpreadsheetStream(Stream stream)
{
// Open a SpreadsheetDocument based on a stream.
SpreadsheetDocument spreadsheetDocument =SpreadsheetDocument.Open(stream, true);
// Add a new worksheet.
WorksheetPart newWorksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
newWorksheetPart.Worksheet.Save();
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild();
string relationshipId = spreadsheetDocument.WorkbookPart.GetIdOfPart(newWorksheetPart);
// Get a unique ID for the new worksheet.
uint sheetId = 1;
if (sheets.Elements().Count() > 0)
{
sheetId = sheets.Elements().Select(s => s.SheetId.Value).Max() + 1;
}
// Give the new worksheet a name.
string sheetName = "Sheet" + sheetId;
// Append the new worksheet and associate it with the workbook.
Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
spreadsheetDocument.WorkbookPart.Workbook.Save();
// Close the document handle.
spreadsheetDocument.Close();
// Caller must close the stream.
}
}
Published in
OpenXML