1、 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.
2、Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Text;
///
3、服务器端需安装office2010方能实现转换)
///
///
/// 要转换的文件物理路径
/// 生成的文件物理路径
///
4、h = sourceFile; string targetPath = targetFile; bool result = false; Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;// Excel.XlFixedFormatType.xlTypePDF; object missing = Type.Missing; Excel.ApplicationClas
5、s application = null; Excel.Workbook workBook = null; try { application = new Excel.ApplicationClass(); object target = targetPath; object type = targetType; workBook = application.Workbooks.Open(sourcePath, missing, miss
6、ing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); #region 设置打印属性 Excel.Worksheet xlSheet = (Excel.Worksheet)workBook.Worksheets[1]; xlSheet.PageSetup.Orientation = XlPageOrient
7、ation.xlLandscape; xlSheet.PageSetup.HeaderMargin = 1; xlSheet.PageSetup.LeftMargin = 0; xlSheet.PageSetup.RightMargin = 0; xlSheet.PageSetup.TopMargin = 1; xlSheet.PageSetup.CenterHorizontally = true; double pageWidth = C
8、onvert.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
9、 > 800) xlSheet.PageSetup.PaperSize = XlPaperSize.xlPaper10x14; else xlSheet.PageSetup.PaperSize = XlPaperSize.xlPaperA4; #endregion if (workBook != null) workBook.ExportAsFixedFormat(targetType, target, Excel.
10、XlFixedFormatQuality.xlQualityStandard, true, true, 1, xlSheet.PageSetup.Pages.Count, true, missing); result = true; } catch (Exception ex) { result = false; } finally { if (workBook != null) {
11、 workBook.Close(true, missing, missing); workBook = null; } if (application != null) { application.Quit(); application = null; } GC.Collect(); GC.WaitForPe
12、ndingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
///
13、ublic 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.Configurati
14、onManager.AppSettings["toolRoot"].ToString();//获取转换工具的安装路径 if (!File.Exists(exe)) { throw new ApplicationException("Can not find: " + exe); } StringBuilder sb = new StringBuilder(); sb.Append(" -o \"" + swfPath + "\"")
15、//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 pict
16、ures 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.ToSt
17、ring(); proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; proc.Start(); proc.WaitForExit(); proc.Close(); proc.Dispose(); } catch (Exception ex) { throw ex; } } }






