If use workbook object then unable kill the excel process
code as follows:
public void CreateExcel(DataView dv,string path,string title1)
{
Excel.Application excel;
try
{
excel =new Excel.Application();
}
catch
{
Response.Write("·þÎñÆ÷¶ËExcel·±Ã¦£¡ÇëÉÔºó!");
return;
}
object oMissing =System.Reflection.Missing.Value;
excel.UserControl =false;
excel.DisplayAlerts=false;
excel.AlertBeforeOverwriting=false;
Excel._Workbook book =excel.Workbooks.Add(oMissing);
int rowIndex =2;
int colIndex =0;
excel.Cells[1,1] =title1;
foreach(DataColumn dc in dv.Table.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex] =dc.ColumnName;
((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =8.8;
}
foreach(DataRow row in dv.Table.Rows)
{
rowIndex++;
colIndex =0;
foreach(DataColumn dc in dv.Table.Columns)
{
colIndex ++;
excel.Cells[rowIndex,colIndex] = row[dc.ColumnName].ToString();
((Excel.Range)excel.Cells[rowIndex,colIndex]).ColumnWidth =8.8;
}
}
Excel.Sheets sheets =book.Worksheets;
Excel._Worksheet worksheet =(Excel._Worksheet)sheets.get_Item(1);
Excel.Range range;
range =worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,14]);
range.MergeCells =true;//ºÏ²¢
range.HorizontalAlignment =Excel.XlHAlign.xlHAlignCenter;//¾ÓÖÐ
range.Font.Bold =true;
range.Font.Size =14;
//»_Ïß
range =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[rowIndex,colIndex]);
range.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle =Excel.XlLineStyle.xlLineStyleNone;
range.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle =Excel.XlLineStyle.xlLineStyleNone;
range.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle =Excel.XlLineStyle.xlContinuous;
//À_¿í±í¸ñ
range =worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[1,1]);
range.ColumnWidth =25.8;
range =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[rowIndex,1]);
range.Font.Bold =true;
range =worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[2,colIndex]);
range.Font.Bold =true;
book.Saved =true;
worksheet.SaveAs(path,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);
this.NAR(oMissing);
this.NAR(sheets);
this.NAR(worksheet);
book.Close(false,null,null);
this.NAR(book);
excel.Quit();
this.NAR(excel);
GC.Collect();
}
private void NAR(object o)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch{}
o=null;
}