如何用C#对工作表中的数据进行排序?

如何用C#对工作表中的数据进行排序?,c#,excel,C#,Excel,我是新来的C,我现在提出一个问题。我需要你的帮助 如何使用C#对工作表中的数据进行排序?!我想根据单词的第一个字母对工作表中的行进行排序。函数类似于sql中的Orderby 有人会帮我吗??提前感谢。我只是假设您正在Excel工作表上使用Excel Interop(请记住澄清您的问题,以便人们将来能更好地帮助您(:) 你看过这个吗 链接中的示例,如果您使用的是范围对象: Excel.Range Fruits = Application.get_Range("A1", "B3"); Fruits.

我是新来的
C
,我现在提出一个问题。我需要你的帮助

如何使用
C#
对工作表中的数据进行排序?!我想根据单词的第一个字母对工作表中的行进行排序。函数类似于sql中的Orderby


有人会帮我吗??提前感谢。

我只是假设您正在Excel工作表上使用Excel Interop(请记住澄清您的问题,以便人们将来能更好地帮助您(:)

你看过这个吗

链接中的示例,如果您使用的是范围对象:

Excel.Range Fruits = Application.get_Range("A1", "B3");
Fruits.Sort(
Fruits.Columns[1, missing], Excel.XlSortOrder.xlAscending,
Fruits.Columns[2, missing], missing, Excel.XlSortOrder.xlAscending,
missing, Excel.XlSortOrder.xlAscending,
Excel.XlYesNoGuess.xlNo, missing, missing,
Excel.XlSortOrientation.xlSortColumns,
Excel.XlSortMethod.xlPinYin,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal,
Excel.XlSortDataOption.xlSortNormal); 

希望这是您想要的。如果不是,在上面的链接上还有“范围”以外的其他类型的示例。

谢谢大家的帮助。我已通过使用第三个组件(Spire.XLS)创建工作簿解决了此问题

有很多方法。我粘贴一个来共享:

    using System.Data;
    using System.Drawing;
    using System.Data.SqlClient;
    using System.Collections;
    using System;
    using System.Linq;

    using Spire.Xls;
    using System.Collections.Generic;

    namespace _4SeriersChart
     x, 2].Text = countryInfo[i].Capital;
                   destSheet.Range[rowIndex, 3].Text = countryInfo[i].Continent;
                   destSheet.Range[rowIndex, 4].NumberValue = countryInfo[i].Area;
                   destSheet.Range[rowIndex, 5].NumberValue = countryInfo[i].Population;
                   destSheet.Pictures.Add(rowIndex, 6, countryInfo[i].Flag);
                   rowIndex++;
                }

                            //set style
                destSheet = SetStyle(destBook, destSheet);

                //Add Column Chart
                destSheet = AddColumnChart(destSheet);

                //Add Stacked Line Chart(Country and population
                destSheet = AddStackLineChart(destSheet);

                //Add Stacked Column chart(Country and area)
                destSheet = AddStackColumnChart(destSheet);

                destSheet.AllocatedRange.AutoFitColumns();
                destBook.SaveToFile(@"..\..\CountryInfo.xls", ExcelVersion.Version97to2003);
                System.Diagnostics.Process.Start(@"..\..\CountryInfo.xls");
            }

            private static Worksheet SetStyle(Workbook workbook, Worksheet sheet)
            {
                //Sets body style
                CellStyle oddStyle = workbook.Styles.Add("oddStyle");
                oddStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
                oddStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
                oddStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
                oddStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
                oddStyle.KnownColor = ExcelColors.SkyBlue;

                CellStyle evenStyle = workbook.Styles.Add("evenStyle");
                evenStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
                evenStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
                evenStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
                evenStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
                evenStyle.KnownColor = ExcelColors.LightBlue;

                foreach (CellRange range in sheet.AllocatedRange.Rows)
                {
                    if (range.Row % 2 == 0)
                        range.CellStyleName = evenStyle.Name;
                    else
                        range.CellStyleName = oddStyle.Name;
                }

                //Sets header style
                CellStyle styleHeader = sheet.Rows[0].Style;
                styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
                styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
                styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
                styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
                styleHeader.VerticalAlignment = VerticalAlignType.Center;
                styleHeader.KnownColor = ExcelColors.Color24;
                styleHeader.Font.KnownColor = ExcelColors.White;
                styleHeader.Font.IsBold = true;

                return sheet;
            }


            //Add columnchart

            private static Worksheet AddColumnChart(Worksheet sheet)
            {
                Chart chart = sheet.Charts.Add();

                //Set region of chart data
                chart.DataRange = sheet.Range["D1:E19"];
                chart.SeriesDataFromRange = false;

                //Set position of chart
                chart.LeftColumn = 8;
                chart.TopRow = 1;
                chart.RightColumn = 17;
                chart.BottomRow = 29;

                chart.ChartType = ExcelChartType.ColumnClustered;

                //Chart title
                chart.ChartTitle = "Country Information";
                chart.ChartTitleArea.Font.Color = Color.Red;
                chart.ChartTitleArea.IsBold = true;
                chart.ChartTitleArea.Size = 12;

                chart.PrimaryCategoryAxis.Title = "Country";
                chart.PrimaryCategoryAxis.TitleArea.Font.Color = Color.Red;
                chart.PrimaryCategoryAxis.Font.IsBold = true;
                chart.PrimaryCategoryAxis.TitleArea.IsBold = true;


                //chart.PrimaryValueAxis.Title = "Sales(in Dollars)";
                chart.PrimaryValueAxis.HasMajorGridLines = false;
                chart.PrimaryValueAxis.MinValue = 0;
                chart.PrimaryValueAxis.NumberFormatIndex = 50000000;
                chart.PrimaryValueAxis.TitleArea.IsBold = true;
                chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;

                //set the series lable 
                chart.Series[0].CategoryLabels = sheet.Range["A2:A19"];
                //chart.Series[0].CategoryLabels.

                foreach (Spire.Xls.Charts.ChartSerie cs in chart.Series)
                {
                    cs.Format.Options.IsVaryColor = true;
                    cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = false;
                }

                chart.Legend.Position = LegendPositionType.Top;
                return sheet;
            }


            //Add Stack Line chart
            private static Worksheet AddStackLineChart(Worksheet sheet)
            {
                Chart chart = sheet.Charts.Add();

                chart.DataRange = sheet.Range["E1:E19"];
                chart.SeriesDataFromRange = false;

                //Set position of chart
                chart.LeftColumn = 1;
                chart.TopRow = 31;
                chart.RightColumn = 8;
                chart.BottomRow = 49;
                chart.ChartType = ExcelChartType.LineMarkersStacked;

                //Chart Title
                chart.ChartTitle = "Country Population Information";
                //chart.ChartTitleArea.IsBold = true;
                chart.ChartTitleArea.Size = 12;

                chart.PrimaryCategoryAxis.Title = "Country";
                //chart.PrimaryCategoryAxis.Font.IsBold = true;
                chart.PrimaryCategoryAxis.TitleArea.IsBold = true;

                chart.PrimaryValueAxis.HasMajorGridLines = false;
                chart.PrimaryValueAxis.MinValue = 0;
                chart.PrimaryValueAxis.NumberFormatIndex = 50000000;
                chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;

                //set the series lable 
                chart.Series[0].CategoryLabels = sheet.Range["A2:A19"];

                chart.Legend.Position = LegendPositionType.Top;
                return sheet;
            }

            //Add Stack Column chart
            private static Worksheet AddStackColumnChart(Worksheet sheet)
            {

                Chart chart = sheet.Charts.Add();
                chart.DataRange = sheet.Range["D1:D19"];
                chart.SeriesDataFromRange = false;

                chart.ChartType = ExcelChartType.ColumnStacked;


                //Set position of chart
                chart.LeftColumn = 9;
                chart.TopRow = 31;
                chart.RightColumn = 19;
                chart.BottomRow = 49;

                //Chart Title
                chart.ChartTitle = "Country Area Information";
                chart.ChartTitleArea.Size = 12;

                chart.PrimaryCategoryAxis.Title = "Country";
                chart.PrimaryCategoryAxis.TitleArea.IsBold = true;

                chart.PrimaryValueAxis.HasMajorGridLines = false;
                chart.PrimaryValueAxis.MinValue = 0;
                chart.PrimaryValueAxis.NumberFormatIndex = 2000000;
                chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;

                //set the series lable 
                chart.Series[0].CategoryLabels = sheet.Range["A2:A19"];

                chart.Legend.Position = LegendPositionType.Top;
                return sheet;
            }

        }
    }
