ASP.NET - How to read a particular cell from Excell in c#.net

Asked By Ajay Paritala on 27-Sep-12 05:30 AM

Hi,

How to get a particular cell value from excell sheet. Can some one help with this please..  using c#.net

Danasegarane Arunachalam replied to Ajay Paritala on 27-Sep-12 06:42 AM
Refer this article

http://sandeep-aparajit.blogspot.in/2008/08/how-to-read-excel-in-c.html
Ajay Paritala replied to Danasegarane Arunachalam on 27-Sep-12 06:49 AM
Hi if you don't mind can you please copy and paste the content in that blog??

it's not opeining in my organization Please?
Danasegarane Arunachalam replied to Ajay Paritala on 27-Sep-12 06:55 AM
Here it is

This article will help you understand reading an excel file in C#. This is often required when developing applications. Create an excel file named "Test.xls" in the C Drive. The sample excel file will look like:
For interacting with an excel file you will have to include the following COM assemblies:

  • Microsoft Excel 12.0 Object Library
  • Microsoft Office 12.0 Object Library

Following code will be used to read the excel file and display the values in a Console application:



// Add Reference
using System;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

namespace ReadExcel
{
/// <summary>
/// This class will be used to read the excel and
/// display it in a console.
/// </summary>
class ReadExcelApplication
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
// Path for the test excel application
string Path = @"c:\test.xls";
// Initialize the Excel Application class
Excel.ApplicationClass app = new ApplicationClass();
// Create the workbook object by opening the excel file.
Excel.Workbook workBook = app.Workbooks.Open(Path,
0,
true,
5,
"",
"",
true,
Excel.XlPlatform.xlWindows,
"\t",
false,
false,
0,
true,
1,
0);
// Get the active worksheet using sheet name or active sheet
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;

// This row,column index should be changed as per your need.
// i.e. which cell in the excel you are interesting to read.
int index = 1;
object rowIndex = 1;
object colIndex1 = 1;
object colIndex2 = 2;

try
{
while (((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2 != null)
{
// Read the Cells to get the required value.
string firstName = ((Excel.Range)workSheet.Cells[rowIndex, colIndex1]).Value2.ToString();
string lastName = ((Excel.Range)workSheet.Cells[rowIndex, colIndex2]).Value2.ToString();
Console.WriteLine("Name : {0},{1} ", firstName, lastName);
index++;
rowIndex = index;
}
}
catch (Exception ex)
{
// Log the exception and quit...
app.Quit();
Console.WriteLine(ex.Message);
}
}

}
}
Hope this helps! Your comments are always welcome

Ajay Paritala replied to Danasegarane Arunachalam on 27-Sep-12 09:05 AM
Thank You

I tried this it working fine.

Microsoft.Office.Interop.Excel.Range firstCell = objWorksheet.get_Range("A1", Type.Missing);
          Microsoft.Office.Interop.Excel.Range lastCell = objWorksheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);

          object[,] cellValues;
          //object[,] cellFormulas;

          Microsoft.Office.Interop.Excel.Range worksheetCells = objWorksheet.get_Range(firstCell, lastCell);
          cellValues = worksheetCells.Value2 as object[,];
          //cellFormulas = worksheetCells.Formula as object[,];

   General objCurrency = new General(); // My Class Name
 objWorksheet.Cells[49, 4] = objCurrency.TranslateCurrency(Convert.ToDecimal(cellValues[49, 9])).ToString();


Danasegarane Arunachalam replied to Ajay Paritala on 27-Sep-12 09:14 AM
Happy to see this worked
Jitendra Faye replied to Ajay Paritala on 01-Oct-12 01:56 AM
reference from-

http://social.msdn.microsoft.com/forums/en-US/vsto/thread/b6e8a28c-6760-4e86-a1aa-e2ce9ec36380/

solution-


 Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(
     "path", 0, true, 5,
      "", "",
true, Excel.XlPlatform.xlWindows, "\t", false, false,
      0,
true);
 
   Excel.Sheets sheets = theWorkbook.Worksheets;
   Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
   for (int i = 1; i <= 10; i++)
   {
   Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString());
   System.Array myvalues = (System.Array)range.Cells.Value;

   string[] strArray = ConvertToStringArray(myvalues);