Custom Search
Logiclabz
  • Home
  • Asp.Net
  • Reading / Writing data from Excel in ASP.Net 2.0

Reading / Writing data from Excel in ASP.Net 2.0

First : Add "Microsoft Excel Object Library" Reference to the Current project. Note - I have used "Microsoft Excel 11.0 Object Library" And use following code to read or write

using Microsoft.Office.Interop.Excel;

// Creates a new Excel Application Object
ApplicationClass xlsApp = new ApplicationClass();  

try
{
    //Opens WorkBook in Excel
    WorkbookClass xlsWrkBook=(WorkbookClass)xlsApp.Workbooks.Open
 ("c:\\1.xls", 0, false, 5, "", "", true,
 XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
    //Gets the sheet 1 of the opened workbook  
    Worksheet xlsWrkSht = (Worksheet)xlsWrkBook.Worksheets.get_Item(1);
    //Gets Cell 1st Row and 1st Column. Desired can be given 
    Range ExcelCellText = (Range)xlsWrkSht.Cells[1, 1];
    // Reads Data of selected cell
    string str = ExcelCellText.Text.ToString();
    // Writes Data of selected cell 
    ExcelCellText.Value2 = "Sample Write";
    xlsWrkBook.Save();
    xlsWrkBook.Close(0, 0, 0);
}

catch
{ }

finally
{
    xlsApp = null;
}



Leave a reply


Do you like this post?