从.xlt文件打开.xltx或xlsx

我有模板.xlt excel文件,并尝试显示在.xltx或xlsx excel文件的数据。

ExcelApp = new Microsoft.Office.Interop.Excel.Application(); 

我尝试打开xlt扩展名为xltx的文件

 ExcelWorkBook = ExcelApp.Workbooks.Open(fileName,…) 

当我打开文件名为fileName,我得到的错误

来自HRESULT 0x800A03EC的exception

在这条线

 Range r = (Range) ExcelWorkBook.Columns["I", 0]; 

这个问题可能是由CultureInfo引起的。 或者也许有一些旧的格式无效的types库


为了解决这个错误,你可以在执行与Excel有关的代码时将CurrentCulture设置为en-US,并使用这两个函数重新设置为原始值。

 //declare a variable to hold the CurrentCulture System.Globalization.CultureInfo oldCI; //get the old CurrenCulture and set the new, en-US void SetNewCurrentCulture() { oldCI = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); } //reset Current Culture back to the originale void ResetCurrentCulture() { System.Threading.Thread.CurrentThread.CurrentCulture = oldCI; }