Sunday 13 May 2012

Excel

Excel.Application excelApp = default(Excel.Application);
Excel.Workbook excelWorkbook = default(Excel.Workbook);
Excel.Worksheet excelWorksheet = default(Excel.Worksheet);
excelApp = new Excel.Application();
excelWorkbook = excelApp.Workbooks.Open(bbi, 0,
true,
5,
"",
"",
true,
Excel.XlPlatform.xlWindows,
"t",
false,
false,
0,
true,
1,
0);
excelWorksheet = (Excel.Worksheet)excelWorkbook.ActiveSheet;
Excel.Sheets shts = (Excel.Sheets)excelWorkbook.Sheets;
int startingrowId = 23;
foreach (Excel.Worksheet sheet1 in shts)
{
excelWorksheet = (Excel.Worksheet)sheet1;

switch (excelWorksheet.Name)
{
case "***": Samplemethod();
break;
case Samplemethod2: continue;
}
}
}

---------------------------------------------------------------------

object columnA = 2;
object columnB = 3;
object columnC = new object();
object columnD = new object();
object columnE = 11;
while (((Excel.Range)excelWorksheet.Cells[startingRowId, columnA]).Value2 != null)
{
try
{
string description = ((Excel.Range)excelWorksheet.Cells[startingRowId, columnA]).Value2.ToString();
string ppString = ((Excel.Range)excelWorksheet.Cells[startingRowId, columnB]).Value2.ToString();
if (((Excel.Range)excelWorksheet.Cells[startingRowId, columnD]).Value2 != null)
{
}
startingRowId++;
}
--------------------------------------------------------------------------------------

public static void readExcelfile()
{
Excel.Application excelApp = default(Excel.Application);
Excel.Workbook excelWorkbook = default(Excel.Workbook);
Excel.Worksheet excelWorksheet = default(Excel.Worksheet);
excelApp = new Excel.ApplicationClass();

//excelWorkbook = excelApp.Workbooks.Open(@"C:Tempsample.xls", 0,
excelWorkbook = excelApp.Workbooks.Open(@"C:Tempsample.xls", 0,
true,
5,
"",
"",
true,
Excel.XlPlatform.xlWindows,
"t",
false,
false,
0,
true,
1,
0);
excelWorksheet = (Excel.Worksheet)excelWorkbook.ActiveSheet;
int index = 0;
int i = 1;
// This row,column index should be changed as per your need.
// i.e. which cell in the excel you are interesting to read.
object rowIndex = 1;
object colIndex1 = 1;
object colIndex2 = 2;
try
{
while (((Excel.Range)excelWorksheet.Cells[rowIndex, colIndex1]).Value2 != null)
{
//rowIndex = 1+index;
string firstName = ((Excel.Range)excelWorksheet.Cells[rowIndex, colIndex1]).Value2.ToString();
string lastName = ((Excel.Range)excelWorksheet.Cells[rowIndex, colIndex2]).Value2.ToString();
Console.WriteLine("Name : {0},{1} ", firstName, lastName);
rowIndex = 1 + i;
i++;
index++;
}
}
catch (Exception ex)
{
excelApp.Quit();
Console.WriteLine(ex.Message);
}
}

--------------------------------------------------------

http://support.microsoft.com/kb/302084
http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm
http://exceldatareader.codeplex.com/
http://www.codeproject.com/KB/cs/Excel_Application_in_C_.aspx
http://stackoverflow.com/questions/193092/c-how-to-add-excel-worksheet-programatically-office-xp-2003

No comments:

Post a Comment