[C++][Office] Excel 增益集自订另存新档及存成XPS、PDF问题

时间:2019-09-04
本文章向大家介绍[C++][Office] Excel 增益集自订另存新档及存成XPS、PDF问题,主要包括[C++][Office] Excel 增益集自订另存新档及存成XPS、PDF问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

摘要:[C++][Office] Excel 增益集自订另存新档


在 Excel 增益集当中我们需要取得存档之后的文件名,所以在 WorkbookBeforeSave 事件当中进行尝试。

因为这个事件在存档前就已经触发了,无法取得使用者到底存成了什么文件名,所以我们取消掉 Office 自己调用的 SaveAs Dialog,让我们自行调用,这样就可以让使用者在我们自己产生的 Dialog 中存档,进行后续动作。

调用 SaveAsDialog 的方式有两种,各有优缺

如何自行调用 SaveAsDialog

1. 使用 Office::FileDialog,可以容易取得存档后的文件名、附档,但原本 PDF 及 XPS 档的发布选项将会不见


 void CALLBACK CConnect::WorkbookBeforeSave(
	Excel::_Workbook* Wb, 
	VARIANT_BOOL SaveAsUI, 
	VARIANT_BOOL* Cancel)
{
	Office:: FileDialog* fileDialog;
	*Cancel = true;
	Wb->Application->get_FileDialog(Office::mosFileDialogSaveAs, &fileDialog);
	if(fileDialog->Show() == VARIANT_TRUE)
	{
                fileDialog->Execute(); // 执行存档
	}
	else
	{
		//Do something if Cancel
	}
} 

2. 使用 XlBuiltInDialog,原有的 SaveAsDialog


 void CALLBACK CConnect::WorkbookBeforeSave(
	Excel::_Workbook* Wb, 
	VARIANT_BOOL SaveAsUI, 
	VARIANT_BOOL* Cancel)
{

	*Cancel = true;
	CComPtr App(Wb->Application);
	CComPtr dlg = App->Dialogs->GetItem(Excel::XlBuiltInDialog::xlDialogSaveAs);
    if(dlg->Show() == VARIANT_TRUE)
	{
                
		_bstr_t fileName = Wb->Name;
                _bstr_t fullName = Wb->FullName[0];
	}
	else
	{
		//Do something if Cancel
	}
} 

Office::FileDialog 存成 PDF 或 XPS 时 需要另行处理

基本上以上两种都可以完成存档,但使用 Office::FileDialog 时发现如果另存为 PDF 或 XPS 两种格式文档时会有问题,文件是保存了没错,但是打开时两种都出现格式错误的问题,将他们的副文件名再改为.xlsx 时又可以用 EXCEL 开启,事实上根本就是假存档真改副文件名罢了。

要另存成 PDF 或 XPS 时需要调用 ExportAsFixedFormat,所以延伸第一种 Dialog 写法另做判断


 void CALLBACK CConnect::WorkbookBeforeSave(
	Excel::_Workbook* Wb, 
	VARIANT_BOOL SaveAsUI, 
	VARIANT_BOOL* Cancel)
{
	Office:: FileDialog* fileDialog;
	*Cancel = true;
	Wb->Application->get_FileDialog(Office::mosFileDialogSaveAs, &fileDialog);
	if(fileDialog->Show() == VARIANT_TRUE)
	{
		Office::FileDialogSelectedItemsPtr selectedItems = fileDialog->SelectedItems;
		_bstr_t path = selectedItems.Item(1);
		WCHAR ext[_MAX_FNAME]={0x00};
		_wsplitpath(path, NULL, NULL, NULL, ext);
		
		if(ext == L".pdf")
		{
			Wb->ExportAsFixedFormat(Excel::xlTypePDF, L"C:myExportPDF.pdf", vtMissing,
				vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
		}
		else if(ext == L".xps)
		{
			Wb->ExportAsFixedFormat(Excel::xlTypeXPS, L"C:myExportXPS.xps", vtMissing,
				vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing);
		}
		else
		{
			fileDialog->Execute(); // 执行一般格式存档
		}
	}
}

如何取得存档后的文件名

使用 Office::FileDialog,从 dialog 内取得参数


 bool isCustomSaveAs = false;
void CALLBACK CConnect::WorkbookBeforeSave(
			Excel::_Workbook* Wb, 
			VARIANT_BOOL SaveAsUI, 
			VARIANT_BOOL* Cancel)
{
	if(isCustomSaveAs)
		return;
	
	// 没有显示 UI 表示他为"存档"事件
	if(SaveAsUI != VARIANT_TRUE)
		return;
	
	isCustomSaveAs = true; 	//fileDialog.Show() 会再次触发此事件,需要滤掉
	*Cancel = true;			// 取消原本的另存新档行为
	
	// 自订另存新档流程
	CcomPtr App(Wb->Application);
	Office::FileDialog* fileDialog;
	App->get_FileDialog(Office::msoFileDialogSaveAs, &fileDialog);
	
	if(fileDialog->Show() != VARIANT_TRUE)
		return;
	
	// 取得存档后的文件名
	Office::FileDialogSelectedItemsPtr items = fileDialog->SelectedItems;
	_bstr_t fileName = selectedItems->Items(1);
	
	// 取得存档的类型
	int typeIndex = fileDialog->FilterIndex;
	Office::FileDialogFilterPtr flterPtr = fileDialog->GetFilters()->Item(typeIndex);
    _bstr_t ext = flterPtr->GetExtensions();
}

使用 XlBuiltInDialog,从 Workbook 取得文件名


void CALLBACK CConnect::WorkbookBeforeSave(
	Excel::_Workbook* Wb, 
	VARIANT_BOOL SaveAsUI, 
	VARIANT_BOOL* Cancel)
{

	*Cancel = true;
	CComPtr App(Wb->Application);
	CComPtr dlg = App->Dialogs->GetItem(Excel::XlBuiltInDialog::xlDialogSaveAs);
    if(dlg->Show() == VARIANT_TRUE)
	{
                // 存成 XPS、PDF 时取不到存档后的文件名,或许有方法,但我还没找到
		_bstr_t fileName = Wb->Name;
		_bstr_t fullName = Wb->FullName[0];
              
	}
	else
	{
		//Do something if Cancel
	}
} 

原文:大专栏  [C++][Office] Excel 增益集自订另存新档及存成XPS、PDF问题


原文地址:https://www.cnblogs.com/chinatrump/p/11458430.html