I'm struggling to copy an entire worksheet from an existing .XLS file into a new Excel file I'm creating within my application. This is my code so far:
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)
However the error I'm getting on the last line is:
The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
Essentially, the sheet I've extracted as tmp1
should go into the sheet called wsGSMData
, which is a sheet (one of 7 sheets in total) that i've created within the new .XLS workbook (called subsetworkbook):
Microsoft.Office.Interop.Excel.Worksheet wsGSMData = (Microsoft.Office.Interop.Excel.Worksheet)subsetworkbook.Sheets["Sheet7"];
What am I doing wrong?