为什么这不会保存我的更改?还是不是复制?

时间:2012-03-30 20:54:09

标签: c# com excel-2010 excel-interop

因此,我要做的就是将每个单元格从一个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;
坦率地说,我甚至不确定它是否正在打开文件。工作簿标识值为null。我正在为两个工作簿传递一个有效的路径。我在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);
}

1 个答案:

答案 0 :(得分:0)

我的问题是我运行了两个excel.exe实例,每个实例都加载了一个工作簿。在同一个Application实例中加载两个工作簿解决了我的问题。