我定义用于存储数据的类:

     using System.Data;
    using System.Drawing;
    using System.Data.SqlClient;
    using System.Collections;
    using System;
    using System.Linq;

    using Spire.Xls;
    using System.Collections.Generic;

    namespace _4SeriersChart
    {
        class Program
        {
            static void Main(string[] args)
            {
                Workbook sourceBook = new Workbook();
                sourceBook.LoadFromFile(@"..\..\Exercise.xls", ExcelVersion.Version97to2003);
                Worksheet sourceSheet = sourceBook.Worksheets[0];

                DataTable SourceTable = sourceSheet.ExportDataTable();

                for (int i = 0; i < sourceSheet.Pictures.Count; i++)
                {
                    ExcelPicture pic = sourceSheet.Pictures[i];
                }

                List<Country> countryInfo = new List<Country>();

                for (int i = 1, k = 0; i < sourceSheet.Rows.Length && k < sourceSheet.Pictures.Count; i++, k++)
                {
                    int j = 0;
                    countryInfo.Add(
                         new Country
                         {
                             Name = sourceSheet.Rows[i].Columns[j].Text,
                             Capital = sourceSheet.Rows[i].Columns[j + 1].Text,
                             Continent = sourceSheet.Rows[i].Columns[j + 2].Text,
                             Area = Convert.ToSingle(sourceSheet.Rows[i].Columns[j + 3].NumberValue),
                             Population = Convert.ToSingle(sourceSheet.Rows[i].Columns[j + 4].NumberValue),
                             Flag = sourceSheet.Pictures[k].Picture
                         }
                     );
                }

                //Sort the List
                countryInfo.Sort(Country.compare);

                Workbook destBook = new Workbook();
                //Initailize worksheet
                destBook.CreateEmptySheets(1);
                Worksheet destSheet = destBook.Worksheets[0];
                destSheet.Name = "Country Information";

                //inset the first row

                for (int i = 1; i <= sourceSheet.Columns.Length;i++ )
                {
                    destSheet.Range[1, i].Text = sourceSheet.Range[1, i].Text;
                }

                //insert data
                int rowIndex = 2;
                for (int i = 0; i < countryInfo.Count;i++ )
                {
                    int j=0;
                   destSheet.Range[rowIndex, 1].Text = countryInfo[i].Name;
                   destSheet.Range[rowIndex, 2].Text = countryInfo[i].Capital;
                   destSheet.Range[rowIndex, 3].Text = countryInfo[i].Continent;
                   destSheet.Range[rowIndex, 4].NumberValue = countryInfo[i].Area;
                   destSheet.Range[rowIndex, 5].NumberValue = countryInfo[i].Population;
                   destSheet.Pictures.Add(rowIndex, 6, countryInfo[i].Flag);
                   rowIndex++;
                }

                            //set style
                destSheet = SetStyle(destBook, destSheet);

                destSheet.AllocatedRange.AutoFitColumns();
                destBook.SaveToFile(@"..\..\CountryInfo.xls", ExcelVersion.Version97to2003);
                System.Diagnostics.Process.Start(@"..\..\CountryInfo.xls");
            }

            private static Worksheet SetStyle(Workbook workbook, Worksheet sheet)
            {
                //Sets body style
                CellStyle oddStyle = workbook.Styles.Add("oddStyle");
                oddStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
                oddStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
                oddStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
                oddStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
                oddStyle.KnownColor = ExcelColors.SkyBlue;

                CellStyle evenStyle = workbook.Styles.Add("evenStyle");
                evenStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
                evenStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
                evenStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
                evenStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
                evenStyle.KnownColor = ExcelColors.LightBlue;

                foreach (CellRange range in sheet.AllocatedRange.Rows)
                {
                    if (range.Row % 2 == 0)
                        range.CellStyleName = evenStyle.Name;
                    else
                        range.CellStyleName = oddStyle.Name;
                }

                //Sets header style
                CellStyle styleHeader = sheet.Rows[0].Style;
                styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
                styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
                styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
                styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
                styleHeader.VerticalAlignment = VerticalAlignType.Center;
                styleHeader.KnownColor = ExcelColors.Color24;
                styleHeader.Font.KnownColor = ExcelColors.White;
                styleHeader.Font.IsBold = true;

                return sheet;
            }

        }
    }
