Set Excel Cell value using C#

update cell value in excel using c#
update excel sheet using c# interop
c# excel get cell value by column name
how to write text in excel sheet using c#
c# excel insert value into cell
how to add new column in existing excel using c#
c# edit excel file programmatically
how to select particular column in excel using c#

I use the following code to set the cell value of an Excel file:

for (int i = 2; i < rowCount; i++)
{
    for (int j = 1; j < columnCount; j++)
    {
        worksheet.Cells[i, j].Value = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
    }
 }

However I keep getting the

Error: Exception from HRESULT: 0x800A03EC

Why do I get this error and how do I fix it?


Update:

  try
            {

                saveFileDialog1.Title = "Save as Excel File";
                saveFileDialog1.FileName = "";
                saveFileDialog1.Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xls";
                if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    Microsoft.Office.Interop.Excel.Application worksheet = new Microsoft.Office.Interop.Excel.Application();
                    worksheet.Application.Workbooks.Add(Type.Missing);
                    worksheet.Columns.ColumnWidth = 20;
                    for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
                    {
                        worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

                    }
                    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j < dataGridView1.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                        }

                    }
                    worksheet.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());
                    worksheet.ActiveWorkbook.Saved = true;
                    worksheet.Quit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

How to write value to a single MS excel cell from c# code?, value "my value" in cell "C24" to an already existing MS excel sheet. to only search for excel files (had trouble setting this in design view). Set Excel cells value using cell name in C# Introduction. The template can be modified from the user, changing the cell's position, Desiderata. I want to have the possibility to define a cell name equal to Solution. To get the desired result, we must use the Evaluate () method, Define


I usually use Value2 property (not Value)

So, your code would be as the following:

for (int i = 2; i < rowCount; i++)
{
    for (int j = 1; j < columnCount; j++)
    {
        worksheet.Cells[i, j].Value2 = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
    }
 }

Set Excel cells value using cell name in C#, Automate the process of set value to Excel cells, assigning a cell name that matches the query result column name, leaving the end user the  Export Excel file from ASP.NET application how to create and download excel file in asp.net using c#, vb.net or create & download excel file from datatable in asp.net. C#: How can I convert an RTF file to a pdf file. effortlessly convert RTF to PDF in your application within this section, enables you to directly load file with .rtf entension and then convert RTF to PDF.


https://www.codeproject.com/Questions/470089/Exception-from-HRESULT-x-A-EC-Error

It seems, that you are not the only one: http://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range[^].

According to the post, this error is occurring when you work with an old (xls) workbook opened in Excel 2007 or 2010. If this is the case, try first to save the file as new format workbook, before you access the cells.

How to edit the cell content of excel file (.xlsx) in C#, A lot of errors you've been made Please, read my comments: Why to use methods of Path class, if you can initiate variables directly? A drive  Excel C# interop - obtaining values from cells Posted in Computing , Programming By FraserG On August 24, 2016 Working with C# and Excel has been discussed on this site before with tips on preventing Excel.exe from hanging and converting integers into Excel column headings .


try this code

for (int i = 1; i < columnCount + 1; i++)
                    {
                        worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

                    }



 for (int i = 0; i < rowCount -1; i++)
    {
        for (int j = 0; j < columnCount; j++)
        {
            worksheet.Cells[i+2, j+1].Value2 = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
        }
     }

how to insert values into cells in excel programatically C#, Hi there Im trying to pass 8 string values in to a row in an excel Try this method to set Excel value in C#, please check full article here - Edit  This article demonstrates how to automate Microsoft Excel by using Microsoft Visual C# 2005 or Microsoft Visual C# .NET to fill and retrieve values in a multi-cell range by using arrays. To fill a multi-cell range without populating cells one at a time, you can set the Value property of a Range object to a two-dimensional array.


When I write exporting into xls using wpf framework in C#. I also encounter with this issue and I solve it with following code.Try to see from this code

 Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            Microsoft.Office.Interop.Excel._Workbook workb = app.Workbooks.Open("c:\\Users\\Jack\\Documents\\Document1.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            worksheet = workb.Sheets[1];
           // worksheet = workbook.Sheets[1];
            //worksheet = workbook.ActiveSheet;
            //worksheet.Name = "birinchi";




            for (int i = 0; i < Table.Columns.Count; i++) 
            {
             //   worksheet.Cells[1, i+1] = Table.Columns[i].Header;

            }


                for (int i = 0; i < Table.Columns.Count; i++)
                {

                    for (int j = 0; j < Table.Items.Count; j++)
                    {
                        TextBlock b = Table.Columns[i].GetCellContent(Table.Items[j]) as TextBlock;
                        worksheet.Cells[j + 2, i + 1] = b.Text;
                        //MessageBox.Show(b.Text);

                    }
                }

                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.FileName = "Document";
                dlg.DefaultExt = ".xlsx";
                Nullable<bool> result = dlg.ShowDialog();
                if (result == true)
                {

                   workbook.SaveAs(dlg.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);



                }
                app.Quit();

Working with Excel cell values, formulas and formatting: C# code , You'll need to get the Range object for the cells which formula you wish to set and then set its Formula property: Excel  its simple i use OleDbCommand to send request to excel file ([Feuil1$] is name of sheet excel - d is name of colums ) then i use the ExecuteNonQuery to execute my request – Badr Bouaichi Nov 1 '19 at 10:38


C# How To Read And Write Microsoft Excel Documents, 3) The following snippet of code shows you an example how to read and change cells' values in Excel files. using Excel = Microsoft.Office.Interop.Excel; Excel. I want to retrieve the background color of cell of excel using c# but I just can't find a way to do it which library shall I use ? microsoft.office.tools.excel.workbook.aspx or microsoft.office.in


Store & retrieve date values in Excel ranges programmatically , How to: Programmatically store and retrieve date values in Excel ranges. 02/02/​2017; 2 minutes to read value in a named range. Create a NamedRange control at cell A1. C# Copy. Microsoft.Office.Tools.Excel.NamedRange NamedRange1 = this. Set today's date as the value for NamedRange1 . C# Copy. DateTime dt  Dim rng As Excel.Range = Me.Application.Range("A1") Set today's date as the value for rng. DateTime dt = DateTime.Now; rng.Value2 = dt; Dim dt As DateTime = DateTime.Now rng.Value2 = dt To retrieve a date value from a native Excel range object. Retrieve the date value from rng.


Automate Excel by using Visual C# to fill or to obtain data by using , NET to fill and retrieve values in a multi-cell range by using arrays. without populating cells one at a time, you can set the Value property of a  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.