我创建了一个Windows服务,里面用了Timer,作用是定期读取文件夹下的XML文件,然后转换成PDF。
可是当代码走到声明对象FpSpread fpSpread = new FpSpread(); 就不往下走了。请尽快帮忙,谢谢了。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.ServiceProcess;
- using System.Text;
- using System.IO;
- using System.Threading;
- using FarPoint.Win.Spread;
- namespace ELN2PDFService
- {
- public partial class ELN2PDF : ServiceBase
- {
- private System.Timers.Timer timer;
- private string LogPath = "C:\\Log\";
- int TimeCount = 0;
- public ELN2PDF()
- {
- InitializeComponent();
- if (timer == null)
- {
- timer = new System.Timers.Timer();
- timer.Enabled = true;
- timer.Interval = 10000; // 10秒
- timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
- }
- }
- #region Events
- protected override void OnStart(string[] args)
- {
- WriteLog("服务启动");
- timer.Enabled = true;
- }
- protected override void OnStop()
- {
- WriteLog("服务停止");
- timer.Enabled = false;
- }
- #endregion
- #region Methods
- private void WriteLog(string Msg)
- {
- StreamWriter swLog = new StreamWriter(LogPath + DateTime.Now.ToString("yyyyMMdd") + ".log", true);
- swLog.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + Msg);
- swLog.Flush();
- swLog.Close();
- }
- // 定时执行
- private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- try
- {
- TimeCount++;
- if (TimeCount > 2)
- {
- timer.Enabled = false;
- WriteLog("Run");
- RunELN2PDF();
- }
- }
- catch (Exception ex)
- {
- WriteLog("Error: " + ex.Message);
- }
- finally
- {
- timer.Enabled = true;
- }
- }
- private void RunELN2PDF()
- {
- try
- {
- string[] strFiles = Directory.GetFiles("C:\\Users\\Administrator\\Desktop\\XML\", "*.xml");
- WriteLog(strFiles.Length.ToString());
- for (int i = 0; i < strFiles.Length; i++)
- {
- string xmlPath = strFiles[i].ToString();
- string PDFPath = xmlPath.Substring(0, xmlPath.LastIndexOf(".")) + ".pdf";
- WriteLog(xmlPath);
- ExportToPDF(xmlPath, PDFPath, 0, "Portrait");
- File.Delete(xmlPath);
- }
- }
- catch (Exception ex)
- {
- WriteLog("Error: " + ex.Message);
- }
- }
- private void ExportToPDF(string xmlPath, string PDFPath, int PageCount, string Orientation)
- {
- try
- {
- WriteLog(PDFPath);
- FpSpread fpSpread = new FpSpread();
- WriteLog(PDFPath);
- fpSpread.Open(xmlPath);
- int count = fpSpread.Sheets.Count - PageCount;
- WriteLog(count.ToString());
- if (count > 0)
- {
- for (int i = 0; i < count; i++)
- {
- SheetView view = fpSpread.Sheets[i];
- PrintInfo printset = new PrintInfo();
- printset.PrintToPdf = true;
- printset.PdfWriteMode = PdfWriteMode.Append;
- printset.PdfWriteTo = PdfWriteTo.File;
- printset.UseMax = true;
- printset.ShowRowHeader = PrintHeader.Hide;
- printset.ShowColumnHeader = PrintHeader.Hide;
- printset.ShowGrid = false;
- printset.ShowBorder = false;
- printset.ShowShadows = false;
- printset.ShowColor = true;
- printset.BestFitRows = false;
- printset.BestFitCols = false;
- printset.PdfFileName = PDFPath;
- printset.Footer = "";
- printset.Header = "";
- printset.PrintType = PrintType.CellRange;
- printset.RowStart = 0;
- printset.ColStart = 0;
- printset.RowEnd = view.GetLastNonEmptyRow(NonEmptyItemFlag.Style | NonEmptyItemFlag.Data);
- printset.ColEnd = view.GetLastNonEmptyColumn(NonEmptyItemFlag.Style | NonEmptyItemFlag.Data);
- if (Orientation == "Landscape")
- {
- printset.Orientation = PrintOrientation.Landscape;
- }
- else
- {
- printset.Orientation = PrintOrientation.Portrait;
- }
- view.PrintInfo = printset;
- }
- fpSpread.PrintSheet(-1);
- }
- }
- catch (Exception ex)
- {
- WriteLog("Error: " + ex.Message);
- }
- }
- #endregion
- }
- }
复制代码 |
|