支持excel,word,ppt,pdf

  1. using Aspose.Cells;
  2. using Aspose.Words.Saving;
  3. using ESBasic;
  4. using OMCS.Engine.WhiteBoard;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Web;
  13. using System.Web.UI;
  14. using SMES.LocaleManagerWeb.DoucmentManager;
  15. using ImageSaveOptions = Aspose.Words.Saving.ImageSaveOptions;
  16. using SaveFormat = Aspose.Words.SaveFormat;
  17.  
  18. namespace SMES.LocaleManagerWeb
  19. {
  20. public partial class Upload : System.Web.UI.Page
  21. {
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. //new Aspose.Words.License().SetLicense(AsposeLicenseHelper.LStream);
  25. //new Aspose.Cells.License().SetLicense(AsposeLicenseHelper.LStream);
  26. //new Aspose.Slides.License().SetLicense(AsposeLicenseHelper.LStream);
  27. //new Aspose.Pdf.License().SetLicense(AsposeLicenseHelper.LStream);
  28. string documentExts = "pdf|doc|docx|xlsx|xls|ppt|pptx";
  29. string imageExts = "jpeg|png|jpg|bmp";
  30. string otherExts = "pdf|xls|xlsx|ppt|pptx";
  31. HttpFileCollection files = Request.Files;//这里只能用<input type="file" />才能有效果,因为服务器控件是HttpInputFile类型
  32.  
  33. FileUpModel model = new FileUpModel();
  34. if (files.Count == 0 || files[0].InputStream.Length == 0)
  35. {
  36. model = new FileUpModel() { State = 0, Error = "没有找到文件" };
  37. Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(model));
  38. //Response.End();
  39. return;
  40. }
  41.  
  42. try
  43. {
  44. HttpPostedFile file = files[0];
  45. model.Extension = GetExtension(file.FileName);
  46. if ((documentExts + "|" + imageExts + "|" + otherExts).IndexOf(model.Extension.ToLower()) == -1)
  47. {
  48. model = new FileUpModel() { State = 0, Error = "不允许上传" + model.Extension + "类型文件" };
  49. Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(model));
  50. //Response.End();
  51. return;
  52. }
  53.  
  54. model.Size = file.ContentLength / 1024;
  55. DateTime nowtime = DateTime.Now;
  56. string dic = "/UpLoadFile/" + nowtime.ToString("yy/MM/dd/");
  57. string newFilename = "/UpLoadFile/" + nowtime.ToString("yy/MM/dd/") + Guid.NewGuid().ToString("N") + "." + model.Extension;
  58. string filepath = Server.MapPath("~" + newFilename);
  59. if (!System.IO.Directory.Exists(Server.MapPath(dic)))
  60. {
  61. System.IO.Directory.CreateDirectory(Server.MapPath(dic));
  62. }
  63. file.SaveAs(filepath);
  64. //将文档装换为图图片
  65. if (documentExts.IndexOf(model.Extension.ToLower()) != -1)
  66. {
  67. //图片目录
  68. string imagedic = newFilename.Replace("." + model.Extension, "");
  69. IImageConverterFactory f = new ImageConverterFactory();
  70. IImageConverter imageConverter = f.CreateImageConverter("." + model.Extension);
  71. string opath = Server.MapPath("~" + newFilename);
  72. string imgpath = Server.MapPath("~" + imagedic + "/");
  73. Thread thd = new Thread(new ThreadStart(() => { imageConverter.ConvertToImage(opath, imgpath); }));
  74. thd.Start();
  75. //DocumentToImage(newFilename, imagedic);
  76. model.ImagePath = imagedic;
  77. }
  78.  
  79. model.State = 1;
  80. model.Path = newFilename;
  81.  
  82. Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(model));
  83.  
  84. }
  85. catch (Exception ee)
  86. {
  87. model = new FileUpModel() { State = 0, Error = ee.Message };
  88. Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(model));
  89. Response.End();
  90. }
  91. }
  92.  
  93. private void DocumentToImage(string filename, string imagedic)
  94. {
  95. Thread thd = new Thread(new ThreadStart(() =>
  96. {
  97. ConvertWordToImage(Server.MapPath("~" + filename), Server.MapPath("~" + imagedic + "/"), "A", 0, 0, ImageFormat.Png, 0);
  98. }));
  99. thd.Start();
  100.  
  101. }
  102.  
  103. private string GetExtension(string filename)
  104. {
  105. if (string.IsNullOrEmpty(filename))
  106. return "";
  107.  
  108. string[] strs = filename.Split('.');
  109.  
  110. if (strs.Length > 0)
  111. {
  112. return strs[strs.Length - 1];
  113. }
  114. else
  115. {
  116. return "";
  117. }
  118. }
  119.  
  120. public void ConvertWordToImage(string wordInputPath, string imageOutputPath,
  121. string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, float resolution)
  122. {
  123. try
  124. {
  125. AsposeLicenseHelper.SetWordsLicense();
  126. // open word file
  127. Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);
  128.  
  129. // validate parameter
  130. if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); }
  131. if (imageOutputPath.Trim().Length == 0) { imageOutputPath = Path.GetDirectoryName(wordInputPath); }
  132. if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
  133. if (imageName.Trim().Length == 0) { imageName = Path.GetFileNameWithoutExtension(wordInputPath); }
  134. if (startPageNum <= 0) { startPageNum = 1; }
  135. if (endPageNum > doc.PageCount || endPageNum <= 0) { endPageNum = doc.PageCount; }
  136. if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
  137. if (imageFormat == null) { imageFormat = ImageFormat.Png; }
  138. if (resolution <= 0) { resolution = 128; }
  139.  
  140. var imageSaveOptions = new ImageSaveOptions(GetSaveFormat(imageFormat))
  141. {
  142. Resolution = resolution
  143. };
  144.  
  145. // start to convert each page
  146. for (int i = startPageNum; i <= endPageNum; i++)
  147. {
  148. imageSaveOptions.PageIndex = i - 1;
  149. doc.Save(Path.Combine(imageOutputPath, imageName) + "_" + i.ToString() + "." + imageFormat.ToString(), imageSaveOptions);
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154.  
  155. }
  156. }
  157.  
  158. private SaveFormat GetSaveFormat(ImageFormat imageFormat)
  159. {
  160. SaveFormat sf = SaveFormat.Unknown;
  161. if (imageFormat.Equals(ImageFormat.Png))
  162. sf = SaveFormat.Png;
  163. else if (imageFormat.Equals(ImageFormat.Jpeg))
  164. sf = SaveFormat.Jpeg;
  165. else if (imageFormat.Equals(ImageFormat.Tiff))
  166. sf = SaveFormat.Tiff;
  167. else if (imageFormat.Equals(ImageFormat.Bmp))
  168. sf = SaveFormat.Bmp;
  169. else
  170. sf = SaveFormat.Unknown;
  171. return sf;
  172. }
  173.  
  174. }
  175.  
  176. public class FileUpModel
  177. {
  178. /// <summary>
  179. /// 状态 1成功 0失败
  180. /// </summary>
  181. public int State
  182. { get; set; }
  183.  
  184. /// <summary>
  185. /// 错误信息
  186. /// </summary>
  187. public string Error
  188. { get; set; }
  189.  
  190. /// <summary>
  191. /// 文件大小
  192. /// </summary>
  193. public decimal Size
  194. {
  195. get;
  196. set;
  197. }
  198.  
  199. /// <summary>
  200. /// 服务端路径
  201. /// </summary>
  202. public string Path
  203. { get; set; }
  204.  
  205. public string ImagePath
  206. { get; set; }
  207.  
  208. public string Extension
  209. { get; set; }
  210.  
  211. }
  212.  
  213. #region 图片转换器工厂 -> 将被注入到OMCS的多媒体管理器IMultimediaManager的ImageConverterFactory属性
  214. /// <summary>
  215. /// 图片转换器工厂。
  216. /// </summary>
  217. public class ImageConverterFactory : IImageConverterFactory
  218. {
  219. public IImageConverter CreateImageConverter(string extendName)
  220. {
  221. if (extendName == ".doc" || extendName == ".docx")
  222. {
  223. return new Word2ImageConverter();
  224. }
  225.  
  226. if (extendName == ".pdf")
  227. {
  228. return new Pdf2ImageConverter();
  229. }
  230.  
  231. if (extendName == ".ppt" || extendName == ".pptx")
  232. {
  233. return new Ppt2ImageConverter();
  234. }
  235. if (extendName == ".xls" || extendName == ".xlsx")
  236. {
  237. return new Xls2ImageConverter();
  238. }
  239. return null;
  240. }
  241.  
  242. public bool Support(string extendName)
  243. {
  244. return extendName == ".doc" || extendName == ".docx" || extendName == ".pdf" || extendName == ".ppt" || extendName == ".pptx" || extendName == ".rar";
  245. }
  246. }
  247. #endregion
  248.  
  249. #region 将word文档转换为图片
  250. public class Word2ImageConverter : IImageConverter
  251. {
  252. private bool cancelled = false;
  253. public event CbGeneric<int, int> ProgressChanged;
  254. public event CbGeneric ConvertSucceed;
  255. public event CbGeneric<string> ConvertFailed;
  256.  
  257. public void Cancel()
  258. {
  259. if (this.cancelled)
  260. {
  261. return;
  262. }
  263.  
  264. this.cancelled = true;
  265. }
  266.  
  267. public void ConvertToImage(string originFilePath, string imageOutputDirPath)
  268. {
  269. this.cancelled = false;
  270. ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, null, 200);
  271. }
  272.  
  273. /// <summary>
  274. /// 将Word文档转换为图片的方法
  275. /// </summary>
  276. /// <param name="wordInputPath">Word文件路径</param>
  277. /// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为Word所在路径</param>
  278. /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
  279. /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数</param>
  280. /// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param>
  281. /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
  282. private void ConvertToImage(string wordInputPath, string imageOutputDirPath, int startPageNum, int endPageNum, ImageFormat imageFormat, int resolution)
  283. {
  284. try
  285. {
  286.  
  287. AsposeLicenseHelper.SetWordsLicense();
  288. Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);
  289.  
  290. if (doc == null)
  291. {
  292. throw new Exception("Word文件无效或者Word文件被加密!");
  293. }
  294.  
  295. if (imageOutputDirPath.Trim().Length == 0)
  296. {
  297. imageOutputDirPath = Path.GetDirectoryName(wordInputPath);
  298. }
  299.  
  300. if (!Directory.Exists(imageOutputDirPath))
  301. {
  302. Directory.CreateDirectory(imageOutputDirPath);
  303. }
  304.  
  305. if (startPageNum <= 0)
  306. {
  307. startPageNum = 1;
  308. }
  309.  
  310. if (endPageNum > doc.PageCount || endPageNum <= 0)
  311. {
  312. endPageNum = doc.PageCount;
  313. }
  314.  
  315. if (startPageNum > endPageNum)
  316. {
  317. int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;
  318. }
  319.  
  320. if (imageFormat == null)
  321. {
  322. imageFormat = ImageFormat.Png;
  323. }
  324.  
  325. if (resolution <= 0)
  326. {
  327. resolution = 128;
  328. }
  329. string imageName = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(wordInputPath));
  330. //string imageName = Path.GetFileNameWithoutExtension(wordInputPath);
  331. Aspose.Words.Saving.ImageSaveOptions imageSaveOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png);
  332. imageSaveOptions.Resolution = resolution;
  333. for (int i = startPageNum; i <= endPageNum; i++)
  334. {
  335. if (this.cancelled)
  336. {
  337. break;
  338. }
  339.  
  340. MemoryStream stream = new MemoryStream();
  341. imageSaveOptions.PageIndex = i - 1;
  342. //string imgPath = Path.Combine(imageOutputDirPath, imageName) + "_" + i.ToString("000") + "." + imageFormat.ToString();
  343. string imgPath = Path.Combine(imageOutputDirPath, "A") + "_" + i + ".Png";
  344. doc.Save(stream, imageSaveOptions);
  345. System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
  346. Bitmap bm = ESBasic.Helpers.ImageHelper.Zoom(img, 0.6f);
  347. bm.Save(imgPath, imageFormat);
  348. img.Dispose();
  349. stream.Dispose();
  350. bm.Dispose();
  351.  
  352. System.Threading.Thread.Sleep(200);
  353. if (this.ProgressChanged != null)
  354. {
  355. this.ProgressChanged(i - 1, endPageNum);
  356. }
  357. }
  358.  
  359. if (this.cancelled)
  360. {
  361. return;
  362. }
  363.  
  364. if (this.ConvertSucceed != null)
  365. {
  366. this.ConvertSucceed();
  367. }
  368. }
  369. catch (Exception ex)
  370. {
  371. if (this.ConvertFailed != null)
  372. {
  373. this.ConvertFailed(ex.Message);
  374. }
  375. }
  376. }
  377. }
  378. #endregion
  379.  
  380. #region 将pdf文档转换为图片
  381. public class Pdf2ImageConverter : IImageConverter
  382. {
  383. private bool cancelled = false;
  384. public event CbGeneric<int, int> ProgressChanged;
  385. public event CbGeneric ConvertSucceed;
  386. public event CbGeneric<string> ConvertFailed;
  387.  
  388. public void Cancel()
  389. {
  390. if (this.cancelled)
  391. {
  392. return;
  393. }
  394.  
  395. this.cancelled = true;
  396. }
  397.  
  398. public void ConvertToImage(string originFilePath, string imageOutputDirPath)
  399. {
  400. this.cancelled = false;
  401. ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, 200);
  402. }
  403.  
  404. /// <summary>
  405. /// 将pdf文档转换为图片的方法
  406. /// </summary>
  407. /// <param name="originFilePath">pdf文件路径</param>
  408. /// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为pdf所在路径</param>
  409. /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
  410. /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为pdf总页数</param>
  411. /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
  412. private void ConvertToImage(string originFilePath, string imageOutputDirPath, int startPageNum, int endPageNum, int resolution)
  413. {
  414. try
  415. {
  416. AsposeLicenseHelper.SetPdfLicense();
  417. Aspose.Pdf.Document doc = new Aspose.Pdf.Document(originFilePath);
  418.  
  419. if (doc == null)
  420. {
  421. throw new Exception("pdf文件无效或者pdf文件被加密!");
  422. }
  423.  
  424. if (imageOutputDirPath.Trim().Length == 0)
  425. {
  426. imageOutputDirPath = Path.GetDirectoryName(originFilePath);
  427. }
  428.  
  429. if (!Directory.Exists(imageOutputDirPath))
  430. {
  431. Directory.CreateDirectory(imageOutputDirPath);
  432. }
  433.  
  434. if (startPageNum <= 0)
  435. {
  436. startPageNum = 1;
  437. }
  438.  
  439. if (endPageNum > doc.Pages.Count || endPageNum <= 0)
  440. {
  441. endPageNum = doc.Pages.Count;
  442. }
  443.  
  444. if (startPageNum > endPageNum)
  445. {
  446. int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;
  447. }
  448.  
  449. if (resolution <= 0)
  450. {
  451. resolution = 128;
  452. }
  453.  
  454. string imageNamePrefix = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(originFilePath));
  455. for (int i = startPageNum; i <= endPageNum; i++)
  456. {
  457. if (this.cancelled)
  458. {
  459. break;
  460. }
  461.  
  462. MemoryStream stream = new MemoryStream();
  463. //string imgPath = Path.Combine(imageOutputDirPath, imageNamePrefix) + "_" + i.ToString("000") + ".Png";
  464. string imgPath = Path.Combine(imageOutputDirPath, "A") + "_" + i + ".Png";
  465. Aspose.Pdf.Devices.Resolution reso = new Aspose.Pdf.Devices.Resolution(resolution);
  466. Aspose.Pdf.Devices.JpegDevice jpegDevice = new Aspose.Pdf.Devices.JpegDevice(reso, 100);
  467. jpegDevice.Process(doc.Pages[i], stream);
  468.  
  469. Image img = Image.FromStream(stream);
  470. Bitmap bm = ESBasic.Helpers.ImageHelper.Zoom(img, 0.6f);
  471. bm.Save(imgPath, ImageFormat.Jpeg);
  472. img.Dispose();
  473. stream.Dispose();
  474. bm.Dispose();
  475.  
  476. System.Threading.Thread.Sleep(200);
  477. if (this.ProgressChanged != null)
  478. {
  479. this.ProgressChanged(i - 1, endPageNum);
  480. }
  481. }
  482.  
  483. if (this.cancelled)
  484. {
  485. return;
  486. }
  487.  
  488. if (this.ConvertSucceed != null)
  489. {
  490. this.ConvertSucceed();
  491. }
  492. }
  493. catch (Exception ex)
  494. {
  495. if (this.ConvertFailed != null)
  496. {
  497. this.ConvertFailed(ex.Message);
  498. }
  499. }
  500. }
  501. }
  502. #endregion
  503.  
  504. #region 将ppt文档转换为图片
  505. public class Ppt2ImageConverter : IImageConverter
  506. {
  507. private Pdf2ImageConverter pdf2ImageConverter;
  508. public event CbGeneric<int, int> ProgressChanged;
  509. public event CbGeneric ConvertSucceed;
  510. public event CbGeneric<string> ConvertFailed;
  511.  
  512. public void Cancel()
  513. {
  514. if (this.pdf2ImageConverter != null)
  515. {
  516. this.pdf2ImageConverter.Cancel();
  517. }
  518. }
  519.  
  520. public void ConvertToImage(string originFilePath, string imageOutputDirPath)
  521. {
  522. ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, 200);
  523. }
  524.  
  525. /// <summary>
  526. /// 将pdf文档转换为图片的方法
  527. /// </summary>
  528. /// <param name="originFilePath">ppt文件路径</param>
  529. /// <param name="imageOutputDirPath">图片输出路径,如果为空,默认值为pdf所在路径</param>
  530. /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
  531. /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为pdf总页数</param>
  532. /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
  533. private void ConvertToImage(string originFilePath, string imageOutputDirPath, int startPageNum, int endPageNum, int resolution)
  534. {
  535. try
  536. {
  537. AsposeLicenseHelper.SetSlidesLicense();
  538. Aspose.Slides.Presentation doc = new Aspose.Slides.Presentation(originFilePath);
  539.  
  540. if (doc == null)
  541. {
  542. throw new Exception("ppt文件无效或者ppt文件被加密!");
  543. }
  544.  
  545. if (imageOutputDirPath.Trim().Length == 0)
  546. {
  547. imageOutputDirPath = Path.GetDirectoryName(originFilePath);
  548. }
  549.  
  550. if (!Directory.Exists(imageOutputDirPath))
  551. {
  552. Directory.CreateDirectory(imageOutputDirPath);
  553. }
  554.  
  555. if (startPageNum <= 0)
  556. {
  557. startPageNum = 1;
  558. }
  559.  
  560. if (endPageNum > doc.Slides.Count || endPageNum <= 0)
  561. {
  562. endPageNum = doc.Slides.Count;
  563. }
  564.  
  565. if (startPageNum > endPageNum)
  566. {
  567. int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;
  568. }
  569.  
  570. if (resolution <= 0)
  571. {
  572. resolution = 128;
  573. }
  574.  
  575. //先将ppt转换为pdf临时文件
  576. string tmpPdfPath = originFilePath + ".pdf";
  577. doc.Save(tmpPdfPath, Aspose.Slides.Export.SaveFormat.Pdf);
  578.  
  579. //再将pdf转换为图片
  580. Pdf2ImageConverter converter = new Pdf2ImageConverter();
  581. converter.ConvertFailed += new CbGeneric<string>(converter_ConvertFailed);
  582. converter.ConvertSucceed += new CbGeneric(converter_ConvertSucceed);
  583. converter.ProgressChanged += new CbGeneric<int, int>(converter_ProgressChanged);
  584. converter.ConvertToImage(tmpPdfPath, imageOutputDirPath);
  585.  
  586. //删除pdf临时文件
  587. File.Delete(tmpPdfPath);
  588.  
  589. if (this.ConvertSucceed != null)
  590. {
  591. this.ConvertSucceed();
  592. }
  593. }
  594. catch (Exception ex)
  595. {
  596. if (this.ConvertFailed != null)
  597. {
  598. this.ConvertFailed(ex.Message);
  599. }
  600. }
  601.  
  602. this.pdf2ImageConverter = null;
  603. }
  604.  
  605. void converter_ProgressChanged(int done, int total)
  606. {
  607. if (this.ProgressChanged != null)
  608. {
  609. this.ProgressChanged(done, total);
  610. }
  611. }
  612.  
  613. void converter_ConvertSucceed()
  614. {
  615. if (this.ConvertSucceed != null)
  616. {
  617. this.ConvertSucceed();
  618. }
  619. }
  620.  
  621. void converter_ConvertFailed(string msg)
  622. {
  623. if (this.ConvertFailed != null)
  624. {
  625. this.ConvertFailed(msg);
  626. }
  627. }
  628. }
  629. #endregion
  630.  
  631. #region Excel
  632. public class Xls2ImageConverter : IImageConverter
  633. {
  634. private Xls2ImageConverter xls2ImageConverter;
  635. public event CbGeneric<int, int> ProgressChanged;
  636. public event CbGeneric ConvertSucceed;
  637. public event CbGeneric<string> ConvertFailed;
  638.  
  639. public void Cancel()
  640. {
  641. if (this.xls2ImageConverter != null)
  642. {
  643. this.xls2ImageConverter.Cancel();
  644. }
  645. }
  646.  
  647. public void ConvertToImage(string originFilePath, string imageOutputDirPath)
  648. {
  649. ConvertToImage(originFilePath, imageOutputDirPath, 0, 0, 200);
  650. }
  651.  
  652. private void ConvertToImage(string originFilePath, string imageOutputDirPath, int startPageNum, int endPageNum, int resolution)
  653. {
  654. try
  655. {
  656. AsposeLicenseHelper.SetCellsLicense();
  657. Aspose.Cells.Workbook excel = new Workbook(originFilePath);
  658.  
  659. if (excel == null)
  660. {
  661. throw new Exception("excel文件无效或者excel文件被加密!");
  662. }
  663.  
  664. if (imageOutputDirPath.Trim().Length == 0)
  665. {
  666. imageOutputDirPath = Path.GetDirectoryName(originFilePath);
  667. }
  668.  
  669. if (!Directory.Exists(imageOutputDirPath))
  670. {
  671. Directory.CreateDirectory(imageOutputDirPath);
  672. }
  673.  
  674. if (startPageNum <= 0)
  675. {
  676. startPageNum = 1;
  677. }
  678.  
  679. if (endPageNum > excel.Worksheets.Count || endPageNum <= 0)
  680. {
  681. endPageNum = excel.Worksheets.Count;
  682. }
  683.  
  684. if (startPageNum > endPageNum)
  685. {
  686. int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum;
  687. }
  688.  
  689. if (resolution <= 0)
  690. {
  691. resolution = 128;
  692. }
  693.  
  694. //先将excel转换为pdf临时文件
  695. string tmpPdfPath = originFilePath + ".pdf";
  696. excel.Save(tmpPdfPath, Aspose.Cells.SaveFormat.Pdf);
  697.  
  698. //再将pdf转换为图片
  699. Pdf2ImageConverter converter = new Pdf2ImageConverter();
  700. converter.ConvertFailed += new CbGeneric<string>(converter_ConvertFailed);
  701. converter.ConvertSucceed += new CbGeneric(converter_ConvertSucceed);
  702. converter.ProgressChanged += new CbGeneric<int, int>(converter_ProgressChanged);
  703. converter.ConvertToImage(tmpPdfPath, imageOutputDirPath);
  704.  
  705. //删除pdf临时文件
  706. File.Delete(tmpPdfPath);
  707.  
  708. if (this.ConvertSucceed != null)
  709. {
  710. this.ConvertSucceed();
  711. }
  712. }
  713. catch (Exception ex)
  714. {
  715. if (this.ConvertFailed != null)
  716. {
  717. this.ConvertFailed(ex.Message);
  718. }
  719. }
  720.  
  721. this.xls2ImageConverter = null;
  722. }
  723.  
  724. void converter_ProgressChanged(int done, int total)
  725. {
  726. if (this.ProgressChanged != null)
  727. {
  728. this.ProgressChanged(done, total);
  729. }
  730. }
  731.  
  732. void converter_ConvertSucceed()
  733. {
  734. if (this.ConvertSucceed != null)
  735. {
  736. this.ConvertSucceed();
  737. }
  738. }
  739.  
  740. void converter_ConvertFailed(string msg)
  741. {
  742. if (this.ConvertFailed != null)
  743. {
  744. this.ConvertFailed(msg);
  745. }
  746. }
  747. }
  748. #endregion
  749. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Mime;
  6. using System.Web;
  7. using Aspose.Words;
  8.  
  9. namespace SMES.LocaleManagerWeb.DoucmentManager
  10. {
  11. public static class AsposeLicenseHelper
  12. {
  13. public const string Key =
  14. "PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
  15. "UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
  16. "9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
  17. "HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
  18. "dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
  19. "wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
  20. "A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
  21. "m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
  22. "dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
  23. "0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
  24. "djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
  25. "jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
  26. "PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
  27. "3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
  28. "FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
  29. "zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
  30. "aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
  31. "Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
  32. "V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";
  33. public static Stream LStream = (Stream)new MemoryStream(Convert.FromBase64String(Key));
  34.  
  35. static readonly string LicensePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\DoucmentManager\\Aspose.Total.lic";
  36. public static void SetWordsLicense()
  37. {
  38. var l=new License();
  39. l.SetLicense(LicensePath);
  40. }
  41. public static void SetPdfLicense()
  42. {
  43. var l = new Aspose.Pdf.License();
  44. l.SetLicense(LicensePath);
  45. }
  46. public static void SetSlidesLicense()
  47. {
  48. var l = new Aspose.Slides.License();
  49. l.SetLicense(LicensePath);
  50. }
  51. /// <summary>
  52. /// Excel
  53. /// </summary>
  54. public static void SetCellsLicense()
  55. {
  56. var l = new Aspose.Cells.License();
  57. l.SetLicense(LicensePath);
  58. }
  59. }
  60. }

