WinForm预览Office文档

使用WinForm, WPF, Office组件

原理:使用Office COM组件将Word,Excel转换为XPS文档, 将WPF的DocumentViewer控件寄宿到WinForm中, 实现预览.

1. 新建WinForm项目

2. 新建WPF用户控件, 注意是WPF控件

3. 编辑WPF用户控件

<UserControl ...
...>
<Grid>
<DocumentViewer x:Name="documentViewer"/>
</Grid>
</UserControl>

VS设计预览显示效果如下:

如果不需要自带的工具栏, 可以添加以下资源隐藏工具栏:

<!--隐藏DocumentViewer边框-->
<UserControl.Resources>
<Style x:Key="{x:Type DocumentViewer}" TargetType="{x:Type DocumentViewer}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DocumentViewer}">
<Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Focusable="False">
<Grid KeyboardNavigation.TabNavigation="Local">
<Grid.Background>
<SolidColorBrush Color="{DynamicResource ControlLightColor}" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer Grid.Row="1" CanContentScroll="true" HorizontalScrollBarVisibility="Auto" x:Name="PART_ContentHost" IsTabStop="true">
<ScrollViewer.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{DynamicResource ControlLightColor}" Offset="0" />
<GradientStop Color="{DynamicResource ControlMediumColor}" Offset="1" />
</LinearGradientBrush>
</ScrollViewer.Background>
</ScrollViewer>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>

4. 新建WinForm用户控件

在WinForm上添加ElementHost

将WPF用户控件添加到ElementHost上,设计器代码XpsPreviewer.Designer.cs如下

    //ElementHost
private System.Windows.Forms.Integration.ElementHost elementHost1;
//XpsPreviewer变量
private WPF.XpsPreviewer xpsPreviewer1; private void InitializeComponent()
{
this.elementHost1 = new System.Windows.Forms.Integration.ElementHost();
this.xpsPreviewer1 = new WPF.XpsPreviewer(); //初始化
//其他属性初始化...
this.elementHost1.Child = this.xpsPreviewer1;
//其他属性初始化...
}

XpsPreviewer.cs后台代码中定义方法:

    /// <summary>
/// 加载XPS文件
/// </summary>
/// <param name="fileName">XPS文件名</param>
internal void LoadXps(string fileName)
{
var xpsDocument = new XpsDocument(fileName, FileAccess.Read);
this.xpsPreviewer1.documentViewer.Document = xpsDocument.GetFixedDocumentSequence();
xpsDocument.Close();
}

5. 将Excel(Word类似)转换为XPS文件

通过Nuget包管理控制台安装COM组件:

    PM> Install-Package Microsoft.Office.Interop.Excel

转换为XPS:

    /// <summary>
/// 将Excel文件转换为XPS文件
/// </summary>
/// <param name="execelFileName">Excel文件名</param>
/// <param name="xpsFileName">转换的xps文件名</param>
public void ConvertExcelToXps(string excelFileName, string xpsFileName)
{
if (string.IsNullOrWhiteSpace(excelFileName))
throw new ArgumentNullException(excelFileName);
if (string.IsNullOrWhiteSpace(xpsFileName))
throw new ArgumentNullException(xpsFileName); var fileInfo = new FileInfo(xpsFileName);
if (!fileInfo.Directory.Exists)
fileInfo.Directory.Create(); //删除已存在的文件
if (File.Exists(xpsFileName))
File.Delete(xpsFileName); Excel.Application app = new Excel.Application();
app.DisplayAlerts = false;
Excel.Workbooks wbs;
Excel.Workbook wb; wbs = app.Workbooks;
wb = wbs.Add(excelFileName);
dynamic Nothing = System.Reflection.Missing.Value;
wb.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypeXPS, xpsFileName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
wb.Close(true);
wbs.Close();
app.Quit();
KillExcelProcess(app);
}

