c# - 如何使用 C# 将自定义属性添加到 Excel 文档?
问题描述
我正在尝试向 excel 文档添加属性(很少有属性会使文档保密)。我尝试了在互联网上找到的几种方法,但要么出现异常,要么什么也没发生。有人能告诉我我在哪里犯错吗?
- 方式:在这里我得到 System.Reflection.TargetParameterCountException。
class Program
{
static Application excelApp;
static Workbook excelWorkBook;
static Worksheet excelWorkSheet;
string name1 = "myprop1";
string val1 = "myvalue1";
string name2 = "myprop2";
string val2 = "myvalue2";
string name3 = "longid";
int val3 = 3;
string name4 = "id";
int val4 = 2;
excelWorkBook.CustomDocumentProperties(name1,val1);
excelWorkBook.CustomDocumentProperties(name2, val2);
excelWorkBook.CustomDocumentProperties(name3, val3);
excelWorkBook.CustomDocumentProperties(name4, val4);
}
- 方式:这里什么都没有发生。属性没有改变,也不例外。
excelWorkSheet.CustomProperties.Add("myprop1", "myvalue1");
excelWorkSheet.CustomProperties.Add("myprop2", "myvalue2");
excelWorkSheet.CustomProperties.Add("myprop3", 3);
excelWorkSheet.CustomProperties.Add("myprop4", 2);
- 尝试给我: System.ArgumentException:Value 不在预期范围内。
excelWorkBook.CustomDocumentProperties["myprop1"].Text = "myvalue1";
excelWorkBook.CustomDocumentProperties["myprop2"].Text = "myvalue2";
excelWorkBook.CustomDocumentProperties["myprop3"].Value = 3;
excelWorkBook.CustomDocumentProperties["myprop4"].Value = 2;
解决方案
对于 c# ,它对我有用,
Microsoft.Office.Core.DocumentProperties prps; prps = (Office.DocumentProperties)this.CustomDocumentProperties; prps.Add("事务类型", false, Microsoft.Office.Core .MsoDocProperties.msoPropertyTypeString,“余额”,缺失);
这里“交易类型”是关键,“余额”是价值
推荐阅读
- javascript - fabricjs从json反序列化单个属性
- python-3.x - 使用 Python3 的日期时间根据服务器响应的格式格式化日期时间
- c# - 如何在 CosmosDBTrigger(或任何其他触发器)属性中配置非字符串值
- c++ - 具有可变数量参数的构造函数
- c++-winrt - 在 uwp 中以编程方式设置系统时间
- python - 如何获取某个组的 Kafka 主题中未读消息的计数
- c++ - 为什么在使用 cv::Umat 时调用 ORB_Impl::detectAndCompute 会阻塞
- php - 如何使用 PHP 从数据库的列中回显一个单词的总数?
- python-poetry - 使用 Poetry 安装 dbt(数据构建工具)时出错
- python - pyinstaller kivy 错误无法获取任何图像提供程序,中止