在项目中遇到一个问题,在打印有公式的Excel文档时,现有程序会在打印完毕后,关闭Excel文档时,弹出一个对话框,“microsoft excel 在打开上次由excel的早期版本保存的文件时,会重新计算公式。”这样一个对话框,它将阻塞程序继续运行(必须点掉它,程序才会继续工作)。
有什么办法可以避免它吗?附上打印代码 Excel.Workbooks xlWorkbooks;
Excel.Workbook xlWorkbook;
Excel.Worksheet xlWorksheet;
System.Type tyWorkbooks;
System.Reflection.MethodInfo[] methods;
object objFilePath; object oMissing = System.Reflection.Missing.Value; try
{
xlApp.Visible = false;
xlWorkbooks = xlApp.Workbooks;
tyWorkbooks = xlWorkbooks.GetType();
methods = tyWorkbooks.GetMethods();
objFilePath = fileName;
xlWorkbook = (Excel.Workbook)tyWorkbooks.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod,
null,
xlWorkbooks,
new object[] { objFilePath, true, true });
int vaildcount = 0;
foreach (Excel.Worksheet item in xlWorkbook.Worksheets)
{
if (item != null && item.Rows.Count > 0 && item.Columns.Count > 0)
{
if (item.UsedRange.Value == null || item.UsedRange.Value.ToString() == "")
{
//....
}
else
{
vaildcount++;
}
xlWorksheet = item;
xlWorksheet.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
System.Threading.Thread.Sleep(1000);
}
}
xlWorkbook.Close(oMissing, oMissing, oMissing); return true;
}
catch (Exception ex)
{
throw new Exception("Print Excel File(" + fileName + ") Exception. Msg:" + ex.Message);
}
finally
{
if (xlApp != null)
{
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect();
}
excel打印C#
有什么办法可以避免它吗?附上打印代码 Excel.Workbooks xlWorkbooks;
Excel.Workbook xlWorkbook;
Excel.Worksheet xlWorksheet;
System.Type tyWorkbooks;
System.Reflection.MethodInfo[] methods;
object objFilePath; object oMissing = System.Reflection.Missing.Value; try
{
xlApp.Visible = false;
xlWorkbooks = xlApp.Workbooks;
tyWorkbooks = xlWorkbooks.GetType();
methods = tyWorkbooks.GetMethods();
objFilePath = fileName;
xlWorkbook = (Excel.Workbook)tyWorkbooks.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod,
null,
xlWorkbooks,
new object[] { objFilePath, true, true });
int vaildcount = 0;
foreach (Excel.Worksheet item in xlWorkbook.Worksheets)
{
if (item != null && item.Rows.Count > 0 && item.Columns.Count > 0)
{
if (item.UsedRange.Value == null || item.UsedRange.Value.ToString() == "")
{
//....
}
else
{
vaildcount++;
}
xlWorksheet = item;
xlWorksheet.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
System.Threading.Thread.Sleep(1000);
}
}
xlWorkbook.Close(oMissing, oMissing, oMissing); return true;
}
catch (Exception ex)
{
throw new Exception("Print Excel File(" + fileName + ") Exception. Msg:" + ex.Message);
}
finally
{
if (xlApp != null)
{
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
GC.Collect();
}
excel打印C#
xlWorkbook.Close(oMissing, oMissing, oMissing);=====>xlWorkbook.Close(false, oMissing, oMissing);