Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old November 16th, 2007, 11:12 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
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()
{
InitializeComponent();
}

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";


}

}
}

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
cells values with validation in another cell srahuf Excel VBA 1 May 30th, 2008 05: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 03: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 03:27 PM



All times are GMT -4. The time now is 07:18 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.