Thanks to Vimal and and Sateesh, we could manage to read an excel file , and change one cell value to double(+doing some math operation).I want to write it back to excel by c#.
Would you please let me know how to :
1. write this new value(of type double) to another excel file(say Book.3) and in Worksheet3 and cell[12,1]
using
System;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 =
false;oBooks = App.Workbooks;
//Open a new workbookoBook = 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] = oSheet.Cells.Cells[1, 1] / 2; for (int j = 1; j <= 2; j++){
for (int i = 1; i < 50; i++){
object m = ((Range)oSheet.Cells.Cells[i, j]).Text; double x = Convert.ToDouble(m) / 2; Console.WriteLine(x);}
}
//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;}
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.