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?
Probably you've got an issue with Access Mode for your Excel file, which causes (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)).
Try to check settings in Excel menu: Review > Share Workbook > Allow changes by more than one user... - you should try to deselect this check box.
Then go back to your code and check any lines, where you could possibly change this settings, especially SaveAs function. If you see xlShared, replace it with xlExclusive (see example below).
I've had the same problem while trying to add charts to file. No googled solution can help in my case, so I've got to figure out, what's going wrong, by myself. After trying to add charts into a fresh new file by different methods, I realise that aforementioned exception in this line:
Microsoft::Office::Interop::Excel::ChartObject ^ExcelChartObj = ExcelChartObjs->Add(100, 100, 300, 250); //create chart object (left, top, width, height)
was actually caused by the fact, that original file had "Allow changes by more than one user" checkbox selected from the start, and moreover, my own code also re-saved it as xlShared - twice! (Nope, double saving is not an error, it is how it should be in my case, cause my program solves a huge task, and I need to input data into the same file in two different steps during it)
Here's the working code (for C++/CLI Windows Forms), which finally made (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)) to disappear in my case:
ExcelApp->ActiveWorkbook->SaveAs(ExcFileName, Microsoft::Office::Interop::Excel::XlFileFormat::xlOpenXMLWorkbook, Type::Missing, Type::Missing,
Type::Missing, Type::Missing, Microsoft::Office::Interop::Excel::XlSaveAsAccessMode::xlExclusive, Type::Missing, Type::Missing, Type::Missing,
Type::Missing, Type::Missing); //save Excel book (access mode - xlExclusive, not xlShared!)
I hope it will help someone out there too! :)
User contributions licensed under CC BY-SA 3.0