#region 保存成Excel ///
/// 保存成Excel /// /////////
/// public static int Code2Excel(DataSet ds, String FileName) { int Result = 0; String Path = ConfigurationSettings.AppSettings["DownLoadPath"] + FileName + ".xls"; try { //创建Application对象 Excel.Application xApp = new Excel.ApplicationClass(); xApp.Visible = false; Excel.Workbook xBook = xApp.Workbooks.Add(Missing.Value);//新建文件的代码 //指定要操作的Sheet Excel.Worksheet xSheet = new Excel.Worksheet(); xSheet = (Excel.Worksheet)xBook.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); xSheet.Name = "点卡"; xSheet.Cells[1, 1] = "卡号"; xSheet.Cells[1, 2] = "是否激活"; for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { if (j == 1) xSheet.Cells[i + 2, j + 1] = "'" + ds.Tables[0].Rows[i][j].ToString(); else xSheet.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString(); } } if (File.Exists(Path)) File.Delete(Path); //保存方式一:保存WorkBook xBook.SaveAs(Path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange, 1, true, Missing.Value, Missing.Value, Missing.Value); xSheet = null; xBook.Close(Missing.Value, Missing.Value, Missing.Value); xBook = null; xApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出 xApp = null; } catch (System.Exception e) { Result = 1; } return Result; } #endregion