使用C#在excel中对行进行分组 [英] Grouping rows in excel using C#

查看:212
本文介绍了使用C#在excel中对行进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



也许有人讨论但我找不到合适的问题...我有以下代码



Hello guys,

Maybe it was discussed but i don't find something to fit my problem ... I have the following code

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

if (xlApp == null)
{
    Console.WriteLine("Excel is not properly installed!!");
    return;
}

Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
Excel.Range xlRange;

xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Name = "Raport generated";
xlWorkSheet.Cells[1, 1] = "ceva";
xlWorkSheet.Cells[2, 1] = "ceva";
xlWorkSheet.Cells[3, 1] = "ceva";
xlWorkSheet.Cells[4, 1] = "ceva";
xlWorkSheet.Cells[5, 1] = "ceva";
xlWorkSheet.Cells[6, 1] = "ceva";
xlWorkSheet.Cells[7, 1] = "ceva";
xlWorkSheet.Cells[8, 1] = "ceva";



xlWorkBook.SaveAs("D:\\Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();


Console.ReadKey();





我想将行分组为2 - > 8(在我的excel中有+符号,当我按下它以展开时),我该怎么办?



提前谢谢你。



我尝试过:





And i want to group the rows from 2 -> 8(having the "+" sign in my excel and when i press it to expand), how can i do that ?

Thank you in advance.

What I have tried:

xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 1], xlWorkSheet.Cells[8, 1]).Group();

推荐答案

Excel.Range xlRange = xlWorkSheet.get_Range(xlWorkSheet.Cells[2, 1], xlWorkSheet.Cells[8, 1]);
            xlRange.rows.Group(misValue, misValue, misValue, misValue);
            xlRange.rows.OutlineLevel = 1;





试试这个。我已经在我的程序中使用了



try this. This i had already used in my program


string _tempPath = _tempRowNb.ToString() + _row.ToString();

Excel.Range myRange = xlWorkSheet.Rows[_tempPath, misValue] as Excel.Range;
myRange.OutlineLevel = 1;
myRange.Group(misValue, misValue, misValue, misValue);







这是我的解决方案希望有人会使用它。




This is my solution. Hope someone will use it.


这篇关于使用C#在excel中对行进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