当前位置:   article > 正文

用C#实现实时监测文件夹,把word/excel/ppt自动转为pdf_c# 监控excel文件程序

c# 监控excel文件程序

很久没更新了,前段时间在准备软考,最近也忙着单位新系统的开发。所以也就没更新了,等后面几个项目做好了,我在项目分享出来!!

由于新开发的信息系统,原来旧版的信息系统,在查看文件时,都需要下载到本地,然后在打开查看,这样比较繁琐,所以在新版信息系统里,用了pdfjs插件进行在线查看文档,那么问题来了,怎么样才能把用户上传上来的office文档转为pdf呢??这个问题我采取了用php调用office的com组件进行转换。转换出来的pdf文件跟源文件基本一致,算是比较完美。但是com组件总会出现这个那个的问题。最后还是放弃了这个方案。第二个方案是安装调用openoffice组件,跟上面的差不多,虽然问题解决了,但是转换的效果很差,唉~~~~现在来说说我最终的方案吧!

现在这个方案是单独用C#写个winform程序,这个程序一直监控上传的目录,当目录有新的office文档上传时,程序直接把这些文件自动生成对应文件名的pdf,也就是test.doc同时生成test.doc.pdf,这样便于管理文件,同时php程序也不用去调用任何组件,两个程序没有半点关系。

程序已经在服务器上跑了一个月了,没发生任何错误!!完美~~~~
在这里插入图片描述

源码很少,用到了3个组件:Aspose.Words.dll、Aspose.Cells.dll、Aspose.Slides.dll,以下是源码:下载地址在最下面!!

using System;
using System.IO;
using System.Windows.Forms;
using Aspose.Words;
using Aspose.Cells;
using Aspose.Slides;
using System.Threading;
using System.Runtime.InteropServices;

namespace Office2PDF
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            StartBtn.Visible = true;
            StopBtn.Visible = false;
            // 创建日志目录
            if (!Directory.Exists(LogFilePath))
            {
                Directory.CreateDirectory(LogFilePath);
            }
            // 初始化控件值
            WatchPath.Text = appsetting.Default.WatchPath;
            ExcludeFolder.Text = appsetting.Default.ExcludeFolder;
            WordCheckBox.Checked = appsetting.Default.WordCheckBox;
            ExcelCheckBox.Checked = appsetting.Default.ExcelCheckBox;
            PowerpointCheckBox.Checked = appsetting.Default.PowerpointCheckBox;
        }

        private String LogFilePath = Application.StartupPath + "\\log";

        private void ExitBtn_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void SelectFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            WatchPath.Text = path.SelectedPath;
            appsetting.Default.WatchPath = path.SelectedPath;
            appsetting.Default.Save();
        }

        public void WriteMessage(string msg)
        {
            var path = "log\\"+DateTime.Now.ToString("yyyyMMdd") + ".log";
            if (!File.Exists(path)){
                var fsc = new FileStream(path, FileMode.Create, FileAccess.Write);
                fsc.Close();
            }
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.BaseStream.Seek(0, SeekOrigin.End);
                    sw.WriteLine("{0}", "[" + GetNowTime() + "]:" +msg, DateTime.Now);
                    sw.Flush();
                }
            }
        }

        private void StartBtn_Click(object sender, EventArgs e)
        {
            StartBtn.Visible = false;
            StopBtn.Visible = true;
            var wPath = WatchPath.Text;
            fileSystemWatcher.Path = wPath;
            fileSystemWatcher.Created += OnCreated;
            fileSystemWatcher.EnableRaisingEvents = true;
        }

        private void LogBtn_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(LogFilePath);
        }

        private void StopBtn_Click(object sender, EventArgs e)
        {
            StartBtn.Visible = true;
            StopBtn.Visible = false;
            fileSystemWatcher.EnableRaisingEvents = false;
        }

        private void OnCreated(object source, FileSystemEventArgs e)
        {
            var msg = e.FullPath;
            var ext = Path.GetExtension(e.FullPath);
            var path = Path.GetDirectoryName(e.FullPath);
            bool isExclude = false;
            // 处理排除目录
            string[] arr = new string[ExcludeFolder.Lines.Length];
            for (int i = 0; i < ExcludeFolder.Lines.Length; i++)
            {
                arr[i] = ExcludeFolder.Lines[i];
                if (ExcludeFolder.Lines[i].Length != 0)
                {
                    if (path.IndexOf(ExcludeFolder.Lines[i]) > 0)
                    {
                        isExclude = true;
                    }
                }
            }
            if (!isExclude)
            {
                if (WordCheckBox.Checked)
                {
                    if (ext == ".doc" || ext == ".docx")
                    {
                        Thread.Sleep(1000);
                        var doc = new Document(@e.FullPath);
                        doc.Save(@e.FullPath + ".pdf", Aspose.Words.SaveFormat.Pdf);
                        WriteMessage(msg);
                    }
                }
                if (ExcelCheckBox.Checked)
                {
                    if (ext == ".xls" || ext == ".xlsx")
                    {
                        Thread.Sleep(1000);
                        var xls = new Workbook(@e.FullPath);
                        xls.Save(@e.FullPath + ".pdf", Aspose.Cells.SaveFormat.Pdf);
                        WriteMessage(msg);
                    }
                }
                if (PowerpointCheckBox.Checked)
                {
                    if (ext == ".ppt" || ext == ".pptx")
                    {
                        Thread.Sleep(1000);
                        var ppt = new Presentation(@e.FullPath);
                        ppt.Save(@e.FullPath + ".pdf", Aspose.Slides.Export.SaveFormat.Pdf);
                        WriteMessage(msg);
                    }
                }
            }
        }

        private void ExcludeFolder_TextChanged(object sender, EventArgs e)
        {
            appsetting.Default.ExcludeFolder = ExcludeFolder.Text;
            appsetting.Default.Save();
        }

        private void WordCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            appsetting.Default.WordCheckBox = WordCheckBox.Checked;
            appsetting.Default.Save();
        }

        private void ExcelCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            appsetting.Default.ExcelCheckBox = ExcelCheckBox.Checked;
            appsetting.Default.Save();
        }

        private void PowerpointCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            appsetting.Default.PowerpointCheckBox = PowerpointCheckBox.Checked;
            appsetting.Default.Save();
        }

        private static string GetNowTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        }

        private void FreeTimer_Tick(object sender, EventArgs e)
        {
            ClearMemory();
        }
        // 释放内存
        [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
        public static void ClearMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                MainForm.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190

项目下载地址:
Office2PDF v1.0

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/325192
推荐阅读
相关标签
  

闽ICP备14008679号