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..
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
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();