public static void ExportExcel(DataSet ds, string strExcelFileName)
{
object objOpt = System.Reflection.Missing.Value;
Excel.Application excel = new Excel.Application();
excel.Visible = true;
Excel._Workbook wkb = excel.Workbooks.Add(objOpt);
Excel._Worksheet wks = (Excel._Worksheet)wkb.ActiveSheet;
wks.Visible = Excel.XlSheetVisibility.xlSheetVisible;
int rowIndex = 1;
int colIndex = 0;
System.Data.DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
//excel.Sheets[0] = "sss";
wkb.SaveAs(strExcelFileName, objOpt, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
wkb.Close(false, objOpt, objOpt);
excel.Quit();
}
<identity impersonate="true" userName="administrator" password="**********"/>
webconfig中加上