Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to insert simple data into an Excel file from .Net

Tags:

c#

.net

excel

I have an excel file that has ~10 columns and 1-20 rows. I need to insert 1-20 rows with various data elements.

I was wondering if there was a way I could put some tags in the excel file so they could be found and replaced. Something that marks a column as "Name". That way in code I could just say:

Name[0] = object.name;

I'm not sure if this exact method is possible, but I really don't need any heavy lifting and I rather not hard code the cell locations as the excel file might change over time.

I will also have to add a hidden 'ID' cell in row. I imagine I can cross that bridge later though.

like image 626
PFranchise Avatar asked Aug 27 '12 19:08

PFranchise


People also ask

How do I quickly input data into Excel?

On the worksheet, click a cell. Type the numbers or text that you want to enter, and then press ENTER or TAB. To enter data on a new line within a cell, enter a line break by pressing ALT+ENTER.

Can C# write to Excel?

Open, write, save, and customize Excel files with the easy to use IronXL C# library. Download a sample project from GitHub or use your own, and follow the tutorial. Use the WorkBook. Load method to read any XLS, XLSX or CSV document.


2 Answers

Using ADO.NET is easy to add a row to an Excel Sheet

string fileName = @"D:\test.xlsx"; 
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;" + 
        "Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName); 

using(OleDbConnection cn = new OleDbConnection(connectionString))
{
    cn.Open();
    OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [Sheet1$] " + 
         "([Column1],[Column2],[Column3],[Column4]) " + 
         "VALUES(@value1, @value2, @value3, @value4)", cn);
   cmd1.Parameters.AddWithValue("@value1", "Key1");
   cmd1.Parameters.AddWithValue("@value2", "Sample1");
   cmd1.Parameters.AddWithValue("@value3", 1);
   cmd1.Parameters.AddWithValue("@value4", 9);
   cmd1.ExecuteNonQuery();
}

The code above assumes that you have a first row with an header with Column1... as column names. Also, the code use the ACE OleDB provider for Excel 2007 or 2010 instead of Microsoft.Jet.OleDb.4.0.

EDIT: To refer to a Named Range you could change the sql command to this one

    OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [yourNamedRange] " + 
                        "VALUES(@value1, @value2, @value3, @value4)", cn);

Alas, I can't find a way to refer to the individual columns.

like image 149
Steve Avatar answered Sep 28 '22 00:09

Steve


    private Excel.Application app = null;
    private Excel.Workbook workbook = null;
    private Excel.Worksheet worksheet = null;
    private Excel.Range workSheet_range = null;
    private const int FIRTSCOLUMN= 0 //Here const you will use to select good column
    private const int FIRSTROW= 0
    private const int FIRSTSHEET= 1

    app = new Excel.Application();
    app.Visible = true;
    workbook = app.Workbooks.Add(1);
    worksheet = (Excel.Worksheet)workbook.Sheets[FIRSTSHEET];
    addData(FIRSTROW,FIRTSCOLUMN,"yourdata");


 public void addData(int row, int col, string data)
    {
        worksheet.Cells[row, col] = data;

    }    
like image 27
Hassan Boutougha Avatar answered Sep 27 '22 22:09

Hassan Boutougha