如何在对列中的值进行排序后为我的位置命名

本文关键字:排序 我的 位置 | 更新日期: 2023-09-27 18:29:21

我正在从文本文件生成excel输出。我正在使用C#来制作我的应用程序。基本上,我是按升序对第4列进行排序。但实际情况是,我的最后一列不是从1到n编号。。请看这个https://imageshack.com/i/exeLuFw3j我该怎么办。。

我的代码片段:

private void button1_Click(object sender, EventArgs e)
{
    //opening a folder
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        //processing selected text file
        int[] cols = new int[] { 15, 15, 25, 15, 15, 15 };
        string[] strLines = System.IO.File.ReadAllLines(textBox1.Text);
        StringBuilder sb = new StringBuilder();
        string line = string.Empty;
        string LastComment = string.Empty;
        string CarouselName = enter.Text;
        int iCarousel = 0;
        char seperator = ''t';
        SortedDictionary<string, ExcelData> lstExcel = new SortedDictionary<string, ExcelData>();
        ExcelData fline = null;
        for (int i = 0; i < strLines.Length; i++)
        {
            line = RemoveWhiteSpace(strLines[i]).Trim();
            if (line.Length == 0)
                continue;
            string[] cells = line.Replace("'"", "").Split(seperator);

            if (i > 0)
            {
                //if (cells[1] != LastComment)
                {
                    if (!lstExcel.ContainsKey(cells[1].Replace(" ", "_")))
                    {
                            //replacing some white spaces to underscores 
                        fline = new ExcelData();
                        lstExcel.Add(cells[1].Replace(" ", "_"), fline);
                        fline.Footprint = cells[2].Replace(" ", "_");
                        fline.Comment = cells[1].Replace(" ", "_");
                        iCarousel++;
                        if (iCarousel > 45)
                            iCarousel = 1; //once it reaches number 45 it will go back to number 1
                        LastComment = cells[1];
                        fline.Location = String.Format("{0}:{1}", CarouselName, iCarousel);
                    }
                    else
                    {
                        fline = lstExcel[cells[1].Replace(" ", "_")];
                    }
                    fline.SrNo++;
                    fline.Total++;
                }
                if (fline.Designator == null)
                    fline.Designator = new List<string>();
                fline.Designator.Add(cells[0].Replace(" ", "_"));
            }
            //Generating string in string builder
            for (int c = 0; c < cells.Length; c++)
                sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c]));
            if (i == 0)
                sb.Append("Location".PadRight(15));// Here i am adding last column Location
            else
                sb.Append(String.Format("{0}:{1}", CarouselName, iCarousel).PadRight(15));//i am starting the numbering of location here.. The Location will be like "name:1, name:2, name:3.. like this
            sb.Append("'r'n");
        }
        ExportInExcel(lstExcel, @"D:'myExcel.xls");
        System.Windows.Forms.Application.Exit();
    }
}

