0

使用时如何保留工作表的格式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);
4

1 回答 1

2

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

于 2013-07-16T21:05:57.593 回答