Excel 2013 does not display default sheet after adding in C# - c#

In Excel 2013 I can't add a Worksheet after creating the workbook. My C#-Program did this, but if I set the Application on Visible = true, there is no Worksheet in it. My Program can rename Sheets, copy Sheets, but can't Show me them.
After all is done in my program, it need to be saved, but it doesnt work, because there is nothing displayed what can be saved!
This is my code:
Excel.Application appversion = new Excel.Application();
appversion.Visible = true;
Excel.Workbook dataBook = excelApp.Workbooks.Add();
dataSheet = dataBook.Worksheets[1]; // <- this sheet is not visible
dataSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible; // does not work
dataSheet.Name = Seiten[0]; // but my program continues
After adding the workbook Excel starts with this:
workbook without sheet
But where is the default sheet? Without that, I can't save anything. In Excel 2010 all Sheets I need appear and I can see how my program works.

Related

automatically enabling macros in excel c#

I am accessing data from an embedded excel sheet through c#, but in order for the script to read the excel sheet I must manually press "enable macros" every time. How can I write a script such that it automatically enables macros every time? I am opening various excels so cannot change the settings on the excel sheet itself.
I saw something regarding the use of VBA but am unsure what to do? Would using this reference (using VBA = Microsoft.Vbe.Interop) be helpful?
The code below simply opens the excel, but I still have to click enable macros.
using Excel = Microsoft.Office.Interop.Excel;
using Word = Microsoft.Office.Interop.Word;
wordApp = new Word.Application();
xlApp = new Excel.Application();
wordDoc = wordApp.Documents.Open(#"Y:DocumentName");
Word.InlineShapes ils = wordDoc.InlineShapes;
ils[1].OLEFormat.Activate();
xlWorkBook = ils[1].OLEFormat.Object;
Have you tried setting the DisplayAlerts property to false?
xlApp.DisplayAlerts = false;

Using the COM for opening Excel, can't select a particular sheet

I am using the COM to work with Excel 2007. When I use the following code, it opens to the first sheet by default.
Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx");
I need to select different sheets. I tryed using the following code from the DDTdriver code.
Excel = Sys.OleObject("Excel.Application");
Delay (3000); // Wait until Excel starts
Excel.Visible = true;
Excel.Workbooks.Open("G:\\Documentation\\CalCit Excel Files\\2004 Test Data v3 FINAL_new.xlsx", "sheet2", true);
But this does not work. I have tryed many other configs and still get nothing.
For Excel version 2013 or later :
According to learn.microsoft.com
this can be done with Sheets.Select method :
((Excel.Worksheet)this.Application.ActiveWorkbook.Sheets[1]).Select();
which in the code the selected sheet is 1
another method for older versions of Excel :
Excel.Worksheet sheet = (Excel.Worksheet)this.Sheets["Sheet2"];
sheet.Select(Type.Missing);
where Sheet2 is the name of the sheet

use c# to open workbook with ribbon

I am trying to write a wrapper that will open up an excel doc with macros, pass the password through, enable the macros, enable editing, and save it as a new excel sheet so that the end user can just start working with the template without all the hassle.
here is my code:
using Microsoft.Office.Interop.Excel;
string path = #"\\\\home\\trevor$\\My Documents\\New\\_Blank_1cert.xlsm";
string savepath = #"\\\\home\\trevor$\\My Documents\\New\\"+txtName.Text.ToString()+".xlsm";
Excel.Application templatem = new Excel.Application();
Excel.Workbook templatemwb;
templatemwb = templatem.Workbooks.Open(Filename: path, Password: "news", ReadOnly: false);
Excel.Worksheet worksheet = templatemwb.Sheets["Begin Entry"];
worksheet.Activate();
templatemwb.SaveAs(Filename: savepath, FileFormat: Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled);
Everything works as expected except that there is no ribbon at the top of the excel window. I need the ribbon there; so how do I show it?
Set the Visible property on your Application object, templatem.

Open Excel workbook in background using interop

I'm wanting to import data from an Excel workbook without actually displaying the open workbook.
I could have sworn I had used the following code on a previous project and it had worked:
var excelApp = new Excel.Application { Visible = false };
var workbook = excelApp.Workbooks.Open(filePath);
Unfortunately when the workbook opens it is displayed to the user which is unnecessary for this application.
I'm using Microsoft Excel 15.0 Object Library on this project when previously I think it was version 12 or 13. Maybe this is the problem, or is my memory fading and the code is incorrect?
I know this is old but just in case anybody still needs this answer...
excelApp.Visible = false;
(excelApp being the name of the variable used for the excel application)

Excel Interop's set_value method damages values

I found a problem while using the set_value method. I wrote the simplest possible code to exclude my mistakes.
When I read some cells and then write them back to sheet using set_value while:
any filters are active in xlsx file,
2 or more columns are get/set,
some of the cells are corrupted (they are empty or contain junk like #N/D!). When I disable filtering in worksheet or in program before using set_value, everything works fine. I can't find any info about this specific problem in google, am I doing something really stupid?
Excel.Workbooks workbooks;
Excel.Workbook workbook = null;
Excel.Worksheet sheet = null;
Excel.Application excelApplication;
excelApplication = new Excel.Application();
excelApplication.Visible = false;
workbooks = excelApplication.Workbooks;
workbook = workbooks.OpenXML(file);
sheet = workbook.Sheets[1];
Excel.Range excelRangeR = sheet.UsedRange;
object[,] valueArrayR = (object[,])excelRangeR.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
//if uncomment line below (or manualy disable filters in XLSX file), set_value works fine
//sheet.AutoFilterMode = false;
excelRangeR.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, valueArrayR);
workbook.Save();
workbook.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelRangeR);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
excelApplication.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApplication);
You can leave the filters on, but if there is an active filter on the sheet, that has to be removed before setting the value of the cells. I think what is happening is that when you set the object[,] variable, it's populating the array with all the data in the range, including the rows that are not visible, but when you set the value of the range to the object[,] array, it only overwrites the visible rows. It's either that or vice versa.
Sorry if this code is incorrect. I'm just guessing at the C# Interop syntax. I don't use it all that often.
if (sheet.get_FilterMode()) {
sheet.ShowAllData();
}
Unfortunately there isn't an easy way to save the current value of the worksheet filters, so it would be difficult if you need to be able to restore the filters after the paste operation.

Categories

Resources