0

我正在尝试使用 c# 代码执行以下操作:

  1. 在excel中隐藏一些行。
  2. 清除 Excel 工作表中的所有数据和格式。
  3. 将其他数据放到excel表中。

我希望隐藏的行仍然隐藏。是否可以?

谢谢你!

4

3 回答 3

3

我使用ClosedXML操作 excel 电子表格取得了很好的效果。

虽然我没有尝试过你的案例,但我做过类似的事情。在我的例子中,我将我的私人数据放入一个新的工作表中并隐藏它,这让 ClodedXML 变得简单。

于 2012-05-10T07:37:41.473 回答
1

这是一个示例代码,可以让您继续前进....

        //Create an Excel App
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        Microsoft.Office.Interop.Excel._Workbook xlWorkBook = null;
        Microsoft.Office.Interop.Excel._Worksheet xlWorksheet;

        //Open a Workbook
        xlWorkBook = xlApp.Workbooks.Open(@"d:\test.xlsx");
        xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Sheets[1];

        //My Workbook contains 10 rows with some data and formatting
        //I Hide rows 3, 4 & 5
        Microsoft.Office.Interop.Excel.Range hiddenRange = xlWorksheet.get_Range("A3:C5");
        hiddenRange.EntireRow.Hidden = true;

        //Get the entire sheet and Clear everything on it including data & formatting
        Microsoft.Office.Interop.Excel.Range allRange = xlWorksheet.UsedRange;
        allRange.Clear();


        //Now Add some new data, say a Title on the first cell, and some more data in a loop later
        xlWorksheet.Cells[1, 1] = "Title";

        for (int i = 6; i < 10; i++)
        {
            xlWorksheet.Cells[i, 1] = i.ToString();
        }

        xlApp.Visible = true;

就是这样....

于 2012-05-10T09:16:57.100 回答
0

将它们存储在变量中并在用数据填充 excel 后再次隐藏它们。

于 2012-05-10T07:38:18.510 回答