根据创建时间对 Excel 工作表进行逆序排序/反转 Excel 工作表

发布于 2024-12-07 04:37:21 字数 188 浏览 0 评论 0原文

我正在使用 Microsoft interop excel 来自动化 Excel 工作簿 其中我有很多工作表(比如 40 个),这些工作表是在几秒甚至更短的时间内创建的 现在我必须以相反的顺序呈现工作表,即首先创建的工作表在打开时应该首先出现(当前它最后出现)简而言之,我必须以相反的顺序或按创建时

对此事进行排序 任何帮助

谢谢

I'm using Microsoft interop excel to automate the excel workboook
in which i have many worksheets(say 40) which have been created at in seconds gap or even less
now i have to present the worksheet in reverse order i.e the sheet which was created first should come first while opening(currently it comes last) in short I have to sort the excel sheet in reverse order or by time of creation

any help in this matter

thnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃不饱 2024-12-14 04:37:21
Workbook wb = app.Workbooks.Open(Form1.strRecentFilename, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp);
        int count = wb.Worksheets.Count;
        Worksheet ws, lastws;
        lastws = (Worksheet)wb.Worksheets[count];
        MessageBox.Show(lastws.Name);
        for (int i = count - 1; i >= 1; i--)
        {
            lastws = (Worksheet)wb.Worksheets[count];
            ws = (Worksheet)wb.Worksheets[i];
            ws.Move(System.Reflection.Missing.Value, lastws);
        }
Workbook wb = app.Workbooks.Open(Form1.strRecentFilename, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp, temp);
        int count = wb.Worksheets.Count;
        Worksheet ws, lastws;
        lastws = (Worksheet)wb.Worksheets[count];
        MessageBox.Show(lastws.Name);
        for (int i = count - 1; i >= 1; i--)
        {
            lastws = (Worksheet)wb.Worksheets[count];
            ws = (Worksheet)wb.Worksheets[i];
            ws.Move(System.Reflection.Missing.Value, lastws);
        }
独享拥抱 2024-12-14 04:37:21

据我所知,Excel不存储每个工作表的创建日期和时间。然而,每个新工作表都会添加到工作簿每个工作表的末尾。
因此,您可以根据这个假设颠倒顺序。

下面是一个用于执行此操作的 VBA 宏,您只需将其调整为互操作或 C#:

Sub reverseOrder()
Dim i As Integer
For i = Worksheets.Count - 1 To 1 Step -1
    Worksheets(i).Move After:=Worksheets(Worksheets.Count)
Next i
End Sub

它会解析从最后一张之前的一张到第一张的工作表,并将每个工作表移动到最后一个位置

As far as I know, Excel doesn't store the date and time of creation of each sheet. Yet, every new sheet is added at the end of every sheets of the workbook.
So, you can reverse the order based on this hypothesis.

Here is a VBA macro to do this, you just have to adapt it to interop or C#:

Sub reverseOrder()
Dim i As Integer
For i = Worksheets.Count - 1 To 1 Step -1
    Worksheets(i).Move After:=Worksheets(Worksheets.Count)
Next i
End Sub

It parses sheets from one sheet before the last one to the first one and move each sheet to the last position.

依 靠 2024-12-14 04:37:21

因此,如果您有 Excel 文件中的工作簿工作表,并且需要对工作表字母表进行排序:

   public void SortWs()
   {
        List<Worksheet> for_sort = new List<Worksheet>();

        foreach (Worksheet ws in wb.Worksheets)
        {
            for_sort.Add(ws);
        }

        for_sort.Sort(delegate(Worksheet wst1, Worksheet wst2)
        {
            return wst1.Name.CompareTo(wst2.Name);//sort by worksheet's name
        });

        Worksheet ws1, ws2;

        for (int i = 0; i < for_sort.Count; i++)
        {
            for (int j = 1; j <= wb.Worksheets.Count; j++)
            {
                ws1 = (Worksheet)wb.Worksheets[j];
                if (for_sort[i].Name == ws1.Name)
                {
                    ws2 = (Worksheet)wb.Worksheets[i+1];
                    ws1.Move(ws2, Type.Missing);
                }
            }
        }
    }

So, if you have a Worksheets of Workbook in excel file and you need to sort Worksheets alphabet:

   public void SortWs()
   {
        List<Worksheet> for_sort = new List<Worksheet>();

        foreach (Worksheet ws in wb.Worksheets)
        {
            for_sort.Add(ws);
        }

        for_sort.Sort(delegate(Worksheet wst1, Worksheet wst2)
        {
            return wst1.Name.CompareTo(wst2.Name);//sort by worksheet's name
        });

        Worksheet ws1, ws2;

        for (int i = 0; i < for_sort.Count; i++)
        {
            for (int j = 1; j <= wb.Worksheets.Count; j++)
            {
                ws1 = (Worksheet)wb.Worksheets[j];
                if (for_sort[i].Name == ws1.Name)
                {
                    ws2 = (Worksheet)wb.Worksheets[i+1];
                    ws1.Move(ws2, Type.Missing);
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文