Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 16th, 2007, 11:12 AM
Registered User
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default c# and Excel cell values

I've scoured the net for days to find a working example of how to
use csharp to open an excel file and put a value in a cell. Its dead easy in VB6, VBA, vb.net but not csharp.

I've managed to get this working but I'm sure its not the easiest way of doing this.

Its the part where I assign the range to Range one that looks well dodgy.

Is there any better way of doing this?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace Exceltest3
public partial class Form1 : Form
public Form1()

private void Form1_Load(object sender, EventArgs e)


private void button1_Click(object sender, EventArgs e)

int ColCount;
int RowCount;
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbooks theWorkbooks;
theWorkbooks = ExcelObj.Workbooks;

Microsoft.Office.Interop.Excel.Workbook theWorkbook;
theWorkbook = theWorkbooks.Open("c:\\test\\input1.xls", 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindow s, "\t", false, false, 0, true,false, false);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.g et_Item(1);
ColCount = worksheet.UsedRange.Columns.Count;
RowCount = worksheet.UsedRange.Rows.Count;

//Is it really necessary to use worksheet.get_Range to get this
 //to work or have I taken the wrong approach.

Microsoft.Office.Interop.Excel.Range Range1 = worksheet.get_Range("a1","a1");
Range1.Cells[1, 1] = "11";



Similar Threads
Thread Thread Starter Forum Replies Last Post
cells values with validation in another cell srahuf Excel VBA 1 May 30th, 2008 04:05 AM
copy columns based on cell values EricB123 Excel VBA 0 December 18th, 2007 12:51 AM
Exporting cell values to text file vemaju Excel VBA 2 October 1st, 2007 02:30 PM
Reading Values from Excel cell into VB 6 applicati pratlina Beginning VB 6 2 December 28th, 2004 08:28 AM
Use VBA to assign cell values eric_winters Excel VBA 2 September 3rd, 2004 02:27 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.