当Office PIA遭遇C# 4.0

来源:互联网 发布:欧赔数据库下载 编辑:程序博客网 时间:2024/06/05 01:37

上回书说到,最近我在写一个Office PIA的程序。我先是写了一个独立的小工程,完美地实现了对Excel Workbook的各种操作,并且也反复作了测试。然后往我们真实的项目里合并。这里有个伏笔:我们team用的是Visual Studio 2008,而我个人用的是Visual Studio 2010。我没忘记在我的Visual Studio 2010工程里面把.Net Framework的版本设置为3.5。这个地方要跟我们实际的项目保持一致,这一点我还是很注意的。

谁曾想,合并的时候出了问题,编译出现一大堆错误。

比如说这个方法:

        /// <summary>        /// Creates the excel doc.        /// </summary>        /// <param name="fileName">the excel doc filename</param>        public void CreateWorkbook(string fileName)        {            try            {                this.CurrentWorkbook = this.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);                this.LatestWorksheet = this.CurrentWorkbook.Sheets[1] as Excel.Worksheet;                this.CurrentWorksheet = this.LatestWorksheet;                string fullPath = this.GetActualFullPath(fileName);                this.LatestWorksheet.SaveAs(                    fullPath,                    this.currentFileFormat,                    Type.Missing,                    Type.Missing,                    Type.Missing,                    Type.Missing);            }            catch (Exception ex)            {                this.Close();                throw ex;            }        }

这段代码,在Visual Studio 2010里面编译没有任何问题,在Visual Studio 2008里面编译时却出现了这样的错误:

Compile error: No overload for method 'SaveAs' takes '6' arguments ......

修改成这样的代码,才在Visual Studio 2008下编译通过(当然也能够在Visual Studio 2010下编译通过):

        /// <summary>        /// Creates the excel doc.        /// </summary>        /// <param name="fileName">the excel doc filename</param>        public void CreateWorkbook(string fileName)        {            try            {                this.CurrentWorkbook = this.Application.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);                this.LatestWorksheet = this.CurrentWorkbook.Sheets[1] as Excel.Worksheet;                this.CurrentWorksheet = this.LatestWorksheet;                string fullPath = this.GetActualFullPath(fileName);                this.LatestWorksheet.SaveAs(                    fullPath,                    this.currentFileFormat,                    Type.Missing,                    Type.Missing,                    Type.Missing,                    Type.Missing,                    Excel.XlSaveAsAccessMode.xlExclusive,                    Type.Missing,                    Type.Missing,                    Type.Missing);            }            catch (Exception ex)            {                this.Close();                throw ex;            }        }

同样的问题多处出现。比如说Visual Studio 2010下能编译通过的这段代码:

        /// <summary>        /// Create new worksheet and insert it after current worksheet.        /// </summary>        /// <param name="worksheetName">name of the new worksheet</param>        public void AddWorksheetAfter(object worksheetName)        {            this.LatestWorksheet = this.CurrentWorkbook.Worksheets.Add(Type.Missing, worksheetName) as Excel.Worksheet;            this.Save();        }

在Visual Studio 2008下编译遭遇这样的错误:

Compile error: No overload for method 'Add' takes '2' arguments ......

改成下面这样才解决:

        /// <summary>        /// Create new worksheet and insert it after current worksheet.        /// </summary>        /// <param name="worksheetName">name of the new worksheet</param>        public void AddWorksheetAfter(object worksheetName)        {            this.LatestWorksheet = this.CurrentWorkbook.Worksheets.Add(Type.Missing, worksheetName, Type.Missing, Type.Missing) as Excel.Worksheet;            this.Save();        }

这是为什么呢?首先请不要疑神疑鬼,不要惊慌,要相信科学,遇到问题要广泛地借助Google和MSDN这两个好工具。微笑


Visual Studio 2008编译出错的地方,全都是出在Office PIA的Interop COM组件的可选参数之处。在Visual Studio 2010中,尽管我的.Net Framework版本选择的是3.5,但是由于Visual Studio 2010的编辑器本身支持C# 4.0的一些“语法糖”,C# 4.0对于COM组件的互操作性作了增强,支持了可选参数的省略,因此我在编程中省掉了那一大堆Type.Missing。这样,我在Visual Studio 2010下写的代码显得很简洁。当然也正是这些地方造成了在Visual Studio 2008下编译的困扰。


MSDN中相关的链接如下:


  • Named and Optional Arguments (C# Programming Guide) http://msdn.microsoft.com/en-us/library/dd264739.aspx


问题的查找与解决并不复杂。尽管这是由Visual Studio 2010的新特性给我造成的小麻烦,但是遇到这样的小麻烦,心情还是很愉悦的,我不认为这是什么坏事情。