首页 > 解决方案 > 使用 C# 打开/保存 excel 文件时的问题

问题描述

我正在尝试使用 C# 访问 excel 模块,但在保存 excel 文件后遇到了问题。

有重复的 Thisworkbook 和 Sheets 如下图所示。

仅当 excel 文件具有模块时才会发生这种情况,而如果 excel 项目中只有工作表,一切都会好起来的。

这是代码,它只是打开和保存同一个文件。

我正在使用 WIN10/VS2013/EXCEL 2010。

有谁知道为什么会发生这种情况?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Vbe.Interop;
namespace ConsoleApplication3
{
  class Program
  {
    static void Main(string[] args)
    {
        string filepath = @"C:\test.xls";
        Excel._Application app = new Excel.Application();
        Excel._Workbook workbook = app.Workbooks.Open(filepath);


        workbook.SaveAs(filepath);

        workbook.Close(true);


        app.Quit();

    }
}

}

保存excel文件后的项目列表快照

标签: c#excel

解决方案


推荐阅读