在Excel c#中刷新数据后记住隐藏的行

时间:2012-05-10 07:19:27

标签: c# excel vsto

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

  1. 隐藏excel中的某些行。
  2. 清除Excel工作表中的所有数据和格式。
  3. 将其他数据放入Excel工作表。
  4. 我希望隐藏的行仍然隐藏。 有可能吗?

    谢谢!

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分: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;

多数民众赞成......

答案 2 :(得分:0)

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