如何隐藏ExportAsFixedFormat的发布进度条

时间:2014-09-12 08:13:09

标签: c# excel excel-interop

我正在尝试使用interop.excel将Excel文件转换为PDF,同时执行ExportAsFixedFormat'发布'进度条显示在网站上。有什么方法可以隐藏这个吗?我发现这个问题适用于大小超过300KB的Excel文件。

代码如下:

//(tried using Application instead of ApplicationClass)
Microsoft.Office.Interop.Excel.ApplicationClass excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
excelApplication.ScreenUpdating = false;                                   
excelApplication.DisplayAlerts = false;                                     
excelApplication.Visible = false;

if (excelWorkbook == null)
{
    excelApplication.Quit();
    excelApplication = null;
    excelWorkbook = null;
    return false;
}
var exportSuccessful = true;
try
{
    excelApplication.AlertBeforeOverwriting = false;
    excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
}
catch (System.Exception ex)
{
    exportSuccessful = false;
}

我无法找到任何解决方案。我的项目是一个C#Web应用程序。

1 个答案:

答案 0 :(得分:1)

我花了几天才弄清楚,但最后这里是一个使用一些WinAPI函数观察windows事件的workarround。挂钩处于活动状态时,会将每个新窗口与其类与PDF保存对话框类相同进行比较。如果是这种情况,则窗口会被隐藏。

感谢一些中国人的解决方案: http://www.itjie.wang/officebase/516998.html

使用要求:
由于WinAPI的使用,操作系统(=操作系统)必须是Windows。

警告:
如果由于某些错误而没有再次停止“SetWinEventHook”,则最好重新启动系统,否则可能会遇到Windows的一些严重问题。

注意:
默认情况下,PDF保存对话框不会定期出现。这取决于保存PDF文件所需的时间。如果需要更长时间,则会显示保存弹出窗口。如果需要更短时间,则不会显示保存弹出窗口。无论如何,你不必担心保存对话框是否会出现,代码已经为你做了这个。

指令:
在Excel工作簿中,如果您还没有模块,请创建一个新模块(名称无关紧要)&粘贴以下代码:

' WINDOWS API FUNCTIONS:
Private Declare Function SetWinEventHook Lib "user32" (ByVal eventMin As Long, ByVal eventMax As Long, ByVal hmodWinEventProc As Long, ByVal pfnWinEventProc As Long, ByVal idProcess As Long, ByVal idThread As Long, ByVal dwFlags As Long) As Long
Private Declare Function UnhookWinEvent Lib "user32" (ByVal hWinEventHook As Long) As Long
Private Declare Function apiGetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassname As String, ByVal nMaxCount As Long) As Long
Private Declare Function apiShowWindow Lib "user32" Alias "ShowWindow" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

' CONSTANT VARIABLES:
Private Const SW_HIDE = 0
Private Const DLG_CLSID = "CMsoProgressBarWindow"
Private Const EVENT_SYSTEM_FOREGROUND = &H3&
Private Const WINEVENT_OUTOFCONTEXT = 0

' GLOBAL VARIABLES:
Dim long_WinEventHook As Long

Public Function StartEventHook() As Long
 long_WinEventHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, 0&, AddressOf WinEventFunc, 0, 0, WINEVENT_OUTOFCONTEXT)
 StartEventHook = long_WinEventHook
End Function

Public Sub StopEventHook()
 Dim b_unhooked As Boolean
 If long_WinEventHook = 0 Then
  MsgBox "WinEventHook couldn't be stopped! " & _
  "Variable 'long_WinEventHook' is empty! " & _
  "Better restart Windows now!"
  Exit Sub
 End If
 b_unhooked = UnhookWinEvent(long_WinEventHook)
 If b_unhooked = True Then
 Else
  MsgBox "WinEventHook couldn't be stopped! " & _
  "Variable 'b_unhooked' is false! " & _
  "Better restart Windows now!"
 End If
End Sub

' CALLBACK FUNC OF "SetWinEventHook" (DEFINE ACTIONS TO RUN ON THE EVENTS):
' http://stackoverflow.com/questions/20486944/detecting-in-vba-when-the-window-containing-an-excel-instance-becomes-active
Public Function WinEventFunc(ByVal HookHandle As Long, ByVal LEvent As Long, ByVal hWnd As Long, ByVal idObject As Long, ByVal idChild As Long, ByVal idEventThread As Long, ByVal dwmsEventTime As Long) As Long
 'This function is a callback passed to the win32 api
 'We CANNOT throw an error or break. Bad things will happen
 On Error Resume Next
 Dim l_handle As Long
 Dim s_buffer As String
 Dim b_visible As Boolean
 Dim i_bufferLength As Integer
 s_buffer = String$(32, 0)
 i_bufferLength = apiGetClassName(hWnd, s_buffer, Len(s_buffer))
 If Left(s_buffer, i_bufferLength) = DLG_CLSID Then
  b_visible = apiShowWindow(hWnd, SW_HIDE)
  WinEventFunc = hWnd
 End If
End Function

在您的VBA代码中,当您想要将Excel工作簿保存为PDF时,您可以调用上面的宏:

' ADD WINDOWS EVENT HOOK BEFORE SAVING:
Application.Run XL_WB.Name & "!StartEventHook"

' SAVE EXCEL AS PDF:
' https://msdn.microsoft.com/de-de/library/microsoft.office.tools.excel.worksheetbase.exportasfixedformat.aspx
XL_WB.ExportAsFixedFormat _
 Type:=xlTypePDF, _
 Filename:="C:\PDF.pdf", _
 Quality:=xlQualityStandard, _
 IncludeDocProperties:=True, _
 IgnorePrintAreas:=False, _
 OpenAfterPublish:=False

' REMOVE WINDOWS EVENT HOOK AFTER SAVING:
Application.Run XL_WB.Name & "!StopEventHook"

在上面的VBA代码示例中,“XL_WB”是一个变量。你必须根据自己的需要进行调整。例如,使用“ActiveSheet”。

来自其他网站的用户也请求帮助解决该特定问题: