收藏 分销(赏)

不同类型的文件转换C#.doc

上传人:仙人****88 文档编号:12021666 上传时间:2025-08-28 格式:DOC 页数:4 大小:38.50KB 下载积分:10 金币
下载 相关 举报
不同类型的文件转换C#.doc_第1页
第1页 / 共4页
不同类型的文件转换C#.doc_第2页
第2页 / 共4页


点击查看更多>>
资源描述
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; } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服