Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
Microsoft.Office.Interop.Excel._Application ExcelApp = new Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);


// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 1; i++)
{
   
    for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
    {
        if (i == 1)

            ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();
            
        else

            ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 1][j - 1].ToString();

        

    }
}
ExcelApp.ActiveWorkbook.SaveCopyAs("E:\\1.xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Data Exported Successfully");


Its generated Successfully but need to put Column and range wise like A1 B2 and vertically


Format is given below. I want in This format.

header				L	U	R
						
						
Sr.no	A	B	C	1	1	1
1	Particular A	A	11	1	0	0
2	Particular A	B	12	5	0	0
	Particular A			7	1	1
						
3	Particular B	A	13	5	1	0
4	Particular B	B	14	0	0	3
	Particular B			5	1	3
Posted
Updated 4-Jul-14 1:00am
v2

Install-Package ClosedXML
Install-Package EnterpriseLibrary.Data

new XLWorkbook
{
Worksheets = { new DatabaseProviderFactory().Create("DatabaseContext").ExecuteDataSet(new SqlCommand("SELECT * FROM Bilhete WHERE NOT OlxId IS NULL")).Tables[0] }
}.SaveAs($"%WEBROOT_PATH%\\relatorios\\{DateTime.Today.ToString("yyyy-MM-dd")}.xlsx");
 
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