by Ciege » Wed Dec 21, 2011 5:31 pm
Could you please share some code examples?
Sure... I'll put a coupld of example methods here. There are more, but this should get you started...
Open Excel Method:- Code: Select all
public static Microsoft.Office.Interop.Excel.Application OpenExcel()
{
/************************************************************************
* Function : OpenExcel()
*
* Description : This method will open and return an Excel COM object.
*
* Parameters : N/A
*
* Return Type : Microsoft.Office.Interop.Excel.Application
*
* Return Value : Valid Excel Object for success, null for failure
*
* Revision History
* Date : Author : Reason/Change
* ---------- : ------------------------- : ------------------------------
* 03/23/2009 : Chris Gendreau : Initial Creation
************************************************************************/
Microsoft.Office.Interop.Excel.Application objXL;
try
{
//Create the Excel Object
objXL = new Microsoft.Office.Interop.Excel.Application();
//Return the Excel Object
return objXL;
}
catch (Exception ex)
{
Report.Failure("Unable to open Excel: " + ex.ToString());
return null;
}
} //End OpenExcel
Open Workbook Method:- Code: Select all
public static Microsoft.Office.Interop.Excel.Workbook OpenExcelWorkbook(Microsoft.Office.Interop.Excel.Application objXL, string strExcelFileName)
{
/************************************************************************
* Function : OpenExcelWorkbook()
*
* Description : This method will open and return an Excel Workbook COM object.
*
* Parameters : objXL - An existing Excel Object
* : strExcelFileName - path & filename of the Excel Workbook
*
* Return Type : Microsoft.Office.Interop.Excel.Workbook
*
* Return Value : Valid Excel Workbook Object for success, null for failure
*
* Revision History
* Date : Author : Reason/Change
* ---------- : ------------------------- : ------------------------------
* 07/10/2009 : Chris Gendreau : Initial Creation
* 11/30/2009 : Chris Gendreau : Added objXLWorkBook.Saved = true
************************************************************************/
Microsoft.Office.Interop.Excel.Workbook objXLWorkBook;
if (objXL != null)
{
try
{
objXLWorkBook = objXL.Workbooks.Open(strExcelFileName, 0, true, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, true, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false,
System.Reflection.Missing.Value, false, false, false);
//Mark the workbook as saved so that the "Do you want to save" dialog does not appear.
objXLWorkBook.Saved = true;
//Return the Excel Workbook Object
return objXLWorkBook;
}
catch (Exception ex)
{
Report.Failure("Unable to open Excel: " + ex.ToString());
return null;
}
}
return null;
} //End OpenExcelWorkbook
Read a Single Cell Method- Code: Select all
public static string ExcelReadSingleCell(string strExcelFileName, string strCell, string strSheet)
{
/************************************************************************
* Function : ExcelReadSingleCell(string strExcelFileName, string strCell, string strSheet)
*
* Description : This method will read and return the data of a single cell
* : in the specified Excel workbook.
*
* Parameters : strExcelFileName - Full path and filename of the Excel spreadsheet to open
* : strCell - Cell to return (i.e. "A1")
* : strSheet - OPTIONAL - Name of the sheet in the spreadsheet the cell is on
* : - If not supplied will default to the 1st sheet.
*
* Return Type : String
*
* Return Value : Data from the cell for success, null for failure
*
* Revision History
* Date : Author : Reason/Change
* ---------- : ------------------------- : ------------------------------
* 03/23/2009 : Chris Gendreau : Initial Creation
************************************************************************/
Microsoft.Office.Interop.Excel.Application objXL;
Microsoft.Office.Interop.Excel.Workbook objXLWorkBook;
Microsoft.Office.Interop.Excel.Sheets objXLSheets;
Microsoft.Office.Interop.Excel.Worksheet objXLWorkSheet;
Microsoft.Office.Interop.Excel.Range objXLRange;
if (strSheet == "")
{
Report.Debug("Reading data from Cell: '" + strCell + "' in spreadsheet: '" + strExcelFileName + "' on sheet: '1'.");
}
else
{
Report.Debug("Reading data from Cell: '" + strCell + "' in spreadsheet: '" + strExcelFileName + "' on sheet: '" + strSheet + "'.");
}
//Open Excel
objXL = OpenExcel();
if (objXL != null)
{
try
{
//Open the Excel Workbook
objXLWorkBook = objXL.Workbooks.Open(strExcelFileName, 0, true, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
System.Reflection.Missing.Value, true, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false,
System.Reflection.Missing.Value, false, false, false);
//Get the sheets from the workbook
objXLSheets = objXLWorkBook.Sheets;
if (strSheet == "")
{
//Select the first worksheet
objXLWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)objXLSheets[1];
}
else
{
//Select the requested worksheet
objXLWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)objXLSheets[strSheet];
}
//Select the cell in the workbook
objXLRange = (Microsoft.Office.Interop.Excel.Range)objXLWorkSheet.get_Range(strCell + ":" + strCell, Type.Missing);
//Read the data from the cell
string ExcelString = objXLRange.Cells.Value2.ToString();
////Print the value of the cell for a single cell selection
//Console.WriteLine(" Text in cell " + strCell + " is: " + ExcelString);
//Close Excel
objXL.Quit();
//Return the data
return ExcelString;
}
catch (Exception ex)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, ex.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, ex.Source);
errorMessage = String.Concat(errorMessage, ex.ToString());
Report.Failure("Error reading a cell from Excel: " + errorMessage);
//Close Excel
objXL.Quit();
return null;
}
}
return null;
} //End ExcelReadSingleCell
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!
Ciege...