Click here to Skip to main content
15,885,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Please help me in creating excel sheet or pdf from the data obtained in the bataBase using C#.

Thanks in advance
Posted
Comments
rmksiva 17-Apr-13 2:58am    
May i know , yours Application is windows Application or Web App ?
ravithejag 17-Apr-13 3:03am    
Hi it's an web app

Hi,

If Your are using Web App Means

Use this

protected void btnExport_click(object sender, EventArgs e)
{
   DataTable dt = objDataLayer.getEmployeeDataTable();
   ExporttoExcel(dt,"EmployeeList")
}

 private void ExporttoExcel(DataTable table,string filename)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Buffer = true;
            //  HttpContext.Current.Response.ContentType = "application/ms-excel";//2003 format
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            HttpContext.Current.Response.Write(@");
            //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=InvoiceList.xls");// 2003 format
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+ filename +".xls");

            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
            HttpContext.Current.Response.Write("<font style="font-size:12.0pt; font-family:Calibri;">");
            HttpContext.Current.Response.Write("<br><br><br>");
            HttpContext.Current.Response.Write("<table border="1" bgcolor="#ffffff" bordercolor="#000000" cellspacing="2px" cellpadding="2px" style="font-size:12.0pt; font-family:Calibri; background:white;"> <tr>");

            //  int columnscount = grdInvoice.Columns.Count;

            int columnscount = table.Columns.Count;

            for (int j = 0; j < columnscount; j++)
            {
                HttpContext.Current.Response.Write("<td>");
                HttpContext.Current.Response.Write("");
                // HttpContext.Current.Response.Write(grdInvoice.Columns[j].HeaderText.ToString());
                HttpContext.Current.Response.Write(table.Columns[j].ColumnName);
                HttpContext.Current.Response.Write("");
                HttpContext.Current.Response.Write("</td>");
            }
            HttpContext.Current.Response.Write("</tr>");
            foreach (DataRow row in table.Rows)
            {
                HttpContext.Current.Response.Write("<tr>");
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write("<td>");
                    HttpContext.Current.Response.Write(row[i].ToString());
                    HttpContext.Current.Response.Write("</td>");
                }

                HttpContext.Current.Response.Write("</tr>");
            }
            HttpContext.Current.Response.Write("</table>");
            HttpContext.Current.Response.Write("</br></br></br></font>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }


it will write exported excel file to response - Means File being download when you click export
 
Share this answer
 
Hi
You can try this solution http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm[^]

It should help i found it useful when i read from cells within excel file and write them into a database and i used that to, write down records from a database query to an excel file. It helped me out a lot. I hope it will do the same for you too.
 
Share this answer
 
Hi the code windows form

C#
private void btnExport_Click(object sender, EventArgs e)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets.Add("Sample");
            WorksheetRow Wrow = sheet.Table.Rows.Add();
            for (int i = 1; i < dgvTasks.Columns.Count; i++)
            {
                sheet.Table.Columns.Add(new WorksheetColumn(100));
                if (dgvTasks.Columns[i].Visible != false)
                {
                    Wrow.Cells.Add(new WorksheetCell(dgvTasks.Columns[i].HeaderText.ToString()));
                }
            }
            // storing Each row and column value to excel sheet 

            for (int i = 1; i <= dgvTasks.Rows.Count; i++)
            {
                WorksheetRow wrow = sheet.Table.Rows.Add();

                for (int j = 1; j < dgvTasks.Columns.Count; j++)
                {
                    if (dgvTasks.Columns[j].Visible != false)
                    {
                        wrow.Cells.Add(dgvTasks.Rows[i - 1].Cells[j].Value.ToString());
                    }
                }
            }

            // save the application 
            try
            {
                Random R = new Random();
                int num = R.Next();
                string strFileName = "c:\\output" + num.ToString() + ".xls";
                saveFD.Filter = "Excel files (*.xls)|*.xls|txt files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFD.FilterIndex = 1;
                saveFD.Title = "Save file";
                saveFD.RestoreDirectory = true;

                if (saveFD.ShowDialog() == DialogResult.OK)
                {
                    strFileName = saveFD.FileName;

                }
                book.Save(strFileName.ToString());

            }
            catch
            {
            }
        }
 
Share this answer
 
click here hope it will help you
 
Share this answer
 
Hello,

Please refer this article: C# - Create Excel Report (Chart Contained) With Mark Designer.

It presents how to generate a report by getting data from database to Excel.
 
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