private void ExportInExcel(SortedDictionary<string, ExcelData> lstData, string excelPath)
{
    Microsoft.Office.Interop.Excel.Application xlApp;
    Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
    Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    xlApp = new Microsoft.Office.Interop.Excel.Application();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    xlWorkSheet.Cells[2, 1] = "Part List";
    xlWorkSheet.get_Range("A2", "A2").Font.Size = 24; //How you can set the font size
    xlWorkSheet.get_Range("A2", "A2").Font.Bold = true;
    xlWorkSheet.Cells[3, 1] = "Project PN:";
    xlWorkSheet.Cells[4, 1] = "Project Name:";
    xlWorkSheet.Cells[5, 1] = "Variant: ";
    xlWorkSheet.get_Range("A3", "A5").Font.Bold = true; //How you can set the font bold
    xlWorkSheet.Cells[6, 1] = "Report Date: " + DateTime.Now.ToString("hh:mm:ss tt");
    xlWorkSheet.Cells[7, 1] = "Footprint: " + DateTime.Now.ToString("MMM dd, yyyy");

    int rowStartIndex = 8;
    xlWorkSheet.Cells[rowStartIndex, 1] = "Sr No.";
    xlWorkSheet.Cells[rowStartIndex, 2] = "Total";
    xlWorkSheet.Cells[rowStartIndex, 3] = "Designator";            
    xlWorkSheet.Cells[rowStartIndex, 4] = "MAX PN";
    xlWorkSheet.Cells[rowStartIndex, 5] = "Footprint";
    xlWorkSheet.Cells[rowStartIndex, 6] = "Location";
    xlWorkSheet.get_Range("A" + rowStartIndex.ToString(), "F" + rowStartIndex.ToString()).Font.Bold = true; //How you can set the font bold

    //Format Columns
    xlWorkSheet.get_Range("A1", "A1").EntireColumn.ColumnWidth = 3;
    xlWorkSheet.get_Range("B1", "B1").EntireColumn.ColumnWidth = 3; 
    xlWorkSheet.get_Range("C1", "C1").EntireColumn.ColumnWidth = 25;
    xlWorkSheet.get_Range("D1", "D1").EntireColumn.ColumnWidth = 10;
    xlWorkSheet.get_Range("E1", "E1").EntireColumn.ColumnWidth = 23;
    xlWorkSheet.get_Range("F1", "F1").EntireColumn.ColumnWidth = 10; 
    xlWorkSheet.get_Range("C1", "C1").EntireColumn.WrapText = true;
    xlWorkSheet.get_Range("E1", "E1").EntireColumn.WrapText = true;
    //End
    //Header color

    xlWorkSheet.get_Range("A" + rowStartIndex.ToString(), "F" + rowStartIndex.ToString()).Interior.Color = System.Drawing.ColorTranslator.ToOle(Color.FromArgb(79,129,189));
    Color Odd = Color.FromArgb(219, 229, 241);
    Color even = Color.FromArgb(184, 204, 228);
    int i = rowStartIndex; ;
    foreach (ExcelData xls in lstData.Values)
    {
        i++;
        //i+2 : in Excel file row index is starting from 1. It's not a 0 index based collection
        xlWorkSheet.Cells[i, 1] = (i - rowStartIndex).ToString();
        xlWorkSheet.Cells[i, 2] = xls.Total.ToString();
        xlWorkSheet.Cells[i, 3] = String.Join(",", xls.Designator.ToArray());
        xlWorkSheet.Cells[i, 4] = xls.Comment;
        xlWorkSheet.Cells[i, 5] = xls.Footprint;
        xlWorkSheet.Cells[i, 6] = xls.Location;
        Color c = (i % 2) > 0 ? Odd : even;
        xlWorkSheet.get_Range("A" + i.ToString(), "F" + i.ToString()).Interior.Color = System.Drawing.ColorTranslator.ToOle(c);
    }
    //DrawBorder(xlWorkSheet, "A2", "F" + i.ToString());
    xlWorkBook.SaveAs(excelPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();
    releaseObject(xlWorkSheet);
    releaseObject(xlWorkBook);
    releaseObject(xlApp);
}

请帮帮伙计们!!!

如何在对列中的值进行排序后为我的位置命名

Stacy为此只需要更改两行

第一线

fline.Location = String.Format("{0}:{1}", CarouselName, iCarousel);

fline.Location = CarouselName;

我们不会在这里使用编号。

第二个变化是CCD_ 1方法。在循环中,我们为excel单元格指定位置。

xlWorkSheet.Cells[i, 6] = xls.Location;

现在,我们只需要将序列号附加在xls.Location属性之后。

xlWorkSheet.Cells[i, 6] = xls.Location + (i - rowStartIndex).ToString();

我们从i中减去rowStartIndex,因为行可以从任何数字开始。现在,它从8开始,我们不需要从8开始位置号。