为已安装的 Excel 选择正确的用途
我有一个场景,客户可以安装 Excel 2007 或安装 Excel 2010,我需要能够导出和导入他们创建的 Excel 文件,OLEDB 不是一个选项,所以我将使用 Interop 服务。
问题是,当我为 2010 年编写代码时,它在 2007 年不起作用,反之亦然,我怎样才能实现这一目标?
我可以以某种方式导入两个引用并根据安装的版本定义 Excel 吗?
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlRange;
object misValue = System.Reflection.Missing.Value;
pbarExcelGenerate.Visible = true;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Material Results";
pbarExcelGenerate.Value = 5;
xlRange = xlWorkSheet.get_Range("a1", "b1");
xlRange.Merge(false);
xlRange.Value = "Material Name";
xlRange.HorizontalAlignment = 3;
xlRange.Font.Bold = true;
xlRange.Font.Size = 16;
I have a scenario where a client could have either Excel 2007 installed or Excel 2010 installed, I need to be able to export and import excel files that they create, OLEDB isn't an option so I was going to use Interop services.
Question is when I coded for 2010 it didn't work for 2007 and vice versa how can I achieve this?
Can I import both references somehow and define Excel depending on the version installed?
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlRange;
object misValue = System.Reflection.Missing.Value;
pbarExcelGenerate.Visible = true;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Material Results";
pbarExcelGenerate.Value = 5;
xlRange = xlWorkSheet.get_Range("a1", "b1");
xlRange.Merge(false);
xlRange.Value = "Material Name";
xlRange.HorizontalAlignment = 3;
xlRange.Font.Bold = true;
xlRange.Font.Size = 16;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你不能...您可以同时使用较低版本...或者使用根本不需要安装 Excel 的第 3 方库(免费或商业),或者您可以使用原始 COM 来访问 Excel 会不太舒服,但允许这样的事情。
编辑 - 根据评论(感谢 @Rup):
您可以在 http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=23656 应该附带2007 年的 Interop 库
no you can't... you can either use the lower version for both... or use a 3rd-party library (free or commercial) which doesn't need Excel to be installed at all or you can use raw COM to access Excel which would be much less comfortable but allows for such things.
EDIT - as per comments (thanks to @Rup):
You can download the older version of VSTO at http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=23656 which should come with the Interop library for 2007