使用系统数据;
使用系统图;
使用System.Data.SqlClient;
使用系统集合;
使用制度;
使用System.Linq;
使用Spire.Xls;
使用System.Collections.Generic;
名称空间\u 4SeriersChart
{
班级计划
{
静态void Main(字符串[]参数)
{
工作簿源代码簿=新工作簿();
sourceBook.LoadFromFile(@.\..\Exercise.xls),ExcelVersion.Version97to2003);
工作表sourceSheet=sourceBook.Worksheets[0];
DataTable SourceTable=sourceSheet.ExportDataTable();
对于(int i=0;i对于(int i=1;我等待,这是什么类型的“工作表”?您使用的是什么?是POI还是Excel Interop还是什么?谢谢您的回答。我已经解决了它。我通过linq从工作表中读取数据。然后使用orderby()然后再使用orderby()。对数据进行排序。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Spire.Xls;
using System.Drawing;

namespace _4SeriersChart
{
    public class Country:IComparable<Country>
    {
        public string Name { get; set; }
        public string Capital { get; set; }
        public string Continent { get; set; }
        public float Area { get; set; }
        public float Population { get; set; }
        public Image Flag { get; set; }

        public static Comparison<Country> compare =
                delegate(Country p1, Country p2)
                {
                    if( p1.Continent.CompareTo(p2.Continent)==0)
                    {
                        return p1.Name.CompareTo(p2.Name);
                    }
                    else
                         return p1.Continent.CompareTo(p2.Continent);
                };


        #region IComparable<Country> Members

        public int CompareTo(Country other)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}