Hi PRA,
Please use below code.
C#
private void GeData()
{
using (SqlConnection con = new SqlConnection(userData))
{
SqlCommand cmd = con.CreateCommand();
string sql = @"SELECT Row_Number() over (Order by CustomerId)as NID,Name,Name as LastName,Name as Birthday,Country as Address FROM Customers ORDER BY NID";
cmd.CommandText = sql;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter();
try
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
string path = Server.MapPath("exportedfiles\\");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
File.Delete(path + "MutilationSheet.xlsx");
Excel.Application xlAppToExport = new Excel.Application();
xlAppToExport.Workbooks.Add("");
Excel.Worksheet xlWorkSheetToExport = default(Excel.Worksheet);
xlWorkSheetToExport = (Excel.Worksheet)xlAppToExport.Sheets["Sheet1"];
int iRowCnt = 5;
xlWorkSheetToExport.Cells[1, 1] = "Rustam";
xlWorkSheetToExport.Cells[2, 1] = "Pulodov";
xlWorkSheetToExport.Cells[1, 3] = "22.12.1987";
xlWorkSheetToExport.Cells[2, 4] = "Dushanbe";
Excel.Range range = xlWorkSheetToExport.Cells[1, 1] as Excel.Range;
xlWorkSheetToExport.Cells[iRowCnt - 1, 1] = "NID";
xlWorkSheetToExport.Cells[iRowCnt - 1, 2] = "LastName";
xlWorkSheetToExport.Cells[iRowCnt - 1, 3] = "Name";
xlWorkSheetToExport.Cells[iRowCnt - 1, 4] = "Birthday";
xlWorkSheetToExport.Cells[iRowCnt - 1, 5] = "Address";
int i;
for (i = 0; i <= dt.Rows.Count - 1; i++)
{
xlWorkSheetToExport.Cells[iRowCnt, 1] = dt.Rows[i].Field<Int64>("NID");
xlWorkSheetToExport.Cells[iRowCnt, 2] = dt.Rows[i].Field<string>("LastName");
xlWorkSheetToExport.Cells[iRowCnt, 3] = dt.Rows[i].Field<string>("Name");
xlWorkSheetToExport.Cells[iRowCnt, 4] = dt.Rows[i].Field<string>("Birthday");
xlWorkSheetToExport.Cells[iRowCnt, 5] = dt.Rows[i].Field<string>("Address");
iRowCnt = iRowCnt + 1;
}
Excel.Range range1 = xlAppToExport.ActiveCell.Worksheet.Cells[11, 1] as Excel.Range;
xlWorkSheetToExport.SaveAs(path + "MutilationSheet.xlsx");
xlAppToExport.Workbooks.Close();
xlAppToExport.Quit();
xlAppToExport = null;
xlWorkSheetToExport = null;
lblConfirm.Text = "Data Exported Successfully";
lblConfirm.Attributes.Add("style", "color:green; font: normal 14px Palatino Linotype;");
btView.Attributes.Add("style", "display:none");
btDownLoadFile.Attributes.Add("style", "display:block");
}
}
catch (Exception ex)
{
lblConfirm.Text = ex.Message.ToString();
lblConfirm.Attributes.Add("style", "color:red; font: bold 14px/16px Palatino Linotype");
}
finally
{
sda.Dispose();
sda = null;
con.Close();
}
}
}
Screenshot
A |
B |
C |
D |
E |
Rustam |
|
22.12.1987 |
|
|
Pulodov |
|
|
Dushanbe |
|
|
|
|
|
|
NID |
LastName |
Name |
Birthday |
Address |
1 |
Mudassar Khan |
Mudassar Khan |
Mudassar Khan |
India |
2 |
Maria |
Maria |
Maria |
Austria |
3 |
Ana Trujillo |
Ana Trujillo |
Ana Trujillo |
France |
4 |
Antonio Moreno |
Antonio Moreno |
Antonio Moreno |
Brazil |
5 |
Christina Berglund |
Christina Berglund |
Christina Berglund |
Ireland |
6 |
John |
John |
John |
Uk |