/MvcApplication/Common/FileCommonOperations.cs

http://zsglsolution.googlecode.com/ · C# · 203 lines · 173 code · 8 blank · 22 comment · 13 complexity · 85202d0023722935359d41e08cf471a8 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Word = Microsoft.Office.Interop.Word;
  6. using System.Reflection;
  7. using Excel = Microsoft.Office.Interop.Excel;
  8. using PPT = Microsoft.Office.Interop.PowerPoint;
  9. using Microsoft.Office.Core;
  10. using System.Data.OleDb;
  11. using System.Data;
  12. namespace MvcApplication
  13. {
  14. public class FileCommonOperations
  15. {
  16. static object path;
  17. static object saveFileName;
  18. #region ??PDF
  19. /// <summary>
  20. /// word???pdf
  21. /// </summary>
  22. /// <param name="filePath"></param>
  23. public static void Word2Pdf(string filePath)
  24. {
  25. path = filePath;
  26. saveFileName = filePath + ".pdf";
  27. var word = new Word.Application();
  28. Type wordType = word.GetType();
  29. Word.Documents docs = word.Documents;
  30. var docsType = docs.GetType();
  31. var doc = (Word.Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs,
  32. new Object[] { path, true, true });
  33. Type docType = doc.GetType();
  34. docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc,
  35. new object[] { saveFileName, Word.WdSaveFormat.wdFormatPDF });
  36. wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);
  37. }
  38. /// <summary>
  39. /// excel???pdf
  40. /// </summary>
  41. /// <param name="filePath"></param>
  42. public static void Excel2Pdf(string filePath)
  43. {
  44. path = filePath;
  45. saveFileName = filePath + ".pdf";
  46. var xlsx = new Excel.Application();
  47. Excel.Workbook wb = xlsx.Workbooks.Open(path.ToString(),
  48. Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  49. Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  50. Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  51. wb.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, saveFileName, Excel.XlFixedFormatQuality.xlQualityStandard,
  52. true, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  53. wb.Close(false, Type.Missing, Type.Missing);
  54. wb = null;
  55. xlsx.Quit();
  56. xlsx = null;
  57. }
  58. /// <summary>
  59. /// ppt???pdf
  60. /// </summary>
  61. /// <param name="filePath"></param>
  62. public static void PPT2Pdf(string filePath)
  63. {
  64. path = filePath;
  65. saveFileName = filePath + ".pdf";
  66. var ppt = new PPT.Application();
  67. var doc = ppt.Presentations.Open(path.ToString(),
  68. Microsoft.Office.Core.MsoTriState.msoFalse,
  69. Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
  70. doc.SaveAs(saveFileName.ToString(), PPT.PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
  71. doc.Close();
  72. doc = null;
  73. ppt.Quit();
  74. ppt = null;
  75. }
  76. #endregion
  77. #region ????
  78. public static List<string> GetWordContent(string filePath)
  79. {
  80. try
  81. {
  82. List<string> allContent = new List<string>();
  83. string content = string.Empty;
  84. path = filePath;
  85. var word = new Word.Application();
  86. Type wordType = word.GetType();
  87. Word.Documents docs = word.Documents;
  88. var docsType = docs.GetType();
  89. var doc = (Word.Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, docs,
  90. new Object[] { path, true, true });
  91. for (int i = 0; i < doc.Paragraphs.Count; i++)
  92. {
  93. if (content.Length <= 300)
  94. content += doc.Paragraphs[i+1].Range.Text;
  95. else
  96. {
  97. allContent.Add(content);
  98. content = "";
  99. content += doc.Paragraphs[i+1].Range.Text;
  100. }
  101. }
  102. if (allContent.Count == 0)
  103. allContent.Add(content);
  104. Type docType = doc.GetType();
  105. docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc,
  106. new object[] { saveFileName, Word.WdSaveFormat.wdFormatPDF });
  107. wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);
  108. return allContent;
  109. }
  110. catch(Exception e)
  111. {
  112. return null;
  113. }
  114. }
  115. /// <summary>
  116. /// ??Excel??
  117. /// </summary>
  118. /// <param name="filePath"></param>
  119. /// <returns></returns>
  120. public static List<string> GetExcelContent(string filePath)
  121. {
  122. path = filePath;
  123. List<string> allContent = new List<string>();
  124. string content = string.Empty;
  125. Excel.Application app = new Excel.Application();
  126. app.DisplayAlerts = false;
  127. Excel.Workbooks wbs = app.Workbooks;
  128. Excel.Workbook wb =
  129. wbs.Open(path.ToString(), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
  130. Type.Missing, Excel.XlPlatform.xlWindows, Type.Missing,
  131. Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  132. string sss = string.Empty;
  133. for (int i = 0; i < wb.Worksheets.Count; i++)
  134. {
  135. Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[i + 1];
  136. for (int j = 0; j < s.UsedRange.Cells.Rows.Count; j++)
  137. {
  138. for (int k = 0; k < s.UsedRange.Cells.Columns.Count; k++)
  139. {
  140. if (content.Length <= 300)
  141. content += ((Excel.Range)s.Cells[j + 1, k + 1]).Text.ToString();
  142. else
  143. {
  144. allContent.Add(content);
  145. content = "";
  146. content += ((Excel.Range)s.Cells[j + 1, k + 1]).Text.ToString();
  147. }
  148. }
  149. }
  150. if (allContent.Count == 0)
  151. allContent.Add(content);
  152. }
  153. wb.Close(false, Type.Missing, Type.Missing);
  154. app.Quit();
  155. System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
  156. app = null;
  157. return allContent;
  158. }
  159. /// <summary>
  160. /// ??PPT??
  161. /// </summary>
  162. /// <param name="filePath"></param>
  163. /// <returns></returns>
  164. public static List<string> GetPPTContent(string filePath)
  165. {
  166. path = filePath;
  167. List<string> allContent = new List<string>();
  168. string content = string.Empty;
  169. var ppt = new PPT.Application();
  170. var doc = ppt.Presentations.Open(path.ToString(),
  171. Microsoft.Office.Core.MsoTriState.msoCTrue,
  172. Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
  173. int Slides = doc.Slides.Count;
  174. foreach (PPT.Slide slide in doc.Slides)
  175. {
  176. foreach (PPT.Shape shape in slide.Shapes)
  177. {
  178. if (content.Length <= 300)
  179. content += shape.TextFrame.TextRange.Text;
  180. else
  181. {
  182. allContent.Add(content);
  183. content = "";
  184. content += shape.TextFrame.TextRange.Text;
  185. }
  186. }
  187. if (allContent.Count == 0)
  188. allContent.Add(content);
  189. }
  190. ppt.Quit();
  191. return allContent;
  192. }
  193. #endregion
  194. }
  195. }