C# web實現word 轉Html、office轉Html、pdf轉圖片 線上預覽檔案
改篇 pdf 預覽再本機沒問題,釋出再iis中 不行 ,(使用剪貼簿的問題..excel和word 可以,)
詳細配置及程式碼
word 轉Html
複製程式碼
1 /// <summary>
2 /// word轉成html
3 /// </summary>
4 /// <param name="wordFileName"></param>
5 private void WordToHtml(object wordFileName,string htmlWord)
6 {
7 //在此處放置使用者程式碼以初始化頁面
8 Word.ApplicationClass word = new Word.ApplicationClass();
9 Type wordType = word.GetType();
10 Word.Documents docs = word.Documents;
11 //開啟檔案
12 Type docsType = docs.GetType();
13 Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
14 //轉換格式,另存為
15 string a = doc.Comments.ToString();
16 Type docType = doc.GetType();
17 string wordSaveFileName = wordFileName.ToString();
21 string strSaveFileName = htmlWord + "\\" + Path.GetFileNameWithoutExtension(wordSaveFileName) + ".html";
23 object saveFileName = (object)strSaveFileName;
24 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
25 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
26 //退出 Word
27 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
28
29 }
複製程式碼
引入 Microsoft.Office.Interop.Word;
office轉Html
複製程式碼
/// <summary>
/// Excel轉成html
/// </summary>
public void ReadExcel(string rFilePath, string rHtmlFilePath)
{
Excel.Application repExcel = new Excel.Application();
Excel.Workbook workbook = null;
//xlsFile為Excel檔案路徑
workbook = repExcel.Application.Workbooks.Open(rFilePath,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value);
//htmlFile 是要另存的html檔名
object ofmt = Excel.XlFileFormat.xlHtml;
workbook.SaveAs(rHtmlFilePath,
ofmt,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Excel.XlSaveAsAccessMode.xlNoChange,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value,
Missing.Value);
object osave = false;
workbook.Close(osave, Missing.Value, Missing.Value);
}
複製程式碼
引入Microsoft.Office.Interop.Excel;
pdf 轉圖片
說明:本方法 用 acrobat 的官方介面
需要安裝 acrobat professional 8.0或更高版本
然後再vs中新增com元件 abode acrobat 版本號 type library 只有安裝了上面軟體 才回有
在web頁 頭部 新增 AspCompat="true" 控制檯程式 要在main函式上加 [STAThread] 保證單執行緒模型下 才能訪問 剪貼簿
using System.IO;
using System.Reflection;
using DataHelp;
using System.Text;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
複製程式碼
/// <summary>
/// Pdf導圖片
/// </summary>
/// <param name="pdfInputPathDirectory"></param>
/// <param name="pngOutputPath"></param>
/// <param name="format"></param>
/// <returns></returns>
public string PDFToPic(string pdfInputPathDirectory, string pngOutputPath, ImageFormat format)
{
StringBuilder b = new StringBuilder();
ImageFormat formate = format;
// Acrobat objects
Acrobat.CAcroPDDoc pdfDoc = null;
Acrobat.CAcroPDPage pdfPage = null;
Acrobat.CAcroRect pdfRect = null;
Acrobat.CAcroPoint pdfPoint = null;
try
{
string[] files = new string[] { pdfInputPathDirectory };
string dic = Path.GetDirectoryName(pdfInputPathDirectory);
//string[] files = Directory.GetFiles(dic, "*.pdf");
for (int n = 0; n < files.Length; n++)
{
string inputFile = files[n].ToString();
string fileName = files[n].Substring(files[n].LastIndexOf(@"\") + 1).Replace(".pdf", "");
// Will always be available as .NET framework ships with all
pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PDDoc", "");
bool ret = pdfDoc.Open(inputFile);
if (!ret)
{
throw new FileNotFoundException();
}
else
{
b = b.AppendLine("<span>pdfDoc成功開啟檔案!!!</span>");
}
// Get the number of pages (to be used later if you wanted to store that information)
int pageCount = pdfDoc.GetNumPages();
b = b.AppendLine("<ul style='azimuth:center; list-style-type:none;' >");
for (int i = 0; i < pageCount; i++)
{
// Get the first page
pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);
pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
b = b.AppendLine("<li> <span>pdfPoint.x=" + pdfPoint.x + ";pdfPoint.y=" + pdfPoint.y + "</span> </li>");
pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rect", "");
pdfRect.Left = 0;
pdfRect.right = pdfPoint.x;
pdfRect.Top = 0;
pdfRect.bottom = pdfPoint.y;
// Render to clipboard, scaled by 100 percent (ie. original size)
// Even though we want a smaller image, better for us to scale in .NET
// than Acrobat as it would greek out small text
// see
http://www.adobe.com/support/techdocs/1dd72.htm
//清空剪貼簿
//Clipboard.Clear();
//b = b.AppendLine("<li> <span>Clipboard.Clear();清空剪貼簿 </span> </li>");
pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);
//b = b.AppendLine("<li> <span>pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)=" +pdfPage.CopyToClipboard(pdfRect, 0, 0, 100)+ ";複製到剪貼簿</span> </li>");
System.Windows.Forms.IDataObject clipboardData = Clipboard.GetDataObject();
bool c = false;
if (clipboardData!=null )
{
c = true;
}
b = b.AppendLine("<li> <span> Clipboard.GetDataObject()=" + c + "剪貼簿中是否有資料;</span> </li>");
b = b.AppendLine("<li> <span>clipboardData.GetDataPresent(DataFormats.Bitmap)=" + clipboardData.GetDataPresent(DataFormats.Bitmap) + ";</span> </li>");
if (clipboardData.GetDataPresent(DataFormats.Bitmap))
{
Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
b = b.AppendLine("<li> <span>生成圖片的大小: pdfBitmap.Size.Height=" + pdfBitmap.Size.Height + "; pdfBitmap.Size.Width=" + pdfBitmap.Size.Width + "</span> </li>");
string dPath = Path.Combine(pngOutputPath, i.ToString("0000") + "." + formate.ToString());
b = b.AppendLine("<li> <span>生成圖片的路徑:" + dPath + ";</span> </li>");
pdfBitmap.Save(
dPath, formate);
pdfBitmap.Dispose();
}
b = b.AppendLine("<li> <img src='..\\" + imgDire + "\\" + i.ToString("0000") + "." + formate.ToString() + "' /> </li><span>第" + (i + 1) + "頁</span>");
}
pdfDoc.Close();
// Not sure how why it is to do this, but Acrobat is not the best behaved COM object
// see
http://blogs.msdn.com/yvesdolc/archive/2004/04/17/115379.aspx
Marshal.ReleaseComObject(pdfPage);
Marshal.ReleaseComObject(pdfRect);
Marshal.ReleaseComObject(pdfDoc);
}
b = b.AppendLine("</ul>");
}
catch (System.Exception ex)
{
Response.Write(ex.ToString());
}
return b.ToString();
}
複製程式碼
關於釋出:
offic 元件釋出問題
我的伺服器環境是 windows servers 2008 64位作業系統
1、 在伺服器上安裝Office的Excel相關軟體(推薦安裝Office 2007);
2、 Cmd中輸入命令 comexp.msc -32 啟動元件服務(啟動元件服務 方法 執行 mmc、mmc -32(32) 、DCOMCNFG.exe(64)) 不行一個一個
3、 依次雙擊"元件服務"->"計算機"->"我的電腦"->"DCOM配置";
4、 在"DCOM配置"中找到"Microsoft Excel Application",在它上面點選右鍵,然後點選"屬性",彈出"Microsoft Excel應用程式屬性"對話方塊;
5、 點選"標識"標籤,選擇 啟動使用者(預設選項) ;
6、 點選"安全"標籤,在以下 三個專案中都選中 “自定義” 新增 ”everyone” 並分配最大許可權(全選)
7、設定Web.config檔案 在Web.config檔案的<system.web>中加入<identity impersonate="true"/> (沒有這一步 會報Excel程序無法開啟相應的excel檔案)。
Ok了
Word 配置同上。