Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I want to create one excel file from my data table. Then i need to save it on my folder (I need to select folder programmatically).

How can i do this ?
Posted

DataSet to Excel File Conversion using ExcelLibrary[^]

Use above link to get solution.

Please mark as answer if you are satisfied.

Thanks,
Vipul
 
Share this answer
 
v2
Comments
Am Gayathri 16-Oct-14 6:24am    
But i need c# code not vb.
[no name] 16-Oct-14 6:32am    
Hi Kavitha,
Convert to C# here http://www.developerfusion.com/tools/convert/vb-to-csharp/
Am Gayathri 17-Oct-14 2:29am    
Thanks

Praneet could you please answer this qustion
http://www.codeproject.com/Questions/830403/Can-I-Save-Excel-File-In-My-Local-Folder-Automatic?arn=0
Hi,

The way I do it is use DataTables WriteXml method.

For e.g.
C#
DataTable dt = new DataTable("Test");
            dt.Columns.Add("Code", typeof(string));
            dt.Columns.Add("Price", typeof(int));

            DataRow dr = dt.NewRow();
            dr["Code"] = "ABC";
            dr["Price"] = 12;

            dt.Rows.Add(dr);

            DataRow dr1 = dt.NewRow();
            dr1["Code"] = "PQR";
            dr1["Price"] = 1;

            dt.Rows.Add(dr1);

            dt.WriteXml("D:/Test.xls");


You can pass your path to writexml here.

Regards,
Praneet
 
Share this answer
 
Comments
Am Gayathri 16-Oct-14 7:31am    
Thanks, but am getting access denied error but i have full control
What is the solution ?
[no name] 16-Oct-14 7:39am    
Go to the properties of the root folder where you are generating the excel file.
Uncheck if it is read only, and see that the rights to that folder are as required.
Am Gayathri 16-Oct-14 7:52am    
Now some other error
Cannot serialize the DataTable. DataTable name is not set.
Need your help Praneet.I guess you would have faced this issue.
[no name] 16-Oct-14 7:56am    
You should initialize the datatable as
DataTable dt = new DataTable("TableName");

Or

DataTable dt = new DataTable();
dt.TableName = "TableName";

This should work.

Regards,
Praneet
Am Gayathri 16-Oct-14 9:35am    
Its working...but another problem
when i open that excel am getting some pop up to choose opening method, i selected xml but the column heading is not coming properly.
Eg : if column head contains space its coming like %20.
If data in data table contain <br/> then there should be a line break. but its not happening here. PLz help
Hi,

I think using interop can be better. This is a very good example.:
Export DataSet into Excel using C# Excel Interop[^]

Hope this will help.

Regards,
Praneet
 
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