關於:excel和word 預覽 請看我的博文:
excel和word 在線預覽 詳細配置及代碼
使用Adobe 組件 在本機vs中調試成功
發布到iis中 在 代碼中涉及到 剪貼板的地方 會不成功 莫名其妙的沒有數據 也不報錯 設置iis權限 、com組件權限+各種搞 均無果..... -_-! 很是郁悶
最終放棄
改用 Ghostscript
須安裝 gs861w32.exe (高版本 貌似 還有問題......)
在安裝目錄 bin 下 找到gsdll32.dll
下在 dll
itextsharp.dll
PDFView.dll
把三個dll放入 項目dll(新建)文件夾中
引入
itextsharp.dll
PDFView.dll
gsdll32.dll無法引入 拷貝到項目bin 目錄下
上代碼:
View Code
1 /// <summary>
2 /// 將PDF 相應的頁轉換為圖片
3 /// </summary>
4 /// <param name="strPDFpath">PDF 路徑</param>
5 /// <param name="Page">需要轉換的頁頁碼</param>
6 private string GetImage(string strPDFpath, string imgDire, ImageFormat imgeF)
7 {
8 StringBuilder b = new StringBuilder();
9
10 PdfReader reader = new PdfReader(strPDFpath);
11 // 獲得文檔頁數
12 int pageCount = reader.NumberOfPages;
13
14 System.IO.MemoryStream Ms = new MemoryStream();
15 try
16 {
17 b = b.AppendLine("<ul style='azimuth:center; list-style-type:none;' >");
18 for (int page = 1; page <= pageCount; page++)
19 {
20 System.Drawing.Image img = PDFView.ConvertPDF.PDFConvert.GetPageFromPDF(strPDFpath, page, 100, "", true);
21 img.Save(Ms, imgeF);
22
23 Bitmap returnImage = (Bitmap)Bitmap.FromStream(Ms);
24
25 string strImgPath = Request.MapPath("..\\" + imgDire + "\\" + page.ToString("0000") + ".jpg");
26
27 returnImage.Save(strImgPath);
28
29 Ms.Position = 0;
30
31
32 b = b.AppendLine("<li> <img src='..\\" + imgDire + "\\" + page.ToString("0000") + ".jpg' /> </li><span>第" + (page) + "頁</span>");
33 }
34
35 Ms.Close();
36 b = b.AppendLine("</ul>");
37
38 }
39 catch (Exception ex)
40 {
41 // b.Clear();
42 // b.AppendLine(ex.ToString());
43 throw;
44 }
45
46
47 return b.ToString();
48
49
50 }
vs 運行 成功
發布到服務器iis 中 設置 iis 對應應用程序池 啟用32為應用程序 為true
-_-! 報錯
System.InvalidOperationException: 當應用程序不是以 UserInteractive 模式運行時顯示模式對話框或窗體是無效操作。請指定 ServiceNotification 或 DefaultDesktopOnly 樣式,以顯示服務應用程序發出的通知。
換一種調用方法 :在web中調用外部的控制台程序
將上面方法 放入控制台程序中
mian函數如下:
View Code
1 [STAThread]
2 static void Main(string[] args)
3 {
4 #region MyRegion
5 /*
6 if (args != null && args.Length > 0)
7 {
8 string PdfPath = args[0].ToString();
9 string ImgPath = args[1].ToString();
10 string imgDire = args[2].ToString();
11 string b = PDFToPic(PdfPath,ImgPath ,imgDire, ImageFormat.Jpeg);
12
13 if (string.IsNullOrEmpty(b))
14 {
15 b = "出錯";
16 }
17
18 Console.WriteLine(b);
19 Console.ReadKey();
20
21 }
22 else
23 {
24 Console.WriteLine("無參數");
25 Console.ReadKey();
26 }
27 */
28
29 #endregion
30
31
32 #region gs
33
34 if (args != null && args.Length > 0)
35 {
36 string PdfPath = args[0].ToString();
37 string HtmlDic = args[1].ToString();
38 string ImgPath = args[2].ToString();
39
40 string b = GetImage(PdfPath, HtmlDic, ImgPath, ImageFormat.Jpeg);
41
42 if (string.IsNullOrEmpty(b))
43 {
44 b = "出錯";
45 }
46 Console.WriteLine(b);
47 }
48 else
49 {
50 Console.WriteLine("無參數");
51 }
52 #endregion
53 // GetImage(@"C:\Users\Administrator.DVT\Desktop\fu.pdf", @"C:\Users\Administrator.DVT\Desktop\imgs", @"C:\Users\Administrator.DVT\Desktop\imgs",ImageFormat.Jpeg);
54
55 }
生成 拿到Debug 下 控制台.exe 和三個組件(
itextsharp.dll
PDFView.dll
gsdll32.dll
)
放入web項目中 (必須放在 同一文件夾下)
在web中調用方法的地方 改寫成 調用控制台程序
代碼如下:
View Code
1 try
2 {
3 Process pro = new Process();
4 pro.StartInfo.RedirectStandardOutput = true;
5 pro.StartInfo.RedirectStandardInput = false;
6 //不顯示窗口
7 pro.StartInfo.CreateNoWindow = true;
8 pro.StartInfo.UseShellExecute = false;
9 //要調用的控制台程序
10 pro.StartInfo.FileName = Request.MapPath("../FileUpload/ConsoleApplication1.exe");
11 //給控制台程序的參數傳遞值
12 pro.StartInfo.Arguments = filePath+" "+HtmlDic+" "+imgDire;
13 pro.Start();
14
15
16 string b= pro.StandardOutput.ReadToEnd();
17
18 pro.WaitForExit();
19 pro.Close();
20 pro.Dispose();
21 result = b;
22 }
23 catch (Exception)
24 {
25
26 throw;
27 }
vs中測試 通過
發布到iis中 的配置:
iis 中 應用程序池 設置中 啟用 32位應用程序
ok,終於解決了 (-_-! 調了兩周)
最近改功能上線 又發現些問題...並解決
1,EXCLE文件生成html 預覽
xlsx文件 生成html后 打開html 會有 警告 或提示
導致進程卡死(因為再程序中打開 ,無法響應)
2,docx文件生成html 預覽 問題同上 、
解決方法:只需設置 兩個屬性 即可
repExcel.DisplayAlerts = false;
word.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
另外 要做進程回收
excel事例:
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
repExcel = null;
GC.Collect();
//根據時間殺進程
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");//依據時間殺滅進程
foreach (System.Diagnostics.Process p in process)
{
if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
{
p.Kill();
}
}
Thread.Sleep(3000);//保證完全關閉
/// <summary>
/// 殺掉進程
/// </summary>
/// <param name="hwnd"></param>
/// <param name="id"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);
public void killexcel(Excel.Application xlapp)
{
try
{
IntPtr app = new IntPtr(xlapp.Hwnd);
int processid;
GetWindowThreadProcessId(app, out processid);
System.Diagnostics.Process.GetProcessById(processid).Kill();
}
catch
{ }
}
pdf 同樣要 回收資源 殺掉進程
....................................擦 ,貌似解決了。。。

View Code