无法使用C#对excel工作表进行排序

时间:2015-06-17 07:05:13

标签: c# excel sorting worksheet

我想使用C#以编程方式对excel工作表进行排序,但我使用的代码不起作用:

{\n      \"1 & 1 Internet\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 55,\n        \"abc\": \"low\"\n      },\n      \"1 website hosting\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"10000ft\": {\n        \"category\": \"Collaboration\",\n        \"xyz\": 48,\n        \"abc\": \"poor\"\n      },\n      \"1010data\": {\n        \"category\": \"Big Data\",\n        \"xyz\": 56,\n        \"abc\": \"low\"\n      },\n      \"101domains\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"111WebHost.com\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 6,\n        \"abc\": \"poor\"\n      },\n      \"11Giraffes\": {\n        \"category\": \"Web Design\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"123 FlashChat\": {\n        \"category\": \"Collaboration\",\n        \"xyz\": 64,\n        \"abc\": \"medium\"\n      },\n      \"123-Hosts\": {\n        \"category\": \"Web Design & Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"123-reg\": {\n        \"category\": \"Web Design & Infrastructure\",\n        \"xyz\": 52,\n        \"abc\": \"low\"\n      },\n      \"123ContactForm\": {\n        \"category\": \"Survey Solutions\",\n        \"xyz\": 43,\n        \"abc\": \"poor\"\n      },\n      \"123LandLord\": {\n        \"category\": \"Asset Management\",\n        \"xyz\": 18,\n        \"abc\": \"poor\"\n      },\n      \"123connect\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 8,\n        \"abc\": \"poor\"\n      },\n      \"15Five\": {\n        \"category\": \"Telecom\",\n        \"xyz\": 49,\n        \"abc\": \"poor\"\n      },\n      \"15MinuteServers\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 13,\n        \"abc\": \"poor\"\n      },\n      \"1ShoppingCart\": {\n        \"category\": \"Software Development\",\n        \"xyz\": 27,\n        \"abc\": \"poor\"\n      },\n      \"1Web Hosting\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 3,\n        \"abc\": \"poor\"\n      },\n      \"1st Domains\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 19,\n        \"abc\": \"poor\"\n      },\n      \"1st Easy\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 51,\n        \"abc\": \"low\"\n      },\n      \"1st4webshops\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 12,\n        \"abc\": \"poor\"\n      },\n      \"1stDNS Limited\": {\n        \"category\": \"Infrastructure\",\n        \"xyz\": 0,\n        \"abc\": \"poor\"\n      },\n      \"1time\": {\n        \"category\": \"Expense Management\",\n        \"xyz\": 12,\n        \"abc\": \"poor\"\n      },\n      \"2001 Web\": {\n        \"category\": \"Web Design & Infrastructure\",\n        \"xyz\": 24,\n        \"abc\": \"poor\"\n      },\n      \"23Video\": {\n        \"category\": \"Content Management\",\n        \"xyz\": 31,\n        \"abc\": \"poor\"\n      },\n      \"24 Seven Office\": {\n        \"category\": \"Enterprise Resource Planning\",\n        \"xyz\": 14,\n        \"abc\": \"poor\"\n      }

我使用消息框对其进行调试,以打印“J”列中的值,结果未排序:

        //the largest size of sheet in Excel 2010
        int maxRowAmount = 1048576;
        int maxColAmount = 16384;

        //Sort by the value in column G1
        sourceWorkSheet.Sort.SortFields.Add(sourceWorkSheet.Range["J:J"], XlSortOn.xlSortOnValues, XlSortOrder.xlAscending, XlSortDataOption.xlSortNormal);

        //Find out the last used row and column, then set the range to sort,
        //the range is from cell[2,1](top left) to the bottom right corner
        int lastUsedRow=sourceWorkSheet.Cells[maxRowAmount, 1].End[XlDirection.xlUp].Row;
        int lastUsedColumn=sourceWorkSheet.Cells[2, maxColAmount].End[XlDirection.xlToLeft].Column;
        Range r = sourceWorkSheet.Range[sourceWorkSheet.Cells[2, 1], sourceWorkSheet.Cells[lastUsedRow,lastUsedColumn ]];
        sourceWorkSheet.Sort.SetRange(r);

        //Sort!
        sourceWorkSheet.Sort.Apply();

它不起作用的原因是什么?

由于

2 个答案:

答案 0 :(得分:0)

解决方案是放一个

sourceWorkSheet.Sort.SortFields.Clear(); 

sourceWorkSheet.Sort.SortFields.Add(sourceWorkSheet.Range["J:J"], XlSortOn.xlSortOnValues, XlSortOrder.xlAscending, XlSortDataOption.xlSortNormal);

答案 1 :(得分:0)

在您的代码中打开excel然后从中读取,以便按原始顺序读取工作表(不按字母顺序排序)。 您可以使用下一个代码来获取已排序的工作表。

        OleDbConnection connection = new OleDbConnection(string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=\"Excel 8.0;HDR=No;\"", filePath));
        OleDbCommand command = new OleDbCommand();
        DataTable tableOfData = null;
        command.Connection = connection;
        try
        {
            connection.Open();
            tableOfData = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string tablename = tableOfData.Rows[0]["TABLE_NAME"].ToString();
            tableOfData = new DataTable();
            command.CommandText = "Select * FROM [" + tablename + "]";
            tableOfData.Load(command.ExecuteReader());
        }
        catch (Exception ex)
        {
        }