下载地址

aspose授权亲测可用配套代码的更多相关文章

  1. jdbc连接数据库以及crud(简单易懂,本人亲测可用 有源代码和数据库)

    今天呢!重新整理了一边jdbc的相关操作:现在来说对于很多框架都使用mybatis和hibernate来操作数据库 ,也有很多使用自己简单封装的ssm或者是其他的一些框架来操作数据库,但是无论使用哪一 ...

  2. 亲测可用!!!golang如何在idea中保存时自动进行代码格式化

    亲测可用,golang在idea中的代码自动格式化 1.ctrl+alt+s打开设置界面,选择[Plugins] -> [Install JetBrains plugin...] -> 搜 ...

  3. Cocos Creator JS web平台复制粘贴代码(亲测可用)

    Cocos Creator JS web平台复制粘贴代码(亲测可用) 1 webCopyString: function(str){ var input = str; const el = docum ...

  4. PHP小程序后端支付代码亲测可用

    小程序后端支付代码亲测可用 <?php namespace Home\Controller; use Think\Controller; class WechatpayController ex ...

  5. mybatis自动生成代码插件mybatis-generator使用流程(亲测可用)

    mybatis-generator是一款在使用mybatis框架时,自动生成model,dao和mapper的工具,很大程度上减少了业务开发人员的手动编码时间 坐着在idea上用maven构建spri ...

  6. Axure RP 9版本最新版授权码和密钥 亲测可用

    分享Axure RP 9版本最新版授权码和密钥 亲测可用 声明:以下资源的获取来源为网络收集,仅供学习参考,不作商业用途,如有侵权请联系博主删除,谢谢! 自新的Axure RP 9.0 Beta版发布 ...

  7. IntelliJ13+tomcat+jrebel实现热部署(亲测可用)

       网上有很多介绍intellij idea整合jrebel插件实现热部署的文章,但是有的比较复杂,有的不能成功,最后经过各种尝试,实现了整合,亲测可用!步骤说明如下:   一.先下载jrebel安 ...

  8. IntelliJ IDEA2017 激活方法 最新的(亲测可用)

    IntelliJ IDEA2017 激活方法(亲测可用): 搭建自己的授权服务器,对大佬来说也很简单,我作为菜鸟就不说了,网上有教程. 我主要说第二种,现在,直接写入注册码,是不能成功激活的(如果你成 ...

  9. [转]QT子线程与主线程的信号槽通信-亲测可用!

    近用QT做一个服务器,众所周知,QT的主线程必须保持畅通,才能刷新UI.所以,网络通信端采用新开线程的方式.在涉及到使用子线程更新Ui上的控件时遇到了点儿麻烦.网上提供了很多同一线程不同类间采用信号槽 ...

随机推荐

  1. FastDFS安装、配置、部署(三)-Storage配置具体解释

    1.基本配置 # is this config file disabled # false for enabled # true for disabled disabled=false # the n ...

  2. IEditableObject的一个通用实现

    原文:IEditableObject的一个通用实现 IeditableObject是一个通用接口,用于支持对象编辑.当我们在界面上选择一个条目,然后对其进行编辑的时候,接下来会有两种操作,一个是保持编 ...

  3. 回调函数实现类似QT中信号机制

    1. 定义回调接口类: class UIcallBack { public: virtual void onAppActivated() = 0; virtual void onShowMore()  ...

  4. hann function

    hann function 是一种离散型窗函数,定义如下: w(n)=12(1−cos(2πnN−1))=sin2(πnN−1) 窗口的长度为 L=N+1; hann function 以及其傅里叶响 ...

  5. EF 导航属性的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  6. JDK源码阅读——LinkedList实现

    1 继承结构图 LinkedList是List的另一种实现.继承自AbstractSequentialList 2 数据结构 LinkedList与ArrayList不同的是LinkedList底层使 ...

  7. JAVASCRIPT高程笔记-------第五章 引用类型

    一.Object类型 1.1创建方式 ①new关键字 : var person = new Oject(); ②给定直接量: var person = { name : "zhangsan& ...

  8. WPF RichTextBox 导出与加载

    private void Button_Click(object sender, RoutedEventArgs e) { string savePth = System.IO.Path.Combin ...

  9. 从一段简单算法题来谈二叉查找树(BST)的基础算法

    先给出一道很简单,喜闻乐见的二叉树算法题: 给出一个二叉查找树和一个目标值,如果其中有两个元素的和等于目标值则返回真,否则返回假. 例如: Input: 5 / \ 3 6 / \ \ 2 4 7 T ...

  10. WPF 为资源字典 添加事件响应的后台类

    原文:WPF 为资源字典 添加事件响应的后台类 前言,有许多同学在写WPF程序时在资源字典里加入了其它控件,但又想写事件来控制这个控件,但是资源字典没有CS文件,不像窗体XAML还有一个后台的CS文件 ...