Как установить или получить свойство тега документа excel грамматически через C#
using DSOFile; using System; using System.Reflection; using System.Windows.Forms; /// <summary> /// (c) GinkoSolutions.com /// This sample class enables you to set meta file information within physical files. /// This is similar to EXIF information for picture files. /// DSOFile works for every file type, not just office files. /// /// NOTE /// DsoFile is an unmnaged 32bit dll. We need to compile in x86 mode or we get 'class not registered exception' /// There is a third party 64bit version available online, or recompile the C++ source manually. /// </summary> public static class DSOFileExample { /// <summary> /// A property name that this sample code uses to store tag information. /// </summary> private static string FILE_PROPERTY = "CustomFileTag"; /// <summary> /// Gets value stored in a custom tag /// </summary> /// <param name="filename">Path to the file</param> /// <returns>Our custom value stored in the custom file tag</returns> public static string GetCustomPropertyValue(string filename) { string comment = string.Empty; //OleDocumentProperties file = new DSOFile.OleDocumentProperties(); try { file = (OleDocumentProperties)Activator.CreateInstance(typeof(DSOFile.OleDocumentProperties)); // Open file file.Open(filename, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault); comment = GetTagField(file); } catch (Exception ex) { MessageBox.Show("Error in DSOFileExample : GetCustomPropertyValue " + ex.Message + " Stack trace " + ex.StackTrace); // Handle errors here } finally { if (file != null) file.Close(); // Clean up } return comment; } //public static void CreateDsoObj(string fileName) //{ // var oOleDocProp = CreateObject("");// new DSOFile.OleDocumentProperties(); // oOleDocProp.Open(fileName); //} static OleDocumentProperties file; static object Create() { // dynamically load assembly from file Test.dll Assembly testAssembly = Assembly.LoadFile(@"I:\ExcelHiddenContent\dsofile_x64.dll"); // get type of class Calculator from just loaded assembly Type dsoType = testAssembly.GetType("DSOFile.OleDocumentProperties"); // create instance of class Calculator return Activator.CreateInstance(dsoType); } /// <summary> /// Sets value stored in a files custom tag /// </summary> /// <param name="filename">Path to the file</param> /// <param name="value">Value to store in the custom file tag</param> public static void SetCustomPropertyValue(string filename, string value) { //OleDocumentProperties file = new DSOFile.OleDocumentProperties(); MessageBox.Show("Start Set tagging"); try { file = new DSOFile.OleDocumentProperties(); //file = (OleDocumentProperties)Activator.CreateInstance(typeof(DSOFile.OleDocumentPropertiesClass)); file.Open(filename, false, DSOFile.dsoFileOpenOptions.dsoOptionDefault); SetTagField(file, value); } catch (Exception ex) { MessageBox.Show("Error in DSOFileExample : SetCustomPropertyValue " + ex.Message + " Stack trace " + ex.StackTrace); // Handle errors here } finally // Always called, even when throwing in Exception block { if (file != null) file.Close(); // Clean up } } /// <summary> /// Gets the value of the file tag property /// </summary> /// <param name="file">Ole Document File</param> /// <returns>Contents of the file tag property. Can be null or empty.</returns> private static string GetTagField(DSOFile.OleDocumentProperties file) { string result = string.Empty; foreach (DSOFile.CustomProperty property in file.CustomProperties) { if (property.Name == FILE_PROPERTY) // Check property exists { result = property.get_Value(); break; } } return result; } /// <summary> /// Sets the value of the file tag property /// </summary> /// <param name="file">Ole Document File</param> /// <param name="value">Value to set as the property value</param> /// <param name="saveDocument">Saves document if set to true</param> /// <param name="closeDocument">Closes the document if set to true</param> private static void SetTagField(DSOFile.OleDocumentProperties file, string value, bool saveDocument = true, bool closeDocument = true) { bool found = false; foreach (DSOFile.CustomProperty property in file.CustomProperties) { if (property.Name == FILE_PROPERTY) // Check property exists { property.set_Value(value); found = true; break; } } if (!found) file.CustomProperties.Add(FILE_PROPERTY, value); if (saveDocument) file.Save(); if (closeDocument) file.Close(); } }
Что я уже пробовал:
Я уже пробовал с DSOFile.
Но с 64 битами это не работает.
Нужен фрагмент кода C#.
file = (OleDocumentProperties)Activator.CreateInstance(typeof(DSOFile.OleDocumentProperties));часть бросает исключение
lmoelleb
Это очень много кода. Где вы нашли подозрительные части при использовании отладчика?