Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
if (txtbatch.Text != "" && txtp.Text == "1")
    {
        DataColumnCollection dcc = dt.Columns;
        Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
        ExcelApp.Application.Workbooks.Add(Type.Missing);
        for (int i = 1; i < dt.Rows.Count + 1; i++)
        {
            for (int j = 1; j < dt.Columns.Count + 1; j++)
            {
                if (i == 1)
                {
                    ExcelApp.Cells[i, j] = dcc[j - 1].ToString();
                }
                else { ExcelApp.Cells[i, j] = dt.Rows[i - 1][j - 1].ToString();
                }

            }
        }
        ExcelApp.ActiveWorkbook.SaveCopyAs("D:\\Export\\StudentRegistation.xls");
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();
        lbl2.Visible = true;
        lbl2.Text = "Students Registation File Pilot1 Successfuly Downloaded...!";
    }
    else
    {
        lbl2.Text = "No Record found...!";
    }
}


What I have tried:

Using this code i am not able to get first record in excel sheet rest of the record i am getting i am not able to understand can any one help me
Posted
Updated 11-Dec-18 4:43am

You could use the EPPlus library, this does not not need any dependencies except .NET:
GitHub - JanKallman/EPPlus: Create advanced Excel spreadsheets using .NET[^]
 
Share this answer
 
Quote:
Using this code i am not able to get first record in excel sheet


You are not getting the first record because your loop starts at 1 as shown in the following:

C#
for (int j = 1; j < dt.Columns.Count + 1; j++)


and then you have the following code inside it:

C#
ExcelApp.Cells[i, j] = dcc[j - 1].ToString();


Keep in mind that index of Cells starts at 0 so I think that causes the issue why you don't see the first record. Try something like this instead:
C#
if (i == 1)
                {
                    ExcelApp.Cells[i-1, j-1] = dcc[j - 1].ToString();
                }
                else { ExcelApp.Cells[i-1, j-1] = dt.Rows[i - 1][j - 1].ToString();
                }


Or you could change your loop to start at 0 instead of 1 so your code would now look something like this:
C#
for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                if (i == 0)
                {
                    ExcelApp.Cells[i, j] = dcc[j].ToString();
                }
                else { ExcelApp.Cells[i, j] = dt.Rows[i][j].ToString();
                }

            }
        }


If you are still having an unexpected results, I would suggest you to debug your code, set a break point to your for-loop code and then step into it to figure out what the data is being transported.
 
Share this answer
 
v2
You've tagged this question as ASP.NET, which means your code has several problems.

Firstly, you cannot use Office Interop from ASP.NET:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

There are various ways to create Excel spreadsheets on the server without using Office interop. For example:


Secondly, your code is saving the Excel file on the server. It is NOT downloading the file to the client. It might appear to work when you test it in Visual Studio, but that's only because the client and server are the same computer in that specific case. Once you deploy your code to a real server, you'll end up with lots of files stored on the server, and lots of confused users who can't find the file that your code says has just been downloaded.

To send the file to the client, you need to write it to the response, along with the correct content-type header. The user will then be able to choose what to do with the file.

NB: You cannot send any other content in the response.

NB: You cannot control where, or even if, the user saves the file.
 
Share this answer
 
Better use:

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
................

}
}

or look here: Mikes Knowledge Base - Export to Excel[^]
 
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