![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
导出 Excel 文件#region 导出 Excel 文件
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///// <summary>
/// 导出 Excel 文件
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名</param>
private void ExportExcel(DataSet ds,string strExcelFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
object objOpt = Missing.Value;
Application excel = new Application();
excel.Visible = true;
_Workbook wkb = excel.Workbooks.Add(objOpt);
_Worksheet wks = (_Worksheet)wkb.ActiveSheet;
![](/Images/OutliningIndicators/InBlock.gif)
wks.Visible = XlSheetVisibility.xlSheetVisible;
int rowIndex=1;
int colIndex=0;
![](/Images/OutliningIndicators/InBlock.gif)
DataTable table=ds.Tables[0] ;
foreach(DataColumn col in table.Columns)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
colIndex++;
excel.Cells[1,colIndex]=col.ColumnName;
}
![](/Images/OutliningIndicators/InBlock.gif)
foreach(DataRow row in table.Rows)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rowIndex++;
colIndex=0;
foreach(DataColumn col in table.Columns)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
//excel.Sheets[0] = "sss";
wkb.SaveAs(strExcelFileName,objOpt,null,null,false,false,XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
wkb.Close(false,objOpt,objOpt);
excel.Quit();
}
#endregion
![](/Images/OutliningIndicators/None.gif)