Click here to Skip to main content
15,914,363 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to write text box values to an excel sheet using c#?i want to enter those data to cells by selecting them.please help me.
Posted
Comments
Jegan Thiyagesan 25-Feb-13 8:47am    
What have you searched for?
What have you tried?
Where did you get stuck?
Muthu Karunarathna 25-Feb-13 8:52am    
i have to access excel data and update some cells of it using c# code. i did first part and it went succesful. but i'm stucked in second part of write to cell.how can i write values of a text box to a cell.that's where i stucked.
Jegan Thiyagesan 25-Feb-13 9:02am    
Can you post the code that you are writing to the Cell?
Muthu Karunarathna 25-Feb-13 9:27am    
Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
Excel.Workbook workbook = (Excel.Workbook)excelApp.Workbooks.Add(Missing.Value);
Excel.Worksheet worksheet;

worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);

((Excel.Range)worksheet.Cells["1", "A"]).Value2 = "5";
((Excel.Range)worksheet.Cells["2", "A"]).Value2 = "7";

//in first line they are saying an error "application class can't be embedded"

You can try below code by using this C# Excel[^]library, but it is also a commercial api like Microsoft Excel,it works fine in my project,hope you goodluck:
C#
namespace textboxtoexcel
{
    public partial class Form1 : Form
    {
        private bool stop = false;
        private Workbook workbook = null;
        private Worksheet worksheet = null;
        private int rowIndex = 0;
        private String fileName = null;
        public Form1()
        {
            InitializeComponent();
        }
        private void labelSize_TextChanged(object sender, EventArgs e)
        {
            if (stop)
            {
                return;
            }
            String no = String.Format("No. {0}", rowIndex);
            rowIndex++;
            worksheet[rowIndex, 1].Text = no;
            worksheet[rowIndex, 2].Text = this.labelSize.Text;
        }
        private void InitWorkbook()
        {
            workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            worksheet = workbook.Worksheets[0];
            worksheet.Name = "image size";
            worksheet.Range[1, 1].Text = "No";
            worksheet.Range[1, 2].Text = "Size Data";
            rowIndex = 1;
            fileName = String.Format("image-size-{0}.xls", Guid.NewGuid());
        }
        private void btnDemoStart_Click(object sender, EventArgs e)
        {
            this.btnOpenExcel.Enabled = false;
            this.stop = false;
            this.InitWorkbook();
            Random random = new Random();
            new Thread(() =>
            {
                while(!this.stop)
                {
                    int width = random.Next(1024);
                    int height = random.Next(1024);
                    this.Invoke(new Action(() =>
                    {
                        this.labelSize.Text = String.Format("{0}, {1}", width, height);
                    }));
                    Thread.Sleep(1000);
                }
            }).Start();
        }
        private void btnDemoStop_Click(object sender, EventArgs e)
        {
            this.stop = true;
            this.btnOpenExcel.Enabled = true;
            this.workbook.SaveToFile(fileName);
        }
        private void btnOpenExcel_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(fileName);
        }
    }
}
 
Share this answer
 
v2
Comments
Muthu Karunarathna 25-Feb-13 21:49pm    
Thank you for your help.i will try this.
[no name] 26-Feb-13 20:54pm    
Hope it cna help you!
 
Share this answer
 
Comments
Muthu Karunarathna 25-Feb-13 21:49pm    
Thank you for your help i will try this.
Thomas Daniels 26-Feb-13 12:51pm    
You're welcome!
hi,

Can you try this:
First you must import "Microsoft.Office.Core" and "Microsoft.Office.Interop.Excel" COM references.

Second:
C#
public void WriteToExcel()
{
   string myPath = @"C:\Excel.xls"; // this must be full path.
   FileInfo fi = new FileInfo(myPath);
   if (!fi.Exists)
   {
       Console.Out.WriteLine("file doesn't exists!");
   }
   else
   {
        var excelApp = new Microsoft.Office.Interop.Excel.Application();
        var workbook = excelApp.Workbooks.Open(myPath);
        Worksheet worksheet = workbook.ActiveSheet as Worksheet; 


        Microsoft.Office.Interop.Excel.Range range = worksheet.Cells[1,1] as Range;
        range.Value2 = "5";

        //excelApp.Visible = true;
        workbook.Save();
        workbook.Close();
   }
}


I hope this helps.

Regards
Jegan
 
Share this answer
 
Comments
Muthu Karunarathna 25-Feb-13 21:48pm    
Thank you for your help.i will try this.
Muthu Karunarathna 27-Feb-13 0:20am    
I tried this code..but it gives following error for " Microsoft.Office.Interop.Excel.Range range = worksheet.Cells[1,1] as Range;"
this line..
nullreferenceunhandled..
can you please tell me a solution for that?
Jegan Thiyagesan 27-Feb-13 4:49am    
Means your worksheet is null, means your workbook is null.
Did you give the full path to open the excel file? i.e. C:\blalba\blabla\blabla\Excel.xls.

Jegan
Muthu Karunarathna 28-Feb-13 0:20am    
now it works..but there is another problem when i use this method to write several data to same excel.i want to add several data to a same excel which is exsiting.but when i tried it it always open excel sheet foe each time when i call this method..is there any method to close the excel like excel.close or something?
Muthu Karunarathna 28-Feb-13 0:24am    
public void WriteToExcel(string myPath,string row,string col,string val)
{

FileInfo fi = new FileInfo(myPath);
if (!fi.Exists)
{
MessageBox.Show("file doesn't exists!");
}
else
{
var excelApp = new Microsoft.Office.Interop.Excel.Application();
var workbook = excelApp.Workbooks.Open(myPath);


Excel.Worksheet worksheet;

worksheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);

((Excel.Range)worksheet.Cells[row, col]).Value2 = val;

excelApp.Visible = true;





//workbook.Save();
workbook.Close();


}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900