很简单的一个程序,就是要删除excel文件中的指定sheet. 如果该excel文件只有一个sheet,那么将该sheet更名为"Test",否则删除该sheet. 现在运行的情况是,如果excel中不只一个sheet,则删除成功,结束后,服务器端的excel进程自动结束. 但是,如果只有一个sheet,改完名后,excel进程还在内存中. 虽然打开excel文件查看,唯一的sheet已经更名为Test.问题出在哪里????
/// <summary>
/// 从当前打开的excel中删除一个sheet
/// </summary>
/// <param name="strSheetName">要删除的sheet的名称</param>
/// <returns>删除成功与否</returns>
public static bool DeleteWorksheet(string strFileName,string strSheetName)
{
if(! (System.IO.File.Exists(strFileName)) )
{
return false;//如果Excel文件不存在,直接返回
}
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Workbook workbook = ExcelApp.Workbooks.Open(strFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //ExcelApp.DisplayAlerts = false;
try
{
if ((ExcelApp != null) && (ExcelApp.ActiveWorkbook != null))
{
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets;
int iCnt = sheets.Count; if (iCnt == 1)
{
Microsoft.Office.Interop.Excel._Worksheet wksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1);
wksheet.Name = "Test";
}else{
for (int i = 0; i < iCnt; i++)
{
Microsoft.Office.Interop.Excel._Worksheet wksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(i + 1);
if (wksheet.Name == strSheetName)
{
wksheet.Delete();
break;
}
}
} }
//ExcelApp.DisplayAlerts = true;
ExcelApp.ActiveWorkbook.Save();
ExcelApp.Workbooks.Close();
ExcelApp.Quit();
ExcelApp = null;
return true;
}
catch
{
ExcelApp.ActiveWorkbook.Save();
ExcelApp.Workbooks.Close();
ExcelApp.Quit();
return false;
}
}兄弟们,只剩70分了。
/// <summary>
/// 从当前打开的excel中删除一个sheet
/// </summary>
/// <param name="strSheetName">要删除的sheet的名称</param>
/// <returns>删除成功与否</returns>
public static bool DeleteWorksheet(string strFileName,string strSheetName)
{
if(! (System.IO.File.Exists(strFileName)) )
{
return false;//如果Excel文件不存在,直接返回
}
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Workbook workbook = ExcelApp.Workbooks.Open(strFileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //ExcelApp.DisplayAlerts = false;
try
{
if ((ExcelApp != null) && (ExcelApp.ActiveWorkbook != null))
{
Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Worksheets;
int iCnt = sheets.Count; if (iCnt == 1)
{
Microsoft.Office.Interop.Excel._Worksheet wksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(1);
wksheet.Name = "Test";
}else{
for (int i = 0; i < iCnt; i++)
{
Microsoft.Office.Interop.Excel._Worksheet wksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(i + 1);
if (wksheet.Name == strSheetName)
{
wksheet.Delete();
break;
}
}
} }
//ExcelApp.DisplayAlerts = true;
ExcelApp.ActiveWorkbook.Save();
ExcelApp.Workbooks.Close();
ExcelApp.Quit();
ExcelApp = null;
return true;
}
catch
{
ExcelApp.ActiveWorkbook.Save();
ExcelApp.Workbooks.Close();
ExcelApp.Quit();
return false;
}
}兄弟们,只剩70分了。
解决方案 »
- select 问题
- 求一正则写法
- 这条语句有什么问题?
- 简单的让人郁闷
- TeeChart 之WebCharta显示问题,急急急!
- 页面自动跳转的同时还要传递一个参数.帮顶者有分!!
- linkbutton的问题,很急,再现等,请帮帮我 ,谢谢大家!!
- 谁见过这样的树啊?有单选按钮的,由复选框的,支持客户端脚本,支持动态读取的
- 救命啊,低级错误,但不知怎么解决,在线等待
- 我老总的网页是弹出窗口的打不开,同一页的可以打开,谁会改注册表啊,急啊,拜托!
- stringBuilder.Append与stringBuilder.AppendLine有何异同!
- 求asp.net网站后台直接发表新闻标题和网址到新浪微博详细实例
GC.GetTotalMemory(false);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.GetTotalMemory(true);传送门