Create Excel Sheet daynamicaly - C# / asp.NET
// you just need to import below namespaces
using System.IO;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
//
your_method()
{
DataTable dt = new DataTable();// Imagine you have 2 data column in the data table...
dt.DataSource = passedTable;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook wb = xlApp.Application.Workbooks.Add(true);
xlApp.Visible = true;
Excel.Worksheet ws = (Excel.Worksheet)xlApp.ActiveSheet;
ws.Activate();
xlApp.Cells[1,1] = dt.Rows[0][0].ToString();
using System.IO;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
//
your_method()
{
DataTable dt = new DataTable();// Imagine you have 2 data column in the data table...
dt.DataSource = passedTable;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook wb = xlApp.Application.Workbooks.Add(true);
xlApp.Visible = true;
Excel.Worksheet ws = (Excel.Worksheet)xlApp.ActiveSheet;
ws.Activate();
xlApp.Cells[1,1] = dt.Rows[0][0].ToString();
// Keep in mind that Excel
//index counting is starting from row index 1 and column index 1.
//Therefore it is little bit differ form a data table.
// data table row count starts from 0 as well as column.
//Finally you can save it...
wb.SaveAs(repName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Also if you want to create a cell range in your excel sheet then you can create it as below
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, 5]);
// Them if you want to give a cell border to that range it is below..
rangeSub_ColumnName.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
//Also if you want to bold the font color as below,
rangeSubColumnHeadings.Font.Bold = true;
}
//index counting is starting from row index 1 and column index 1.
//Therefore it is little bit differ form a data table.
// data table row count starts from 0 as well as column.
//Finally you can save it...
wb.SaveAs(repName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
// Also if you want to create a cell range in your excel sheet then you can create it as below
Excel.Range range = xlApp.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, 5]);
// Them if you want to give a cell border to that range it is below..
rangeSub_ColumnName.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
//Also if you want to bold the font color as below,
rangeSubColumnHeadings.Font.Bold = true;
}
Comments
Post a Comment