【问题标题】:Creating Workbooks from templates programmatically [closed]以编程方式从模板创建工作簿[关闭]
【发布时间】:2014-12-12 19:42:17
【问题描述】:

我有一个在“Office 2003”中创建的 excel 模板,话虽如此,我需要以编程方式从这个现有模板创建一个新工作簿。另外,如果有任何会导致错误的事情,例如锁定的单元格、隐藏的公式、隐藏的工作表等,请提及。

了解一些可能会有所帮助的事情

  1. 我已经从 Office 2003 升级 -> Office 2010,但是文件格式是 Excel 2007。

  2. 有超过 303 行和 26 列,每一个都包含自己独特的公式(仅在一张纸上),总共可能有 700 多行和 100 列(在添加任何其他工作表之前),每个都包含自己相对独特的公式。##

  3. 某些单元格和工作表已锁定,以防止用户引发错误或用户篡改单元格内公式。

  4. 这个模板将被几个人使用,他们都有自己的电脑。我不能保证这些文件在每台用户的计算机上都位于相同的位置。

软件信息:

Visual Studio 2013 办公室 2010 办公室 2003

【问题讨论】:

  • 我已经阅读了一半以上的 msdn 文档,我已经阅读了 3 - 4 种不同的书籍,问题是它们通常不提及版本兼容性。

标签: c# excel-2007 compatibility excel-2003 excel-interop


【解决方案1】:

找到了解决办法!

在 vs2013 中,他们提供 excel 2007 插件, 在预定义的互操作性中,有一种方法可以从模板创建文件,让我失望的部分是显示的参数部分:

“Type.missing”。 此代码结构的示例如下:

//the #region is simply so that if copied and pasted, the code will make sense, and there will be a label encapsulating the specific code lines. *Note it does not affect the results
#region add workbook from template file
Excel.Application f;
//the following line is written assuming that you would have already made sure that excel was in the Running Objects Table (ROT)
f=(Excel.Application)Marshal.GetActiveObject("Excel.Application");
f.visible=true;
f.Workbooks.Add("C:\\...");
\if there are any questions please comment, I will do my best to explain my own answer
#endregion

【讨论】:

    【解决方案2】:

    您需要查看 EPPlus http://epplus.codeplex.com

    EPPlus 是一个 .net 库,它使用 Open Office Xml 格式 (xlsx) 读写 Excel 2007/2010 文件。

    【讨论】:

    • 它可以从 Office 2003 读取文件吗?
    • 技术上应该是这样。但我还没有测试过。请阅读文档。
    • 好的,谢谢,我只是想知道是不是赶时间。
    • 如果对你有帮助,你可以看看stackoverflow.com/questions/151005/…
    • 谢谢,至于 epplus。我已经对其进行了测试,id 似乎不适用于任何 Office 2003 文件格式,这并不可怕,但也许我应该提到,其他将使用它的人,可能仍在使用 Office 2003 .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多