I have a problem with Excel worksheet. I am trying to create an Excel file with c#.
This code works and runs correctly on my computer but in other computers get an error at last line:
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheetInvoice;
Excel.Worksheet xlWorkSheetInvoiceLine;
object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheetInvoice = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheetInvoiceLine = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
System.Runtime.InteropServices.COMException (0x8002000B): Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))
at Microsoft.Office.Interop.Excel.Sheets.get_Item(Object Index)
I executed a sample application with similar code on a machine with Excel 2013 and it fails at the same line of code you mentioned. By default Excel 2013 application opens up with a single worksheet ("Sheet1") so you would have to modify the code accordingly
DISP_E_BADINDEX seems to suggest the number of worksheets is less on the other computers. Build in a check to see if the number of worksheets is less than 2 before using get_Item().
i have created new sheet and assigned xlWorkSheetInvoice and xlWorkSheetInvoiceLine to solve it.
var xlSheets = xlWorkBook.Sheets as Excel.Sheets;
var xlNewSheet = (Excel.Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
var xlNewSheet2= (Excel.Worksheet)xlSheets.Add(xlSheets[2], Type.Missing, Type.Missing, Type.Missing);
xlWorkSheetInvoice = xlNewSheet;
xlWorkSheetInvoiceLine = xlNewSheet2;
xlWorkSheetInvoice = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheetInvoiceLine = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
Related
All,
Apologies if this is a very basic question and has been asked before, I predominately write in VBA / JAVA. However a project I am working on requires a C# script. Which carries out 3 simple steps:
Target a excel workbook which is already open. File path:
\Csdatg04\psproject\Robot\Peoplesoft To LV\Master Files - Do not use\Transactions into LV Template.xlsm
Populate cells A1,A2 & A3 with three variables already retrieved earlier in the automation.
Run a macro stored within the filepath mentioned above Macro name "ControlMacroACT"
The code I have developed is below, however in each stage identified above I am encountering errors (Probably basic errors).
Error 1: This line of code is to open a workbook I would like this to target an already active workbook.
Error 2: Worksheet not found
public void RunActualsMacro(string Filepath, string Period, String FiscalYear)
{
//~~> Define your Excel Objects
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
//~~> Start Excel and open the workbook.
//Error 1
xlWorkBook = xlApp.Workbooks.Open("\\Csdatg04\\psproject\\Robot\\Peoplesoft To LV\\Master Files - Do not use\\Transactions into LV Template.xlsm");
// Populat Cells A1,A2,A3 with string variables
// Error 2 Worksheet not found
worksheet.Rows.Cells[1, 1] = Filepath;
worksheet.Rows.Cells[2, 1] = Period;
worksheet.Rows.Cells[3, 1] = FiscalYear;
//~~> Run the macro ControlMacroAct
xlApp.Run("ControlMacroACT");
//~~> Clean-up: Close the workbook
xlWorkBook.Close(false);
//~~> Quit the Excel Application
xlApp.Quit();
}
Any help would be much appreciated.
You need to use Marshal.GetActiveObject, and this code is roughly right, but cannot test right now.
public void RunActualsMacro(string Filepath, string Period, String FiscalYear)
{
//~~> Define your Excel Objects
Excel.Application xlApp = null;
Excel.Workbook xlWorkBook;
//~~> Start Excel and open the workbook.
//handle errors below
try {
xlApp = (Excel.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
} catch {
//perhaps exit - or throw??
}
xlWorkBook = xlApp.Workbooks["Transactions into LV Template.xlsm"];
// Populat Cells A1,A2,A3 with string variables
Excel.Worksheet ws = xlWorkBook.Worksheets["Sheet1"] //what the tab name of sheet
ws.Cells[1, 1] = Filepath;
ws.Cells[2, 1] = Period;
ws.Cells[3, 1] = FiscalYear;
//~~> Run the macro ControlMacroAct
xlApp.Run("ControlMacroACT");
//~~> Clean-up: Close the workbook
xlWorkBook.Close(false);
//~~> Quit the Excel Application
xlApp.Quit();
}
I'm currently working on a Programm which creates reports using Excel Interop. Therefore I intend to create a Line Chart as followed:
As one can see here I have a clear development of num and denom in relation to Milestones. When I select my values as chartRange manually in Excel I recieve this Diagramm. However, when do it programmatically (select exactly the same range) I get this:
Well, you can see the data selected. Excel (or something I don't know about) fails to recognize the Milestones row as axis. When I right click on the Diagramm --> select data --> Change rows/columns I get my right Chart.
Now does anybody know an order to switch those rows/columns or some way to define ranges for each of the axis?
I attached the relevant part of my code here:
Excel.Application myExcelApplication;
Excel.Workbook myExcelWorkbook;
object misValue = System.Reflection.Missing.Value;
myExcelApplication = null;
myExcelApplication = new Excel.Application();
myExcelApplication.Visible = true;
myExcelApplication.ScreenUpdating = true;
myExcelWorkbook = (Excel.Workbook)(myExcelApplication.Workbooks.Add(misValue));
Excel.Worksheet newWorksheet;
newWorksheet = (Excel.Worksheet)myExcelApplication.Worksheets.Add();
newWorksheet.Name = requestedVehicles[m];
newWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;
Excel.Range chartRange = myExcelApplication.get_Range("B2", endrange + 4);
Excel.ChartObjects xlCharts = (Excel.ChartObjects)newWorksheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(100, 20, 500, 400);
Excel.Chart chartPage = myChart.Chart;
chartPage.HasTitle = true;
chartPage.ChartTitle.Text = "num/denom";
chartPage.SetSourceData(chartRange, Excel.XlRowCol.xlColumns);
chartPage.ChartType = Excel.XlChartType.xlXYScatterLines;
Thanks in advance,
Andy
I solved it myself due to this post I've stumbled across:
https://www.codeproject.com/questions/870177/csharp-excel-chart-x-axis-category-labels
The Problem was that the upper left cell of my defined range wasn't empty.. so Excel assumed it was supposed to be a data series.
hmmm, if the proposed solution does not work for all, this is how you can do it directly from the interop; use the method ChartWizard(), the parameter PlotBy does the trick.
This is how I use it:
wsChart.ChartWizard(Type.Missing, Type.Missing, Type.Missing, Excel.XlRowCol.xlRows, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Here is the doc:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel._chart.chartwizard?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(Microsoft.Office.Interop.Excel._Chart.ChartWizard);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6);k(DevLang-csharp)%26rd%3Dtrue&view=excel-pia
I'm trying to edit an Excel Sheet through C# code but the Excel sheet is giving me an error saying read only
CS0200 Property or indexer 'Range.Text' cannot be assigned to -- it is read only
This is the code that is trying to access the sheet.
Code:
//Load workbook
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(#"S:\Utils\documents\ServerManager\serverlist.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
xlWorksheet.Range["D2"].Text = "Kelly Cooper";
xlWorksheet.Range["D2"].Style.Font.FontName = "Arial Narrow";
xlWorksheet.Range["D2"].Style.Font.Color = Color.DarkBlue;
In the properties the Excel sheet is not read only and when I open it to edit it does not prompt me with the "This document is read only, want to enable editing." How can I go around this, Visual Studio won't let me compile because of it.
This is Excel in Office365.
You want to use .Value instead of .Text.
xlWorksheet.Range["D2"].Value = "Kelly Cooper";
xlWorksheet.Range["D2"].Style.Font.FontName = "Arial Narrow";
xlWorksheet.Range["D2"].Style.Font.Color = Color.DarkBlue;
If you check the documentation you can see that Text is read-only.
Also, if you check the documentation you can see that Value is not read-only.
EDIT
You also might have to change your Open parameters to:
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(Filename: "path/to/file", ReadOnly: false);
I'm working with an excel file and I'm trying to find the position(address) of the cells with date value like "16/02/2015" as content. In order to do this with windows Forms application I use a datetimepicker. My code is as following:
// Firstly I want a string like 16/02/2015 inserted in the datetimepicker
string date = DateTimePicker1.Value.ToString().Substring(0,10);
List<string> Testsdone = new List<string>();
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(file_opened, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(4);
Excel.Range currentFind = null;
Excel.Range firstFind = null;
Excel.Range tests = xlWorkSheet.get_Range("K13", "K2295");
currentFind = tests.Find(date);
.....
I have exactly the same content "16/02/2015" written in my excel file, but the currectFind always shows null! I have no idea why the two "16/02/2015" are different. Anybody got an idea? thanks!
Well after some investigation I found that the problem is caused by the Find function
currentFind = tests.Find(date);
which for unknown reason failed to find the date. From another post
how to Search in excel file I got this method, that I changed this into:
currentFind = xlworkSheet.Cells.Find(date, Type.Missing, Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,Type.Missing, Type.Missing);
And it works. But I still have no idea why the Excel.Range.Find fails and Excel.workersheet.Cells.Find works
A really good new feature of Excel 2013 is that it cannot forced to show more than one Excel workbook in one application. This seems the cause of my Problem:
If I open an Excel workbook programmatically using c# and interop Excel 2013 starts with a new application window. I can working with the workbook in code without problems but I want to hide the application.
Using
Excel.Application excelApp = new Excel.Application();
......
excelApp.Workbooks.Open(...);
excelApp.Visible = false;
hides the application window after showing it. Is there a way to stop showing the application as in Excel 2010 or earlier Version?
In my Excel 2013, using excelApp = new Excel.Application doesn't show any window.
May it be some VBA code in opened workbook which displays window?
So I know the question is old but I needed an answer and none of the given ones worked for me. I ended up just setting Visible to false when initializing to avoid the window flashing open before hiding.
Excel.Application excelApp = new Excel.Application() { Visible = false };
Hide Excel application your code has launched, before opening any Workbook :
Excel.Application excel = new Excel.Application();
excel.Visible = false;
[...]
Excel.Workbook workbook;
workbook = excel.Workbooks.Open(...);
You should always put the Visible into try/catch-block
Excel.Application xlsApp = new Excel.Application();
try
{
// Must be surrounded by try catch to work.
// http://naimishpandya.wordpress.com/2010/12/31/hide-power-point-application-window-in-net-office-automation/
xlsApp.Visible = false;
xlsApp.DisplayAlerts = false;
}
catch (Exception e)
{
Console.WriteLine("-------Error hiding the application-------");
Console.WriteLine("Occured error might be: " + e.StackTrace);
}
Excel.Workbook workbook;
workbook = xlsApp.Workbooks.Open(File, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);