如何阅读包含多个工作表的Excel工作表,并仅编辑一个工作表

时间:2022-02-20 20:53:43

I have a program which picks up values from an XML sheet and writes them into an excel sheet, using TestWriter.

我有一个程序从XML表中获取值,并使用TestWriter将它们写入excel表。

FileStream ostrm;
                StreamWriter strm;
                TextWriter txtWrt = Console.Out;
                ostrm = new FileStream(path + "\\Output.xls", FileMode.Truncate, FileAccess.Write);

Now the output file, Output.xls has multiple tabs, but I only have to truncate and write into the first sheet, leaving the other tabs untouched. I was using the below code, but it deleted all other tabs and edited the first tab.

现在输出文件Output.xls有多个选项卡,但我只需要截断并写入第一个工作表,而保持其他选项卡不变。我使用下面的代码,但它删除了所有其他选项卡并编辑了第一个选项卡。

public Excel.Application app = null;
    public Excel.Workbook workBook = null;
    public Excel.Worksheet workSheet = null;
    app = new Excel.Application();
            workBook = app.Workbooks.Add();
            workSheet = (Worksheet)workBook.Worksheets.get_Item(1);
            workBook.SaveAs(path+ "\\Output.xls");
            workBook.Close();
            app.Quit();

I need to find a way by which this can be avoided.

我需要找到一种可以避免这种情况的方法。

Thanks in anticipation!

在期待中感谢!

2 个解决方案

#1


0  

MSDN states that FileMode.Truncate "Specifies that the operating system should open an existing file. When the file is opened, it should be truncated so that its size is zero bytes." You can try changing to FileMode.OpenOrCreate instead, and then clear the tab before writing to it.

MSDN声明FileMode.Truncate“指定操作系统应该打开现有文件。当文件打开时,应该截断它,使其大小为零字节。”您可以尝试更改为FileMode.OpenOrCreate,然后在写入之前清除选项卡。

#2


0  

You cannot use the truncate command in the way you describe. Its intended for text files. For starters why are you usng FileStream?

您不能以您描述的方式使用truncate命令。它用于文本文件。对于初学者,你为什么要使用FileStream?

You should be using the XLSX open xml format Excel file type, and using the Open XML SDK. here is one article that explains how http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK

您应该使用XLSX开放xml格式Excel文件类型,并使用Open XML SDK。这篇文章解释了http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK

Whilst XLS format can accept xml data it is not designed to interoperate as efficiently as a XLSX format file. While you can get away with using filestream for small operations it is limited and you have already run into those limitations.

虽然XLS格式可以接受xml数据,但它并不像XLSX格式文件那样有效地进行互操作。虽然您可以将文件流用于小型操作,但它已经受到限制,您已经遇到了这些限制。

#1


0  

MSDN states that FileMode.Truncate "Specifies that the operating system should open an existing file. When the file is opened, it should be truncated so that its size is zero bytes." You can try changing to FileMode.OpenOrCreate instead, and then clear the tab before writing to it.

MSDN声明FileMode.Truncate“指定操作系统应该打开现有文件。当文件打开时,应该截断它,使其大小为零字节。”您可以尝试更改为FileMode.OpenOrCreate,然后在写入之前清除选项卡。

#2


0  

You cannot use the truncate command in the way you describe. Its intended for text files. For starters why are you usng FileStream?

您不能以您描述的方式使用truncate命令。它用于文本文件。对于初学者,你为什么要使用FileStream?

You should be using the XLSX open xml format Excel file type, and using the Open XML SDK. here is one article that explains how http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK

您应该使用XLSX开放xml格式Excel文件类型,并使用Open XML SDK。这篇文章解释了http://www.codeproject.com/Articles/670141/Read-and-Write-Microsoft-Excel-with-Open-XML-SDK

Whilst XLS format can accept xml data it is not designed to interoperate as efficiently as a XLSX format file. While you can get away with using filestream for small operations it is limited and you have already run into those limitations.

虽然XLS格式可以接受xml数据,但它并不像XLSX格式文件那样有效地进行互操作。虽然您可以将文件流用于小型操作,但它已经受到限制,您已经遇到了这些限制。