2012-03-30 150 views
0

因此,我所要做的就是將每個單元格從一個Excel文檔複製到另一個文檔,目的是防止用戶將包含宏的文件上載到我的服務器上。當我運行此通過與測試的形式,它問我在哪裏保存「清潔」的文件,但它看起來好像沒有變化向它提出,除了時間戳...爲什麼不能保存我的更改?還是不復制?

object objMissing = Missing.Value; 
Excel.Workbook xlDirtyWorkbook = xlDirtyApp.Workbooks.Open(strDirtyDocPath, 
              objMissing, objMissing, objMissing, 
              objMissing, objMissing, objTrue, 
              objMissing, objMissing, objMissing, 
              objMissing, objMissing, objMissing, 
              objMissing, objMissing); 
Excel.Worksheet xlDirtyWorksheet = 
     (Excel.Worksheet)xlDirtyWorkbook.Worksheets[1]; 

Excel.Workbook xlCleanWorkbook = xlCleanApp.Workbooks.Open(strCleanDocPath, 
              objMissing, objMissing, objMissing, 
              objMissing, objMissing, objTrue, 
              objMissing, objMissing, objMissing, 
              objMissing, objMissing, objMissing, 
              objMissing, objMissing); 
Excel.Worksheet xlCleanWorksheet = 
     (Excel.Worksheet)xlCleanWorkbook.Worksheets[1]; 

// -- Copy User Modifiable Range -- 
Excel.Range rnThisDirtyCell = xlDirtyWorksheet.get_Range("A2:L310", objMissing); 
Excel.Range rnThisCleanCell = xlCleanWorksheet.get_Range("A2:L310", objMissing); 

rnThisDirtyCell.Copy(rnThisCleanCell); 

// -- Copy Document Meta-Data -- 
object dpDirtyProps; 
object dpCleanProps; 

dpDirtyProps = xlDirtyWorkbook.BuiltinDocumentProperties; 
dpCleanProps = xlCleanWorkbook.BuiltinDocumentProperties; 

dpCleanProps = dpDirtyProps; 

坦率地說,我如果它打開文件,它甚至不確定。工作簿標識值爲空。我爲這兩個工作簿傳遞了有效的路徑。我在Application.Worksheets.Open方法中使用包含文件名的完整路徑。這是不正確的?

我錯過了什麼?它不會拋出任何例外......

====================================== ============================================

爲了後代,這就是我解決它的方法:

Excel.Workbook xlDirtyWorkbook = xlApp.Workbooks.Open(strDirtyDocPath, 
           objMissing, objMissing, objMissing, 
           objMissing, objMissing, true, 
           objMissing, objMissing, objMissing, 
           objMissing, objMissing, objMissing, 
           objMissing, objMissing); 

Excel.Workbook xlCleanWorkbook = xlApp.Workbooks.Open(strCleanTemplatePath, 
           objMissing, objMissing, objMissing, 
           objMissing, objMissing, true, 
           objMissing, objMissing, objMissing, 
           objMissing, objMissing, objMissing, 
           objMissing, objMissing); 

for (int iIndex = 1; iIndex <= xlDirtyWorkbook.Worksheets.Count; iIndex++) 
{ 
    rnThisDirtyCell = xlDirtyWorkbook.Worksheets[iIndex].UsedRange; 
    rnThisCleanCell = xlCleanWorkbook.Worksheets[iIndex].Range(
     rnThisDirtyCell.Address); 
    rnThisDirtyCell.Copy(rnThisCleanCell); 
} 
+0

爲什麼不把它保存爲'.XLSX'(宏免費工作簿)? – 2012-03-30 22:23:10

+0

那麼,窗體上有需要在那裏的宏,我只是想確保上傳時沒有摻假宏。所以我試圖將上傳文檔的單元格複製到原始分佈式模板文檔的副本中。我也試圖從上傳的文檔中保留單元格的任何格式/樣式更改,並將其保留在複製的版本中(我知道背景中的宏是合法的。 – 2012-03-31 03:08:02

回答

0

我的問題是我運行了兩個excel.exe實例,每個實例都加載一個工作簿。在同一個應用程序實例中加載兩個工作簿解決了我的問題。

相關問題