资源描述
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Text;
/// <summary>
///FileConvert 的摘要说明
/// </summary>
public class FileConvert
{
public FileConvert()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>XLS文件转换为PDF(服务器端需安装office2010方能实现转换)
///
/// </summary>
/// <param name="sourceFile">要转换的文件物理路径</param>
/// <param name="targetFile">生成的文件物理路径</param>
/// <returns></returns>
public static bool XLSConvertToPDF(string sourceFile, string targetFile)
{
string sourcePath = sourceFile;
string targetPath = targetFile;
bool result = false;
Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;// Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
#region 设置打印属性
Excel.Worksheet xlSheet = (Excel.Worksheet)workBook.Worksheets[1];
xlSheet.PageSetup.Orientation = XlPageOrientation.xlLandscape;
xlSheet.PageSetup.HeaderMargin = 1;
xlSheet.PageSetup.LeftMargin = 0;
xlSheet.PageSetup.RightMargin = 0;
xlSheet.PageSetup.TopMargin = 1;
xlSheet.PageSetup.CenterHorizontally = true;
double pageWidth = Convert.ToDouble(xlSheet.UsedRange.Columns.Width);
if (pageWidth > 1200)
xlSheet.PageSetup.PaperSize = XlPaperSize.xlPaperCsheet;
else if (pageWidth > 1000)
xlSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA3;
else if (pageWidth > 800)
xlSheet.PageSetup.PaperSize = XlPaperSize.xlPaper10x14;
else
xlSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4;
#endregion
if (workBook != null)
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, true, 1, xlSheet.PageSetup.Pages.Count, true, missing);
result = true;
}
catch (Exception ex)
{
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
/// <summary>转为PDF文件为SWF
///
/// </summary>
/// <param name="pdfPath">PDF文件的物理路径</param>
/// <param name="swfPath">SWF文件的物理路径</param>
public static void ConvertToSwf(string pdfPath, string swfPath)
{
try
{
//string exe = HttpContext.Current.Server.MapPath("PDF2SWF/pdf2swf.exe");
//string exe = @"d:\SWFTools\pdf2swf.exe ";
string exe = System.Configuration.ConfigurationManager.AppSettings["toolRoot"].ToString();//获取转换工具的安装路径
if (!File.Exists(exe))
{
throw new ApplicationException("Can not find: " + exe);
}
StringBuilder sb = new StringBuilder();
sb.Append(" -o \"" + swfPath + "\"");//output
sb.Append(" -z");
sb.Append(" -s flashversion=9");//flash version
sb.Append(" -s disablelinks");//禁止PDF里面的链接
//sb.Append(" -p " + "1" + "-" + page);//page range
sb.Append(" -j 100");//Set quality of embedded jpeg pictures to quality. 0 is worst (small), 100 is best (big). (default:85)
sb.Append(" \"" + pdfPath + "\"");//input
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = exe;
proc.StartInfo.Arguments = sb.ToString();
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
proc.Close();
proc.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
}
展开阅读全文