【发布时间】:2015-10-01 11:07:03
【问题描述】:
我正在努力将整个工作表从现有的 .XLS 文件复制到我在我的应用程序中创建的新 Excel 文件中。到目前为止,这是我的代码:
Microsoft.Office.Interop.Excel.Workbook tempworkbook = workbooks.Open(
Directory.GetCurrentDirectory() + "\\Template.xlsx", //FileName
0, //UpdateLinks
true, //ReadOnly
5, //Format
Type.Missing, //Password
Type.Missing, //WriteResPassword
true,//IgnoreReadOnlyRecommended
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, //Origin
"\t", //Delimiter
false, //Editable
false, //Notify
0, //Converter
false, //AddToMRU
1, //Local
0 //CorruptLoad
);
Microsoft.Office.Interop.Excel.Worksheet tmp1 = (Microsoft.Office.Interop.Excel.Worksheet)tempworkbook.Sheets["GSM Data"];
string test = tmp1.get_Range("B2", "B2").Value2.ToString(); //test to see if sheet can be accessed - value should be "Database";
tmp1.Copy(Type.Missing, wsGSMData); //copy worksheet into wsGSMData (which is the sheet in the new XLS file)
但是我在最后一行遇到的错误是:
服务器抛出异常。 (来自 HRESULT 的异常:0x80010105 (RPC_E_SERVERFAULT))
基本上,我提取为tmp1 的工作表应该进入名为wsGSMData 的工作表,这是我在新的.XLS 工作簿(称为子工作簿):
Microsoft.Office.Interop.Excel.Worksheet wsGSMData = (Microsoft.Office.Interop.Excel.Worksheet)subsetworkbook.Sheets["Sheet7"];
我做错了什么?
【问题讨论】:
-
你从哪里得到 wsGSMData?是否在同一个 Excel 进程中运行(即是否通过同一个 Excel 应用程序对象获取)?
-
这是我在我创建的新 XLS 工作簿(子集工作簿)中创建的 7 张工作表之一:subsetworkbook.Sheets.Add(Type.Missing, Type.Missing, 7, Type.Missing); Microsoft.Office.Interop.Excel.Worksheet wsGSMData = (Microsoft.Office.Interop.Excel.Worksheet)subsetworkbook.Sheets["Sheet7"];