if (dt = = null)
{
Return "";
}
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application ();
if (xlapp = = null)
{
Return "Cannot create Excel object, may not have Excel installed on your machine";
}
Microsoft.Office.Interop.Excel.Workbooks Workbooks = xlapp.workbooks;
Microsoft.Office.Interop.Excel.Workbook Workbook = workbooks. ADD (Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet Worksheet = (Microsoft.Office.Interop.Excel.Worksheet) workbook. worksheets[1];//Get Sheet1
Microsoft.Office.Interop.Excel.Range Range;
Long totalcount = dt. Rows.Count;
Long rowread = 0;
float percent = 0;
string[] columnlist = columns. Split (New char[]{', '},stringsplitoptions.removeemptyentries);
Write fields
for (int i = 0; i < columnlist.length; i++)
{
Worksheet. Cells[1, i + 1] = Columnlist[i];
Range = (Microsoft.Office.Interop.Excel.Range) worksheet. Cells[1, i + 1];
Range. Interior.ColorIndex = 15;
Range. Font.Bold = true;
}
Write value
for (int r = 0; r < dt. Rows.Count; r++)
{
for (int i = 0; i < dt. Columns.count; i++)
{
Worksheet. Cells[r + 2, i + 1] = dt. Rows[r][i];
}
rowread++;
Percent = ((float) (rowread))/totalcount;
}
Xlapp.visible = true;
ASP. NET C # export Excel data