Excel导出后第一次无法正确打开文件
在测试机器上(开发机器上是正确的)用如下代码导出Excel文件
导出后第一次打开文件
Excel窗口死掉 只能显示菜单,不能显示Sheet
大家给看下可能是什么原因
Excel类型库: Microsoft Excel 9.0 Object Library
private void ExportToExcelStart()
{
Excel.Application excel = null;
try
{
excel = new Excel.Application();
excel.DisplayAlerts = false;
Workbook workbook = excel.Workbooks.Add(Type.Missing);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = null;
for(int i = sheets.Count;i> 1;i--)
{
worksheet = (_Worksheet) sheets.get_Item(i);
worksheet.Delete();
}
worksheet = (_Worksheet) sheets.get_Item(1);
exportExcelParamClass.worksheet = worksheet;
this.ExportDataToExcelSheet();//这个函数是向Excel写入数据
worksheet.Activate();
worksheet.get_Range( "A1 ", "A1 ").Select();
excel.ActiveWindow.DisplayGridlines =false;
worksheet.Name = GetExcelSheetName(exportExcelParamClass.PageSetting);
worksheet.Columns.AutoFit();
if( exportExcelParamClass != null && exportExcelParamClass.Succeed)
{
workbook.SaveAs(exportExcelParamClass.FileName, System.Reflection.Missing.Value, " ", " ",
false, false, Excel.XlSaveAsAccessMode.xlNoChange, 1, true,
System.Reflection.Missing.Value, System.Reflection.Missing.Value);
}
workbook.Saved = true;
excel.Quit();
}
catch(Exception ex)
{
exportExcelParamClass.Succeed = false;
ShowMessageInMainProcess( "导出错误,可能存在的问题为: " + Environment.NewLine +
"1.文件夹只读; " + Environment.NewLine +
"2.同名的文件存在,已经被打开; " + Environment.NewLine +
"3.没有安装excel文件! "
#if(DEBUG)
+ Environment.NewLine+ex.Message
+ Environment.NewLine+ex.StackTrace
#endif
);
return;
}
finally
{
if (excel != null)
{
int generation = 0;
excel.UserControl = false;
excel.Quit();
generation = System.GC.GetGeneration(excel);
excel = null;
System.GC.Collect(generation);
}
if( exportExcelParamClass.Succeed)
{
exportExcelParamClass.eProgess(100d,100);
ShowMessageInMainProcess( "导出成功! ");
}
exportExcelParamClass.frmProgess.Close();
}
}
------解决方案--------------------mark
------解决方案--------------------我一般是采用文件首先存储到服务器,让用户下载,然后再删除的方式来处理。
------解决方案--------------------JF UP 正在写把电子表格读到GRIDVIEW的东东``
------解决方案--------------------导出文件到服务器,客户端提供链接下载,
如果客户端直接打开服务器生成的excel文件,excel的进程是永远不能关闭,会出现无法正确打开文件。