扩展: 每次调用Excel打开文件,均会产生一个进程, 在网络上收集的释放Excel进程方式均不起作用. 因此选择直接结束进程, 根据Excel句柄结束进程, 而不是根据进程名称杀死全部正在运行的Excel.

    [DllImport("User32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ProcessId); /// <summary>
/// 结束Excel进程
/// </summary>
/// <param name="obj"></param>
private void KillExcelProcess(Excel.Application app)
{
if (app == null)
return;
try
{
IntPtr intptr = new IntPtr(app.Hwnd);
int id;
GetWindowThreadProcessId(intptr, out id);
var p = Process.GetProcessById(id);
p.Kill();
}
catch { }
}

现在已经可以正常的预览Excel文件了. 由于Excel另存为XPS文件会耗费一定的时间, 因此建议在后台线程中提前异步生成, 在预览时可直接调取XPS文件.

补充: Word 转换为 PDF

    Word.Application app = new Word.Application();
Word.Document document = null;
object missing = System.Reflection.Missing.Value;
app.Visible = false;
document = app.Documents.Open(sourcePath);
document.ExportAsFixedFormat(targetPath, Word.WdExportFormat.wdExportFormatPDF);
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
document.Close(ref saveChanges, ref missing, ref missing);
((Word._Application)app).Quit(ref missing, ref missing, ref missing);

WinForm中预览Office文件的更多相关文章

  1. 在线预览office文件

    Office Online 实现在线预览 office的在线预览,针对不同的浏览器版本和系统具有要求,具体的相关文档请参考官方文档. 利用office online 平台进行office 文档的在线查 ...

  2. React中使用react-file-viewer,实现预览office文件(pdf,word,xlsx等文件)前端实现

    最近做一个项目要求在前端浏览器可以直接打开office文件(pdf,doc,xlsx等文件).pdf浏览器可以直接打开(可以直接用a标签href="文件地址"或者iframe标签s ...

  3. 在线预览Office文件【效果类似百度文库】

    引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前项目中是用到公司购买的Ntko控件,该控件每次浏览文件时则会提示安装信任插件,很繁琐,而且浏览效果不好. 提到Offic ...

  4. 在线预览Office文件【效果类似百度文库】(转载)

    转载地址:http://www.cnblogs.com/sword-successful/p/4031823.html 引言 结合上个项目和目前做的这个项目,其中都用到了Office文件在线预览,目前 ...

  5. 经管资源库项目总结----在线预览office文件的实现与总结

    依旧是这个经管的项目.在线预览作为资源和文档管理系统的一个很酷的并且是如此重要的功能,是必须要实现的.然后百度一下office在线预览,看起来so eazy啊,各种博客各种demo,一下子就做出效果来 ...

  6. 浏览器预览office文件(word,Excel,等)

    提示:预览是通过后台转pdf到前台展示的过程,当然网上也有购买的api 举个栗子:(http://www.officeweb365.com/) <!DOCTYPE html> <ht ...

  7. 文档控件NTKO OFFICE 详细使用说明之预览Excel文件(查看、编辑、保存回服务器)

    1.在线预览Excel文件 (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将www.ntko.com加入到浏 ...

  8. 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)

    1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...

  9. 在网页中预览excel表格文件

    项目需求在前端页面中实现预览excel表格的功能,上网了解之后大致总结为一下几种方法. 1.office文档转换为pdf,再转swf,然后通过网页加载flash进行预览 2.通过 xlsx.js,js ...

随机推荐

  1. gym 101657 D

    理论1A.  //没删debug的文件读入.. 傻逼题. 先求出来每条边两侧的三角形,然后枚举边,根据叉积判断三角形位置,建图,拓扑排序. #include <bits/stdc++.h> ...

  2. 升级 phpstud y中的 mysql 版本

    1.找到你 phpstudy 安装目录,找到 MySQL 文件夹 (我自己的实际目录  D:\pc\phpstudy\MySQL),关掉退出 phpstudy服务,删除 MySQL 文件夹里的文件,如 ...

  3. python接口自动化测试(c测试环境的准备)

    接口测试的方式有很多,比如可以用工具(jmeter,postman)之类,也可以自己写代码进行接口测试,工具的使用相对来说都比较简单,重点是要搞清楚项目接口的协议是什么,然后有针对性的进行选择,甚至当 ...

  4. mac重启,开启apache时报错~~~镜像没有找到

    mac重启apache时,报类似下面的错 dyld: Library not loaded: /usr/local/lib/libjpeg8.dylib Referenced from: /usr/l ...

  5. a标签强制不换行

    a标签文字强制不换行 强制不换行 a{ white-space:nowrap; } 再补充说明所有关于换行的CSS样式: white-space: normal|pre|nowrap|pre-wrap ...

  6. Python学习之旅(二十)

    Python基础知识(19):面向对象高级编程(Ⅱ) 定制类 形如“__xx__”的变量或函数在Python中是有特殊用途的 1.__str__ 让打印出来的结果更好看 __str__:面向用户:__ ...

  7. asp.net mvc ef 性能监控调试工具 MiniProfiler

    MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控 ...

  8. stm8 同时使用dac和adc 采集异常,电平异常

    这种现象在早期的 使用stm8l151的dac 和adc相互干扰很厉害.后来通过读手册发现  相邻三个引脚一般不建议同时使用dac和adc.也就是这两种功能,引脚分配至少隔离三个引脚.内部为了节省成本 ...

  9. keras实现textcnn

    https://github.com/MoyanZitto/keras-cn/blob/master/docs/legacy/blog/word_embedding.md 这个链接将带有embedin ...

  10. python框架之Flask(6)-flask-sqlalchemy&flask-script&flask-migrate使用

    整合SQLAlchemy 安装 pip3 install flask-sqlalchemy 简单使用 from flask import Flask from flask_sqlalchemy imp ...