the code tries to export the table memberinfo to excel.
Export to excel
Hi guys, am new with C# but try to export a table named memberinfo to excel.
I had this code from the net but have sat on it for three hours without being able
to make it work. First i want to know what to reference and then the code to use to
export to excel. Please if possible try to comment on the code below. I am kind of got
used to it or can give a different one all together. Please help me.
[code]
private void btnexportfile_Click(object sender, EventArgs e)
{
Excel.ApplicationClass excel = new ApplicationClass();
excel.Application.Workbooks.Add(true);
DataTable table = memberinfo.Tables[0];
int ColumnIndex = 0;
foreach (Datacolumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[1, ColumnIndex] = col.ColumnName;
}
int rowIndex = 0;
foreach (DataRow row in table.Rows)
{
rowIndex++;
ColumnIndex = 0;
foreach (DataColumn col in table.Columns)
{
ColumnIndex++;
excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.ColumnName].Text;
}
}
excel.Visible = true;
Worksheet worksheet = (Worksheet)excel.ActiveSheet;
worksheet.Activate();
}
[/code]
Please the are with errors
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.