如何在对列中的值进行sorting后给出我的位置命名

我正在生成一个文本文件的Excel输出。 我正在使用C#来使我的应用程序。 基本上,我正在按升序排列第四列。 但是,发生什么事是我的最后一列是不是从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); } 

请帮帮忙!

斯泰西为此,你只需要改变两行

第一行

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

 fline.Location = CarouselName; 

我们不会在这里申请编号。

而第二个变化是在ExportInExcel()方法中。 在循环中,我们将位置分配给Excel单元格。

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

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

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

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