0
List<Table> t = (List<Table>)GenerateLayout(dtDPList, dtBOList, dpl);

In the list i am getting the 3 Webcontrol table and i want to export those webconrol tables to excel.. so please help me..

1
  • Fixed code indent. Commented Jul 5, 2017 at 12:03

2 Answers 2

0

You can use the below generic method to convert datatable to excel :

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelDemo
{
    public class ExcelUtility
    {
    public static void CreateExcel(DataSet ds, string excelPath)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;

        try
        {
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                {
                    xlWorkSheet.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                }
            }

            xlWorkBook.SaveAs(excelPath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlApp);
            releaseObject(xlWorkBook);
            releaseObject(xlWorkSheet);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private static void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch
        {
            obj = null;
        }
        finally
        {
            GC.Collect();
        }
    } 
}

}

You can directly apply the style properties to a cell as given below:

workSheet.Cells[2, 1].Font.Bold = true

You can find more style tags using the link below:

http://csharp.net-informations.com/excel/csharp-format-excel.htm

Sign up to request clarification or add additional context in comments.

4 Comments

for (int i = 0; i < t[0].Rows.Count; i++) { for (int j = 0; j < t[0].Rows[i].Cells.Count; j++) { xlWorkSheet.Cells[i + 1, j + 1] = t[0].Rows[i].Cells[j].Text; } }
Brother, How to apply styles?? I M Using xlWorkSheet.Rows[i].Style = t[0].Rows[i].Style; in the for loop but its showing error, so please help me..
You need to create a style object and then needs to define style properties. And then you can assign style object to workbook. Please see the update answer. Hope it will help you.
0

List list = GenerateLayout(dt1,dt2,dpl); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment;filename=DeliveryPickList.xls"); Response.AddHeader("Content-Type", "application/vnd.ms-excel");

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                list[0].RenderControl(htw);
                TextWriter output= Response.Output;
                output.Write(sw.ToString());
            }
        }
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                list[1].RenderControl(htw);
                TextWriter output = Response.Output;
                output.Write(sw.ToString());
            }
        }
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                list[2].RenderControl(htw);
                TextWriter output = Response.Output;
                output.Write(sw.ToString());
            }
        }
        Response.End();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.