I am trying to insert an existing sheet from Excel into my current worksheet. But, instead to inserting into my current worksheet, it creates new worksheet. What am I missing in my code?
I am using this:
Workbook wkActive = Globals.ThisAddIn.Application.ActiveWorkbook;
objBook = Globals.ThisAddIn.Application.Workbooks.Open(IdsTemplatePath, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);
wkActive.Sheets.Add(objBook.Sheets[1], Type.Missing, Type.Missing, Type.Missing);
wkActive.Save();
wkActive.Close();
From an MVP:
I believe the Worksheet.Copy method is what you want. Assuming that you are using VSTO, the following code demonstrates one way to copy a worksheet from a non-VSTO workbook into the workbook in your VSTO customization. This particular example copies the first worksheet in a non-VSTO workbook named "MyWorkbook.xls" into a VSTO workbook, after "Sheet3".
Excel.Workbook workbook = Globals.ThisWorkbook.Application.Workbooks.Open(#"C:\MyWorkbook.xls", missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
Excel.Worksheet worksheet = workbook.Sheets[1] as Excel.Worksheet;
worksheet.Copy(missing, Globals.Sheet3.InnerObject);
The code first opens a non-VSTO workbook named "MyWorkbook.xls". The code then copies the first worksheet of this workbook into the VSTO workbook by calling the Copy method of the non-VSTO worksheet, and passing in the VSTO worksheet that you want to copy the non-VSTO worksheet after.
The only somewhat tricky VSTO-specific part of this code is the fact that it passes in the InnerObject property instead of Globals.Sheet3 into the Copy method. This is because Copy expects a Microsoft.Office.Interop.Excel.Worksheet, and the VSTO worksheet (that is, Globals.Sheet3) is a Microsoft.Office.Tools.Excel.Worksheet. The InnerObject property exposes the Microsoft.Office.Interop.Excel.Worksheet that underlies the Microsoft.Office.Tools.Excel.Worksheet.
Related
I want to create an Excel file, at specific location and specific name in c#.
Can we use File.Create("Filename.xlsx") for this?
This creates the file, but when we try to open it; it says problem in extension.
I have tried another way, using excelApp. But that way, it creates Excel file default at "~Documents\Sheet1.xlsx". So i can't specify location and filename in that case.
Excel.Application excelApp = new Excel.Application();
Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(1);
The question is similar to - Specify Path and filename while creating excel
Since i am unable to comment, i have to ask again.
I have tried the answer provided in link.
My code:
Excel.Application excelApp = new Excel.Application();
object missing = System.Reflection.Missing.Value;
Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(missing);
//....
Fill excelWorkbook after creating worksheets
....//
excelWorkBook.Save();
Now, as per suggestion, instead of Save, i have used SaveAs.
excelWorkBook.SaveAs(#"E:\\MyExcelBook\\abcd.xlsx",
Excel.XlFileFormat.xlOpenXMLWorkbook, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
It gives me error:
*The file could not be accessed. Try one of the following:\n\n• Make sure the specified folder exists. \n• Make sure the folder that contains the file is not read-only.\n• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or \n• Make sure the file/path name doesn't contain more than 218 characters.
Please tell me where am i doing wrong?
Just because you save an "empty" file with an extension of .xlsx doesn't mean excel is able to read such a file. You can use OpenXML, ClosedXML (my preferred), interop (yucky) and I'm sure many other methods of creating an excel document
File.Create(...) is not much different than right clicking in file explorer, choosing new text document, and renaming it from "New Text Document.txt" to "New Text Document.xlsx". Yeah that extension is excel, but excel can't read an empty file that states it's an excel file.
You can use any of the libraries available to you to create excel files:
ClosedXML
OpenXML
Interop
etc. Most "Save" methods or "SaveAs" methods within these libraries would (probably) create the file in the working directory by default, but you can specify a directory to create them in as well.
Just as the answer in the linked question shows:
workbook.SaveAs(#"c:\documents\book1.xlsx",
Excel.XlFileFormat.xlOpenXMLWorkbook, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
When I try to open an Excel document I have this error
System.Runtime.InteropServices.COMException (0x800706BE): The remote
procedure call failed.
I built an application for my company, it works fine on every computer but on my boss computer it stopped working when he the app has to open an Excel document.
I have tried to change the Permissions in Component Services but it didn't fix it.
I am working on Windows 7.
Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
object missing = Type.Missing;
Excel.Range xlRange;
Excel.Sheets xlSheets = null;
Excel.Worksheet xlSheet = null;
Excel.Workbook xlWorkbook = null;
xlWorkbook = excelApp.Workbooks.Open(filePath, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing);
In my application, I created excel workbooks in microsoft word document using this code :
wordApp.Selection.InlineShapes.AddOLEObject("MSGraph.Chart.8", excellApp.ActiveWorkbook.Name, false, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Then I need to get that excel workbooks from word document, how can i do that?
I also tried:
Word.InlineShapes shapes = wordApp.Selection.InlineShapes;
However, shapescount equals to zero.
Word.InlineShapes shapes = wordApp.ActiveDocument.InlineShapes;
should work
using Excel = Microsoft.Office.Interop.Excel;
I need to save a excel file (.xls) from my datagridview. All works fine, writing data, changing cell format etc. But problem appears while saving the doc.
string format = "yyyy_MM_dd_HH-mm";
string pathtoexport = #"C:\DM\SMS\Przychodzace\Znalezione_SMS_" + DateTime.Now.ToString(format) + ".xls";
oWB.SaveAs(pathtoexport, Excel.XlFileFormat.xlWorkbookNormal, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
}
Well some days ago it was working, but now it can only say
HRESULT: 0x800A03EC"}
I need .xls to import it to SQL. Any advices?
Before the problem was at Excel.XlFileFormat.xlWorkbookDefault, when Iv changed it to Normal it was doing well. Now it doesnt work. LOst much time to handle it on my own with google, but I failed.
oWB.SaveAs(MyFile + #"C:\SMS\XMLCopy.xls",
Excel.XlFileFormat.xlWorkbookNormal, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
string format = "yyyy_MM_dd_HH-mm";
string pathtoexport = "C:\\DM\\SMS\\Przychodzace\\Znalezione_SMS_" + DateTime.Now.ToString(format) + ".xls";
oWB.SaveAs(pathtoexport, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Now it works. Thanks for help.
Try with Excel.XlFileFormat.xlWorkbookDefault
I can't see anything obviously wrong with your code. Is it possible that there is some problem with accessing that path? If you try to save a file manually to that exact path (assuming that you're logged in under the same account as the interop process runs under), does it work?
Otherwise, are you using some kind of Excel template file or something that could have become corrupted?
If you/your customer can afford, there is Aspose.Cells which is great for generating Excel documents from scratch, without the need for Office being installed on a machine.
On the other hand, the solution costs money.
Looks to me like an Interop version conflict. Are you experiencing the problem on the same machine as it used to work on or have you moved to another? If you're on the same try to back track and figure out any changes or updates. If you're on another then verify that you have the correct version installed there.
This might sound stupid but I've encountered all sorts of wierdness when moving from dev -> test -> prod due to inconsistencies between the environments.
If you haven't already, try removing the reference from the project and reapplying it.
It could also be due to permissions (or lack there of). Have you tried creating a plain text file from the same piece of code?
I'm trying to open an excel workbook and trying to get a worksheet in it. Excelapp.workbooks.Open line is throwing an Exception as
System.Runtime.InteropServices.COMException from HRESULT: 0x800A03EC at Microsoft.Office.Interop.Excel.Workbooks.Open
Here is my code:
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(strWBPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, true);
StrWbPath is my Excel location. I'm refering to 2007 excel and added Microsoft.office.interop.excel of version 12.0.0.0.
(Sorry for answering this old question, but it's google result #1 for this problem and the correct answer is missing).
The error occurs because excel thinks the Workbook has been corrupted. When opening the Excel file, the last parameter tells excel how to handle this situation. Pass Microsoft.Office.Interop.Excel.XlCorruptLoad.xlExtractData to instruct it to retrieve the data (This will open a popup telling the user excel tried to extract the data if the file is corrupted).
However, I noticed this problem can also be caused if the workbook you are trying to open has a different locale than your excel (was saved on a machine using another language setting) OR your system not having the en-us locale set.
While this is extremely stupid, it's easy to overcome. For me the solution was to just set the current locale to en-us before opening the file:
static System.Globalization.CultureInfo oldCI;
oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlExtractData);
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
credit goes to this page
What if you try System.Reflection.Missing.Value instead of Type.Missing?
Regards,
M.
Which class are you using?
Microsoft.Office.Interop.Excel.ApplicationClass
if this then try changing it to
Microsoft.Office.Interop.Excel.Application
In case it helps someone else, I lost tons of time on this one. I was passing Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile for the CorruptedLoad parameter for all files, thinking it meant "open the file, and if it's corrupt, attempt a repair". This worked for most files.
However, for some files, I was still getting the exception (Exception from HRESULT: 0x800A03EC). Yet if you open the file in Excel manually, it is not corrupt.
It turned out that for those files, you must not supply the CorruptedLoad parameter. Then it opens successfully. So in the end I adjusted my code to first try without any CorruptedLoad parameter, and only if that returns the exception, then try passing xlRepairFile for that parameter. I also adjusted my understanding of that parameter to mean "open and repair this file regardless of whether it's corrupt".