Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi...
i wan to pass value in excel using asp.net

i try below code but i want to pass cell no and data insert in perticular cell i try below code but in this code i must specifiy column in place i need and cell no in which i add value so any modification in my code or any other code to do this please tell me.
C#
string szConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MySamplefile.xls;Extended Properties='Excel 8.0;HDR=No'";
       OleDbConnection conn = new OleDbConnection(szConn);
       conn.Open();
       OleDbCommand cmd = new OleDbCommand();
       cmd = new OleDbCommand("INSERT INTO [sheet2$](columnName1,columnName2) VALUES ('A3','B3')", conn);
       cmd.ExecuteNonQuery();
       conn.Close();


- in place of column name columnName1,columnName2 i want to give any Cell

thanks
Posted
Updated 19-Mar-13 1:09am
v2
Comments
CHill60 19-Mar-13 6:17am    
I believe that this can't be done, oledb will just go to the next row for the specified column
ZurdoDev 19-Mar-13 7:09am    
I cannot understand what you need. Can you be more clear please?
bhavesh002 19-Mar-13 7:25am    
string szConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MySamplefile.xls;Extended Properties='Excel 8.0;HDR=No'";
OleDbConnection conn = new OleDbConnection(szConn);
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd = new OleDbCommand("INSERT INTO [sheet2$](columnName1,columnName2) VALUES ('A3','B3')", conn);
cmd.ExecuteNonQuery();
conn.Close();
in this Example inplace of columnName1 and columnName2 i want to pass any Cell no like A1,B1 and any other cell
ZurdoDev 19-Mar-13 7:31am    
You can try Update [Sheet1$A1:A15] SET A15 = 'DesiredNumber'
see http://stackoverflow.com/questions/4718171/using-oledb-to-insert-data-into-excel-2007-in-c-sharp Also look at http://support.microsoft.com/kb/316934

1 solution

Inserting data into Excel Sheet with OLEDB[^] is provided for tables, arrays.

If you want to add some values into particular cells, for example: A1, C5, E9, you need to:
1) Create instance of Excel,
'late binding
VB
xlApp = CreateObject("Excel.Application")

2) Open workbook,
VB
xlWbk = xlApp.Workbooks.Open("FullFileName")

3) Add data into particular cells:
VB
xlWsh = xlWbk.Worksheets("Sheet1")
xlWsh.Range("A1") = "String Value"
xlWsh.Range("C5") = 2005 'numeric value
xlWsh.Range("E9") = "Client No. 203584"


Above code is in VB.NET. Examples in C#:
http://www.c-sharpcorner.com/uploadfile/ae35ca/read-and-write-excel-data-using-C-Sharp/[^]
http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm[^]
on CP KB:
Write Data to Excel using C#[^]
Excel Reader[^]
 
Share this answer
 

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