Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue); //Create new Excel Excel.Worksheet xlWorkSheet= (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //取第一個Sheet //指定列高, Object的高度約55像素, 這裡用第3列為範例 ((Excel.Range)xlWorkSheet.Rows["3:3", System.Type.Missing]).RowHeight = 55; //取得位置, 這裡用H3為範例 Excel.Range ColH = xlWorkSheet.get_Range("H3"); //Add OLE Object xlWorkSheet.Shapes.AddOLEObject(ClassType: Type.Missing , Filename: Directory.GetCurrentDirectory() + "\\PDF檔案名稱.pdf" , Link: false , DisplayAsIcon: true , IconFileName: Directory.GetCurrentDirectory() + "\\PDF的Icon檔案名稱.ico" , IconIndex: "0" , IconLabel: "這裡可以寫字" , Left: ColH.Left //放 OLE Object 的位置 , Top: ColH.Top //放 OLE Object 的位置 //, Width: 15 //有設定Icon時, Width/Height的設定貌似無效 //, Height: 15 //有設定Icon時, Width/Height的設定貌似無效 ); //xlWorkSheet.Cells.EntireRow.AutoFit(); //自動列高, 因為有指定列高, 這邊不設定自動 xlWorkSheet.Cells.EntireColumn.AutoFit(); //自動欄寬 //另存新檔 xlWorkBook.SaveAs(@"存檔路徑 \\172.16.0.11\Report\", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); //關閉 xlWorkBook.Close(true, misValue, misValue); xlApp.DisplayAlerts = false; xlApp.Quit(); //釋放 releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; Console.WriteLine("Unable to release the Object " + ex.ToString()); } finally { GC.Collect(); } }
文章標籤
全站熱搜
留言列表