I need to write hard-coded value "my value" in cell "C24" to an already existing MS excel sheet. Is it possible tht i can write a function in which i just have to pass the "C24" AND "MY VALUE" and hence it wud write the value to the corresponding cell??
// prepare open file dialog to only search for excel files (had trouble setting this in design view)
this.openFileDialog1.FileName =
"TestWorksheet.xls";if (this.openFileDialog1.ShowDialog()
== DialogResult.OK)
{
// Here is the call to Open a Workbook in Excel
// It uses most of the default values (except for the read-only which we set to true)
objApp = new Microsoft.Office.Interop.Excel.Application();
true, 5,"",
"", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t", false,
false, 0, true,
false, false);
// get the collection of sheets in the workbook
objSheets = objBook.Worksheets;
// get the first and only worksheet from the collection of worksheets
workSheet = (Microsoft.Office.Interop.Excel.Worksheet)objSheets.get_Item(1);
// loop through 5 rows of the spreadsheet and place each row in the list view
for (int i = 1; i <= 5; i++)
{
range = workSheet.get_Range("A" + i.ToString(),
"D" + i.ToString());
here it is just writing "my value" to column 1 of all the 5 rows being looped. can i somehow just pass"c24" and get to that specific cell and wrote a value??
S_a_r_a_h
Member
26 Points
56 Posts
How to write value to a single MS excel cell from c# code?
Nov 05, 2008 05:07 AM|LINK
Hi all...
I need to write hard-coded value "my value" in cell "C24" to an already existing MS excel sheet. Is it possible tht i can write a function in which i just have to pass the "C24" AND "MY VALUE" and hence it wud write the value to the corresponding cell??
so far i have done this...
private void browseButton_Click(object sender, EventArgs e){
Excel.Application objApp;Excel.
Workbook objBook; Excel.Sheets objSheets;Excel.
_Worksheet workSheet; Excel.Range range;
// prepare open file dialog to only search for excel files (had trouble setting this in design view) this.openFileDialog1.FileName = "TestWorksheet.xls";if (this.openFileDialog1.ShowDialog() == DialogResult.OK){
// Here is the call to Open a Workbook in Excel // It uses most of the default values (except for the read-only which we set to true) objApp = new Microsoft.Office.Interop.Excel.Application();objBook = objApp.Workbooks.Open(openFileDialog1.FileName, 0,
true, 5,"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false); // get the collection of sheets in the workbookobjSheets = objBook.Worksheets;
// get the first and only worksheet from the collection of worksheets workSheet = (Microsoft.Office.Interop.Excel.Worksheet)objSheets.get_Item(1);
// loop through 5 rows of the spreadsheet and place each row in the list view for (int i = 1; i <= 5; i++){
range = workSheet.get_Range("A" + i.ToString(), "D" + i.ToString());System.
Array myvalues = (System.Array)range.Cells.Value2; string[] strArray = ConvertToStringArray(myvalues);workSheet.Cells[i, 1] =
"my value"; }}
}
here it is just writing "my value" to column 1 of all the 5 rows being looped. can i somehow just pass"c24" and get to that specific cell and wrote a value??
Plz help me out !
riteshtandon...
Member
281 Points
72 Posts
Re: How to write value to a single MS excel cell from c# code?
Jul 08, 2011 08:07 AM|LINK
Hi,
I think if you change your line
range = workSheet.get_Range("A" + i.ToString(), "D" + i.ToString());
To
range = workSheet.get_Range("A12", "A12");
This would refer the range object to cell A12 and then you can set the value.
thnx
ritessh(http://www.jalandharsearch.com)
Rajani123
Member
238 Points
95 Posts
Re: How to write value to a single MS excel cell from c# code?
Jul 08, 2011 08:51 AM|LINK
Try using following:
excelWorksheet.Cells[24,3] = "my value";