I am attempting to read an excel workbook that contains 5 worksheets. No matter what I do, it reads in the last worksheet only. Don't get why and it is driving me crazy.
Application xlApp = new Application();
Workbook xlWorkbook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory+#"Tables.xlsx");
_Worksheet xlWorksheet = (Worksheet)xlWorkbook.Worksheets[4];
Range xlRange = xlWorksheet.UsedRange;
Will return worksheet 5.
Application xlApp = new Application();
Workbook xlWorkbook = xlApp.Workbooks.Open(System.AppDomain.CurrentDomain.BaseDirectory+#"Tables.xlsx");
_Worksheet xlWorksheet = (Worksheet)xlWorkbook.Worksheets["Table 4"];
Range xlRange = xlWorksheet.UsedRange;
Still returns 5 (5 is labeled "Table 5")
Any help would be great. Appreciate ya.
For an excel 2010 plugin, I need a method to change cell values of the active worksheet.
This code works, but opens a new excel window.
How can I change this, to edit the values from the active worksheet?
Microsoft.Office.Interop.Excel.Application objApp;
Microsoft.Office.Interop.Excel.Workbook objBook;
Microsoft.Office.Interop.Excel.Sheets objSheets;
Microsoft.Office.Interop.Excel._Worksheet workSheet;
objApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
objApp.Visible = true;
objBook = (Microsoft.Office.Interop.Excel.Workbook)objApp.ActiveWorkbook;
if (objBook == null)
{
objBook = objApp.Workbooks.Add();
}
// get the collection of sheets in the workbook
objSheets = objBook.Worksheets;
// get the first and only worksheet from the collection of worksheets
workSheet = (Microsoft.Office.Interop.Excel.Worksheet)objSheets.get_Item(1);
workSheet.Cells[1, "A"] = "wert1";
Now I found the solution:
Microsoft.Office.Interop.Excel.Workbook objBook;
Microsoft.Office.Interop.Excel.Sheets objSheets;
Microsoft.Office.Interop.Excel._Worksheet workSheet;
objBook = (Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
if (objBook == null)
{
objBook = objApp.Workbooks.Add();
}
// get the first and only worksheet from the collection of worksheets
workSheet = (Microsoft.Office.Interop.Excel.Worksheet)objBook.ActiveSheet;
workSheet.Cells[1, "A"] = "value1A";
Source:
http://www.dreamincode.net/forums/topic/199576-getting-current-excel-application-or-workbook-in-c%23/
My code opens the first sheet of an excel. My target is to open the sheet selected in combobox. Do anyone can help me to find solution:
My Code:
string currsheet = comboBox1.SelectedItem.ToString();
Microsoft.Office.Interop.Excel.Application xap = new Microsoft.Office.Interop.Excel.Application();
xap.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wk = xap.Workbooks.Open(path3,0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Sheets excelsheet = wk.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet wsh = (Worksheet)excelsheet.get_Item(currsheet);
By sheet name
Microsoft.Office.Interop.Excel.Worksheet wsh = (Worksheet)excelsheet["SheetName"];
By index (starting with 1 - first sheet)
Microsoft.Office.Interop.Excel.Worksheet wsh = (Worksheet)excelsheet[1];
Just use one of those values in your combobox.
If you want to populate your combo box with available sheets, you can go throug Worksheets
foreach (Worksheet Sh in excelsheet)
{
Combobox.Items.Add(Sh.Name);
}
Then, the combobox selected value will be already a sheet name, you get by:
Microsoft.Office.Interop.Excel.Worksheet wsh = (Worksheet)excelsheet[Combobox.SelectedValue]; //I'm not sure if combobox value is got like this, but the excel part is ok.
I am currently working in a C# application which has a class which will generate an excel file. Everything went smooth. The data populated on the excel sheet has 'Times New Roman' has font. I would like to change it to some other fonts (Calibari). How can I do that programmatically.
From what I tried, simply changing font name, size etc... on range changes font for that range:
range.Font.Name = "Arial"
range.Font.Size = 10
range.Font.Bold = true
Here is how:
//Declare Excel Interop variables
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
//Initialize variables
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
//Set global attributes
xlApp.StandardFont = "Arial Narrow";
xlApp.StandardFontSize = 10;
Focus on the 2nd line from the bottom. That sets the default font type, but I wanted to show you where xlApp came from, even if it's self explanatory.
the following worked for me, when I tried setting the default application font it did nothing so I was able to set the font name of the active sheet rows and it worked. Also worth noting I used and tested this using Excel Interop version 12
Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Create\Add workbook object
Excel.Workbooks workBooks = excelApp.Workbooks;
//Excel.Workbook
Excel.Workbook workBook = workBooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
//use worksheet object
Excel.Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
//set default font
workSheet.Rows.Font.Name = "Arial";
Have you tried something like this:
new Font("Arial", 10, FontStyle.Bold);
var range = worksheet.get_Range(string.Format("{0}:{0}", startRowIndex, Type.Missing));
range = range.EntireRow;
range.Style.Font.Name = "Arial";
range.Style.Font.Bold = false;
range.Style.Font.Size = 12;
Hey Do not upset I do it and works for me .
Just define Font.Name and excell sheet fill all sheet use everywhere .
Any Way Code is :
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount-1]].Merge();
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Interior.Color = ColorTranslator.ToOle(Color.FromArgb(23,65,59));
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Borders.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
workSheet.Range[workSheet.Cells[1, tempCount], workSheet.Cells[1, tempCount + mergeCount - 1]].Style.Font.Name = "Arial Narrow";
((Excel.Range)WorksheetResult.UsedRange).Font.Name = "Avant Garde";
WorksheetResult is just a sheet reference.
Found this thread by my own similar problem. I had a little picker box that, when a cell was clicked, needed to paste a unique font's symbol into the selected excel cell. Here's how i did that:
string selectedItem = arrayOfSymbols[tableLayoutPanel1.GetRow((Control)sender), tableLayoutPanel1.GetColumn((Control)sender)];
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveSheet;
Excel.Range cell = Globals.ThisAddIn.Application.ActiveCell;
ws.Cells[cell.Row, cell.Column].Font.Name = "My Custom Font";
ws.Cells[cell.Row, cell.Column] = selectedItem;
I'm using C# COM Interop to create and update Excel files on the fly. I used the below code. But I'm not sure how to add color to a particular cell. Please help me with the same.
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Excel.Application();
excel.Visible = true;
Excel.Workbook wb = excel.Workbooks.Open(excel_filename);
Excel.Worksheet sh = wb.Sheets.Add();
sh.Name = "TestSheet";
sh.Cells[1, "A"].Value2 = "SNO";
sh.Cells[2, "B"].Value2 = "A";
sh.Cells[2, "C"].Value2 = "1122";
wb.Close(true);
excel.Quit();
Try this
wb.Cells[row, clmn].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)