0
我正在從文本文件生成excel輸出。我使用C#來製作我的應用程序。基本上,我正在按升序排列第4列。但是,什麼情況是我的最後一列是不是從1編號到n number..See此請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);
}
請大家幫幫忙傢伙! !