Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
I have some tables in my Sql server 2007 database I need to copy the contents of the table into an excel sheet by selecting the table name in a dropdownlist.with an event or method through the front end in the asp.net using C# code.plz help me.....
Posted 21-Aug-12 21:15pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Refer to the following link. this could be useful to achieve what you want.
 
Export to EXCEL from Datatable in C#.Net[^]
  Permalink  
Comments
srinath.pothineni at 22-Aug-12 7:31am
   
thankq rahul its useful for me and i got the solution....
Rahul Rajat Singh at 22-Aug-12 7:33am
   
good to hear that. if it is working mark them as solutions so that other will know it is solved and perhaps someone else can also benefit from it.
srinath.pothineni at 22-Aug-12 7:32am
   
one more question rahul i want to generate log files when i create an excel file how can i do this...
Rahul Rajat Singh at 22-Aug-12 7:34am
   
Where do you want to write logs, text file or eventlog?
srinath.pothineni at 22-Aug-12 7:43am
   
sorry for the delay...
i want to write in .txt file...
Rahul Rajat Singh at 22-Aug-12 7:49am
   
writting to text files is simple file handling. you can do it by using static functions of File class. Still i would recommend doing it in proper way. refer this link for details on how this can be done properly.
 

http://www.codeproject.com/Articles/2344/Create-Simple-Error-Log-Files-using-ASP-NET-and-C
 
P.S. mark as solution to indicate the question is solved.
srinath.pothineni at 23-Aug-12 1:38am
   
ThankQ rahul its useful for me.....:-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I hope you get the answer here Export GridView Data to Excel using OpenXml[^]
  Permalink  
Comments
srinath.pothineni at 22-Aug-12 2:34am
   
Thanks Pramod.I want to convert the table data to excel sheet directly with out a gridview.plz send me the exact code...
ythisbug at 22-Aug-12 2:41am
   
@Srinath
simple idea is add column to perticular table and save field as columnTablename in that field add ur table name.and bind to dropdown as columntable name field and click export then u can get details..try this solution to export to excel.
pramodhegde88 at 22-Aug-12 2:45am
   
You can use this.
 
string excelfile = Path.GetTempPath() +
Guid.NewGuid().ToString() + ".xlsx";
using (SpreadsheetDocument excelDoc = SpreadsheetDocument.Create(
excelfile,
DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
CreateExcelParts(excelDoc, table);
}
return excelfile;
 
and 'table' should be of type DataTable. so, all you need to do is, create DataTable from sql and pass it to this method.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Well, try this. Here is the full source to export datatable to excel
 
    public void ExportToExcel(DataTable data, string fileName)
    {
        HttpContext context = HttpContext.Current;
        context.Response.Clear();
 
        context.Response.ContentType = "text/csv";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
 
        //iterate column header names
        for (int i = 0; i < data.Columns.Count; i++)
        {
            if (i > 0)
            {
                context.Response.Write(",");
            }
            context.Response.Write(data.Columns[i].ColumnName);
        }
        context.Response.Write(Environment.NewLine);
 
        //prepare data
        foreach (DataRow row in data.Rows)
        {
 
            for (int i = 0; i < data.Columns.Count; i++)
            {
                if (i > 0)
                {
                    context.Response.Write(",");
                }
                context.Response.Write(row.ItemArray[i].ToString());
            }
            context.Response.Write(Environment.NewLine);
        }
        context.Response.End();
    }
 
    private DataTable getData()
    {
        //return datatable - that you get from database
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        ExportToExcel(getData(), "[customefilename]");
    }
 
 
Hope this helps.
cheers
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Check my article. I have explained how to export datatable to excel with formatting. you have to load datatable from database using procedure/query through c#
 
Export DataTable to Excel with Formatting in C#[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Thanks one and all for your great support.finally i completed the task.its working but it generates .aspx file format it is opening by selecting open with excel option.see the below code and tell me how to generate direct excel file..
 

 
Quote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
 
public partial class DbtoExcel : System.Web.UI.Page
{
SqlConnection cn;
DataGrid dg = null;
static SqlCommand cmd;
SqlDataAdapter da = null;
string cs,excelfile;
SqlDataReader dr;
DataSet ds=null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
cs = WebConfigurationManager.ConnectionStrings["Mydbcon"].ConnectionString;
cn = new SqlConnection(cs);
cn.Open();
cmd = new SqlCommand("select table_name from INFORMATION_SCHEMA.Tables", cn);
dr = cmd.ExecuteReader();
ds = new DataSet();
while (dr.Read())
{
ddtable.Items.Add(dr[0].ToString());
}
dr.Dispose();
cn.Close();
}
catch(Exception ex)
{
lerror.Text = ex.Message;
}
}

}
 
protected void ddtable_SelectedIndexChanged(object sender, EventArgs e)
{
lerror.Text = ddtable.SelectedItem.Text;

}
 
protected void bdownload_Click(object sender, EventArgs e)
{
da=new SqlDataAdapter("select * from (" + ddtable.SelectedItem.Text + ")", cn);
da.Fill(ds,"tab");
dg = new DataGrid();
dg.DataSource = ds;
dg.DataBind();
Response.Clear();
Response.Buffer = true;
Response.ContentType="application/vnd.ms_excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sqlstringwriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter sqlhtmltextwriter = new System.Web.UI.HtmlTextWriter(sqlstringwriter);
dg.RenderControl(sqlhtmltextwriter);
Response.Write(sqlstringwriter.ToString());
Response.End();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Check Out here PDF WORD AND Excel you Can Download . How to convert GridView data to Excel, PDF, Word file using C#[^]
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 22 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100