Hantera dokumentegenskaper

Introduktion

Microsoft Excel ger möjlighet att lägga till egenskaper till kalkylarksfiler. Dessa dokumentegenskaper ger användbar information och är indelade i två kategorier enligt beskrivningen nedan.

  • Systemdefinierade (inbyggda) egenskaper: Inbyggda egenskaper innehåller allmän information om dokumentet som dokumenttitel, författarens namn, dokumentstatistik och så vidare.
  • Användardefinierade (anpassade) egenskaper: Anpassade egenskaper definierade av slutanvändaren i form av namn-värdepar.

Hantera dokumentegenskaper med Microsoft Excel

Microsoft Excel låter dig hantera dokumentegenskaperna för Excel-filerna på ett WYSIWYG-sätt. Följ stegen nedan för att öppnaEgenskaper dialogrutan i Excel 2016.

  1. FrånFil menyn, väljInfo.
Välj infomeny
todo:image_alt_text
  1. Klicka påEgenskaperrubrik och välj “Avancerade egenskaper”.
Klicka på Avancerat val av egenskaper
todo:image_alt_text
  1. Hantera filens dokumentegenskaper.
Egenskapsdialog
todo:image_alt_text
I dialogrutan Egenskaper finns det olika flikar, som Allmänt, Sammanfattning, Statistik, Innehåll och Tull. Varje flik hjälper till att konfigurera olika typer av information relaterad till filen. Fliken Anpassad används för att hantera anpassade egenskaper.

Arbeta med dokumentegenskaper med Aspose.Cells

Utvecklare kan hantera dokumentegenskaperna dynamiskt med hjälp av Aspose.Cells API:er. Den här funktionen hjälper utvecklarna att lagra användbar information tillsammans med filen, som när filen togs emot, bearbetades, tidsstämplad och så vidare.

Åtkomst till dokumentegenskaper

Aspose.Cells API:er stöder båda typerna av dokumentegenskaper, inbyggda och anpassade. Aspose.Cells'Arbetsbok klass representerar en Excel-fil och, liksom en Excel-fil, denArbetsbok klass kan innehålla flera kalkylblad, var och en representerad avArbetsblad klass medan samlingen av kalkylblad representeras avArbetsbladssamlingklass.

AnvändArbetsbladssamlingför att komma åt filens dokumentegenskaper enligt beskrivningen nedan.

BådeWorksheetCollection.BuiltInDocumentProperties ochWorksheetCollection.CustomDocumentProperties returnera instansen avAspose.Cells.Properties.DocumentPropertyCollection. Denna samling innehållerAspose.Cells.Properties.DocumentPropertyobjekt, som vart och ett representerar en enskild inbyggd eller anpassad dokumentegenskap.

Det är upp till applikationskravet hur man kommer åt en fastighet, det vill säga; genom att använda index eller namn på fastigheten frånDocumentPropertyCollectionsom visas i exemplet nedan.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Accessing a custom document property by using the property name
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["ContentTypeId"];
Console.WriteLine(customProperty1.Name + " " + customProperty1.Value);
// Accessing the same custom document property by using the property index
Aspose.Cells.Properties.DocumentProperty customProperty2 = customProperties[0];
Console.WriteLine(customProperty2.Name + " " + customProperty2.Value);

DeAspose.Cells.Properties.DocumentPropertyklass tillåter att hämta namn, värde och typ av dokumentegenskapen:

Medlemsnamn Beskrivning ToXXX-metoden
Boolean Egenskapsdatatypen är boolesk ToBool
Datum Egenskapsdatatypen är DateTime. Observera att endast Microsoft Excel-butiker
datumdelen, ingen tid kan lagras i en anpassad egenskap av denna typ
ToDateTime
Flyta Egenskapsdatatypen är dubbel Att dubblera
siffra Egenskapsdatatypen är Int32 ToInt
Sträng Egenskapens datatyp är String Att stränga
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Accessing a custom document property
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties[0];
// Storing the value of the document property as an object
object objectValue = customProperty1.Value;
// Accessing a custom document property
Aspose.Cells.Properties.DocumentProperty customProperty2 = customProperties[1];
// Checking the type of the document property and then storing the value of the
// document property according to that type
if (customProperty2.Type == Aspose.Cells.Properties.PropertyType.String)
{
string value = customProperty2.Value.ToString();
Console.WriteLine(customProperty2.Name + " : " + value);
}

Lägga till eller ta bort anpassade dokumentegenskaper

Som vi har beskrivit tidigare i början av detta ämne kan utvecklare inte lägga till eller ta bort inbyggda egenskaper eftersom dessa egenskaper är systemdefinierade men det är möjligt att lägga till eller ta bort anpassade egenskaper eftersom dessa är användardefinierade.

Lägga till anpassade egenskaper

Aspose.Cells API:er har avslöjatLägg till metod förCustomDocumentPropertyCollection klass för att lägga till anpassade egenskaper till samlingen. DeLägg till metod lägger till egenskapen i Excel-filen och returnerar en referens för den nya dokumentegenskapen som enAspose.Cells.Properties.DocumentPropertyobjekt.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Adding a custom document property to the Excel file
Aspose.Cells.Properties.DocumentProperty publisher = customProperties.Add("Publisher", "Aspose");
// Saving resultant spreadsheet
workbook.Save(dataDir + "out_sample-document-properties.xlsx");

Konfigurera “Länk till innehåll” anpassad egendom

För att skapa en anpassad egenskap kopplad till innehållet i ett givet intervall, anropaCustomDocumentPropertyCollection.AddLinkToContent metod och pass egenskapens namn och källa. Du kan kontrollera om en egenskap är konfigurerad som länkad till innehåll med hjälp avDocumentProperty.IsLinkedToContent fast egendom. Dessutom är det också möjligt att få källomfånget med hjälp avKälla egendom avDocumentPropertyklass.

Vi använder en enkel mall Microsoft Excel-fil i exemplet. Arbetsboken har ett definierat namngivet intervall märktMyRange som hänvisar till ett cellvärde.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate an object of Workbook
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Add link to content.
customProperties.AddLinkToContent("Owner", "MyRange");
// Accessing the custom document property by using the property name
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["Owner"];
// Check whether the property is lined to content
bool islinkedtocontent = customProperty1.IsLinkedToContent;
// Get the source for the property
string source = customProperty1.Source;
// Save the file
workbook.Save(dataDir + "out_sample-document-properties.xlsx");

Ta bort anpassade egenskaper

För att ta bort anpassade egenskaper med Aspose.Cells, ringDocumentPropertyCollection.Removemetod och skicka namnet på dokumentegenskapen som ska tas bort.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Removing a custom document property
customProperties.Remove("Publisher");
// Save the file
workbook.Save(dataDir + "out_sample-document-properties.xlsx");

Förhandsämnen