Excel interop workbook.SaveAs()删除原始格式?

时间:2013-07-16 18:59:42

标签: c# excel interop

使用Microsoft.Office.Interop.Excel.Workbook.SaveAs(...)?

时如何保留工作表的格式

例如,我使用Excel 2010打开以前创建的工作簿,我发现它看起来很漂亮:列标题中的粗体字体,漂亮的网格线,显示输入单元格的高亮颜色等等。

我切换到VS2012并使用我在下面写的ExcelAppManager开始打开具有漂亮格式的工作簿。然后,我使用Interop库以编程方式将新单元格值写入其中一个工作表。然后,我使用SaveAs()保存工作表,如下面ExcelAppManager中所示。然后我使用Microsoft Excel 2010打开工作表:我可以看到我在各自单元格中写的值 - 这很棒,它有效 - 但整个工作簿不再具有格式。这是简单的香草格式,所有美丽的格式都消失了。

我将格式定义为画家可以操作的格式:粗体,字体,对齐方式,网格线,缩进,宽度和高度等。

示例代码:

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

namespace ExcelStuff
{
    public class ExcelAppManager
    {
        private Application _excelApp;
        private bool _isDefaultWorksheets = true;

        private Workbook _workBook;
        private Workbooks _workBooks;
        private Sheets _workSheets;

        public ExcelAppManager(string pathToExistingWorksheet)
        {
            _excelApp = new Application {DisplayAlerts = false};

            _workBooks = _excelApp.Workbooks;
            _workBook = _workBooks.Open(pathToExistingWorksheet, Type.Missing, false, Type.Missing, Type.Missing,
                                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            _workSheets = _workBook.Worksheets;

            // NOTE: following lines are nice for debug of existing worksheets (to find the worksheets names)
            //Get the reference of second worksheet
            //            var worksheet = (Microsoft.Office.Interop.Excel.ExcelWorksheet) _workSheets.Item[1];
            //            string strWorksheetName = worksheet.Name; //Get the name of worksheet.


            _isDefaultWorksheets = true;
        }

        public ExcelAppManager()
        {
        }

        public void Initialize()
        {
            _excelApp = new Application {DisplayAlerts = false};
            _workBooks = _excelApp.Workbooks;
            _workBook = _workBooks.Add(Missing.Value);
            _workSheets = _workBook.Worksheets;
            _isDefaultWorksheets = true;
        }

        public void KillProcess()
        {
            _workBook.Close();
            _workBooks.Close();
            _excelApp.Quit();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Marshal.FinalReleaseComObject(_workSheets);
            Marshal.FinalReleaseComObject(_workBook);
            Marshal.FinalReleaseComObject(_workBooks);
            Marshal.FinalReleaseComObject(_excelApp);
        }


        public void SaveAs(string filepath, string fileExtensionOfExcelFile)
        {
            _excelApp.DisplayAlerts = false;

            if (fileExtensionOfExcelFile == "xlsm")
            {
                _workBook.SaveAs(filepath, XlFileFormat.xlOpenXMLWorkbookMacroEnabled,
                                 Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange,
                                 XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
            }
            else
            {
                _workBook.SaveAs(filepath, Type.Missing,
                                 Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange,
                                 XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
            }
        }


        internal Sheets GetSheets()
        {
            return _workSheets;
        }

    }

在我的客户端代码中,我使用ExcelAppManager:

var _manager = new ExcelAppManager(_excelFilepath);

1 个答案:

答案 0 :(得分:2)

在VS2012中使用Interop库时,默认使用Office 2013,并以该格式保存工作簿。稍后您将使用Office 2010打开它。这可能是原始格式丢失的原因。尝试以2010格式保存excel工作簿,这可能会解决问题。