在项目中遇到一个问题,在打印有公式的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#
解决方案 »
- C#窗口继承的棘手问题(控件总移动位置)
- C#,将访问的文件直接转为全路径的方法是什么?
- c# 如何返回托管类型的内存字节大小
- 一个较偏僻的技术问题,求高手解答
- 大家来讨论一下“定时开机”
- 关于线程的一个问题。如何在一个窗体内启动一个线程 作为前台显示等待,后台对数据库进行操作。 谢谢!!!
- 如何在C#生成的图片上添加随即噪点
- 日期时间转换.有内置的转换方法么?
- public abstract class BaseEntity<TEntity> : IComparable<TEntity>求解释
- 求解决思路,treeview中节点数量巨大(上万),如何才能得到较好的性能?欢迎大家踊跃讨论
- 改变任意一个comboBox的值 其他的comboBox全部变为相同的值
- 插件开发问题~
xlWorkbook.Close(oMissing, oMissing, oMissing);=====>xlWorkbook.Close(false, oMissing, oMissing);