Excel COM in C# Visual Studio - c#

So I am working in visual studio 2013 and I am trying to return the string in a given cell in a worksheet.
public StringBuilder MyCellData(int rCnt, int cCnt)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
StringBuilder str = new StringBuilder("");
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(#"C:\Users\james.naughton\Documents\Visual Studio 2013\Projects\QA_Automation\QA_Automation\copyChangeReportWAccountID.xlsx", 0, true, 5, "Landeck1", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets.get_Item(1);
range = xlWorkSheet.UsedRange[rCnt,cCnt];
str.Append((range.Item[rCnt, cCnt]).ToString());
return str;
Now whenever I run the program for some reason the values for range and xlWorkSheet are always COM Object, so when it is not returning what I want it to. Any suggestions on how to access the correct worksheet and return the data from a cell?
Thanks,
James

You need to get the Value of your cell:
str.Append((range.Item[rCnt, cCnt]).Value.ToString());

Related

How to get type of data reading from excel file in C#

I am trying to read data from an Excel file. Is there a way to check the type of data in each cell?
I have tried .GetType(); but the result is a very complicated class which didn't help.
here are parts of my code:
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(FilePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
var a = range.Cells[1/*row*/, 1/*column*/] as Excel.Range;
var type = a.GetType();
UPDATE: For example, by runnig this line :
string MobileNumber = (string)(range.Cells[1, 7] as Excel.Range).Value2;
I get this error:
Cannot convert type 'double' to 'string'
But this problem was solved with following code:
double tempValue = (range.Cells[1, 7] as Excel.Range).Value2;
string MobileNumber = tempValue.ToString();

Exporting to Excel from clipboard and prevent line breaks from adding blank rows

When exporting to excel I get blank or multiple rows per data source row when one of my data source cells contains a line break (Carriage Return). I want to prevent this.
This is the code I am using to create a workbook and paste in my data:
//Copy to clipboard
DGVJobList.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
DGVJobList.SelectAll();
DataObject DO = DGVJobList.GetClipboardContent();
Clipboard.SetDataObject(DO);
DGVJobList.ClearSelection();
//Create Excel Workbook and Paste
Excel.Application XLApp = new Excel.Application();
Excel.Workbook XLWorkBook;
Excel.Worksheet XLWorkSheet;
object misValue = System.Reflection.Missing.Value;
XLWorkBook = XLApp.Workbooks.Add(misValue);
XLWorkSheet = (Excel.Worksheet)XLWorkBook.Worksheets.get_Item(1);
Excel.Range XLRange = (Excel.Range)XLWorkSheet.Cells[1, 1];
XLRange.Select();
XLWorkSheet.PasteSpecial(XLRange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
Instead of using the clipboard, you will have more control over data placement by creating a 2D array of data out of your DGVJobList. Then, create a reference to the Excel Range of the same dimensions and set its value. This example shows only a generic 2D array for illustration. You would build your 2D array to lay out your data the way you want.
//Build data as 2D array
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
//Create Excel Workbook and Paste
Excel.Application XLApp = new Excel.Application();
Excel.Workbook XLWorkBook;
Excel.Worksheet XLWorkSheet;
object misValue = System.Reflection.Missing.Value;
XLWorkBook = XLApp.Workbooks.Add(misValue);
XLWorkSheet = (Excel.Worksheet)XLWorkBook.Worksheets.get_Item(1);
//Use range same size as data
Excel.Range XLRange = (Excel.Range) XLWorkSheet.Range["A1", "B4"];
XLRange.Value = array2D;

How to open the corresponding excel sheet in a workbook using c#

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.

C# and Excel advanced filter

I'm trying to filter data from one workbook to another obne, but I get an unpsecified exception.
I can't find many examples for adavnced filtering in excel using c# and I don't know Where's exactly my error.
Here's my code:
public void excel()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBookImport;
Excel.Workbook xlWorkBookOriginal;
Excel.Worksheet xlWorkSheetImport;
Excel.Worksheet xlWorkSheetOriginal;
xlApp = new Excel.ApplicationClass();
xlWorkBookImport = xlApp.Workbooks.Open("c:/open.xls");
xlWorkBookOriginal = xlApp.Workbooks.Open("c:/open1.xls");
xlWorkBookOriginal.Activate();
xlWorkSheetOriginal = (Excel.Worksheet)xlWorkBookOriginal.Worksheets.get_Item(1);
xlWorkSheetOriginal.get_Range("1:7").Delete();
xlWorkBookImport.Activate();
xlWorkSheetImport = (Excel.Worksheet)xlWorkBookImport.Worksheets.get_Item(1);
xlWorkSheetOriginal.Cells.AdvancedFilter(Excel.XlFilterAction.xlFilterCopy, Type.Missing, CopyToRange: "a1:a6", Unique: true);
xlApp.Visible = true;
}
Anyone can help me with a solution?
I got it figured out myeslf in the end:
public void excel()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBookImport;
Excel.Workbook xlWorkBookOriginal;
Excel.Worksheet xlWorkSheetImport;
Excel.Worksheet xlWorkSheetOriginal;
xlApp = new Excel.ApplicationClass();
xlWorkBookImport = xlApp.Workbooks.Open("c:/open.xls");
xlWorkBookOriginal = xlApp.Workbooks.Open("c:/open1.xls");
xlWorkBookOriginal.Activate();
xlWorkSheetOriginal = (Excel.Worksheet)xlWorkBookOriginal.Worksheets.get_Item(1);
xlWorkSheetOriginal.get_Range("1:7").Delete();
// added code
Excel.Range range = xlWorkSheetOriginal.get_Range("a1:v53");
//
xlWorkBookImport.Activate();
xlWorkSheetImport = (Excel.Worksheet)xlWorkBookImport.Worksheets.get_Item(1);
// added code
xlWorkSheetImport.Activate();
Excel.Range target = xlWorkSheetImport.get_Range("a1:f1");
//
// modified code
xlWorkSheetOriginal.Cells.AdvancedFilter(Action : Excel.XlFilterAction.xlFilterCopy,
CriteriaRange: Type.Missing, CopyToRange: target, Unique: false);
//
xlApp.Visible = true;
}

C# code to Hide toolbars in an excel document

I have a C# Winforms program that opens an excel document with the code below.
It works great but what I can not figure out how to do, is to turn off ALL menu's and toolbars.
The excel version I am using right now is 2003... But I will be upgrading to 2010 in the near future.
Any ideas?
//top of source...
using Excel = Microsoft.Office.Interop.Excel;
// Code inside a function...
// Get report and display it on the screen.
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(strFileName, 0, true, 5, "", "", true,Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlApp.Visible = true;
xlApp.DisplayFullScreen = true;
// Display the Document and then Sleep.
System.Threading.Thread.Sleep(timeToShowMilliseconds);
// Close the Excel report
xlWorkBook.Close(false, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
I've been looking into your case and i think with Excel.Application you can find the answer.
Apparently what you need to do is something like this:
Excel.Application xlApp;
xlApp.CommandBars("tabName").Controls("File").Enabled = false;
try it and let me know, if it doesnt work we'll figure something out.

Categories