Who can please let me know:
How to read the excel cells having DOUBLE values into c# from worksheet1.Then do some calculation and write it back into other excel worksheet2?
tnx
Who can please let me know:
How to read the excel cells having DOUBLE values into c# from worksheet1.Then do some calculation and write it back into other excel worksheet2?
tnx
Know the answer? Post it. Somebody with the same question will find it here.
Bechir BejaouiRank #144 · 15.9K pointsPosted Feb 23, 2009, 10:51 AM EST
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace ConsoleApplication1
{
class Program
{
static ApplicationClass App;
static Workbooks oBooks;
static Workbook oBook;
static Worksheet oSheet;
static void Main(string[] args)
{
App = null;
oBooks = null;
oBook = null;
oSheet = null;
Console.WriteLine("Enter the Excel file path");
string filepath = Console.ReadLine();
App = new ApplicationClass();
App.Visible = true;
oBooks = App.Workbooks;
//Open a new workbook
oBook = oBooks.Open(filepath,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value);
oSheet = (Worksheet)App.ActiveSheet;
/* After that you can get your cells either by
* range */
Range myRange = oSheet.get_Range("A1", "C3");
//Or by cell
oSheet.Cells.Cells[1, 1] = 2;
//You can get or create another sheet and transmit the content
if (App.Worksheets[2] != null)
{
Worksheet oSheet2 = App.Worksheets[2] as Worksheet;
oSheet2.Cells.Cells[1, 1] = oSheet.Cells.Cells[1, 1];
}
oBook.Save();
App.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(App);
App = null;
}
}
}
yamid88 pointsPosted Feb 23, 2009, 12:11 PM EST
tnx for your reply but , I could not do some mathematical operation on the value of each cell, because they are obj. would you please tell me how to convert those cell values to Double?
thanks
Bechir BejaouiRank #144 · 15.9K pointsPosted Feb 26, 2009, 12:49 AM EST
object myConvertingObject = oSheet2.Cells.Cells[row, col] as Range ;
double myConvertedObject = (Double)myConvertingObject;