提问者:小点点

请帮我把列表框项转换成C#中的字符串数组列表


我有listbox和项目在列表框中,我想将这些项目转换为字符串数组列表,如

var headrow = new List<string[]>
{
new string[] { "a" ,"b", "c" }
};

请帮我一下,下面是什么是已经试过的,它不起作用

Listboxitems contains values with different items

     private void Exportexcel_Click(object sender, EventArgs e)
        {
            LoginForm.qctd.IgnoreHtmlFormat = true;
            BugF = LoginForm.qctd.BugFactory;
            BugFFilter = BugF.Filter;
            BugL = (List)BugF.NewList("");

            ExcelPackage Defects_excel = new ExcelPackage();
            Defects_excel.Workbook.Worksheets.Add("Defects");

            var headerRow = Selectedfields.Items.Cast<ListViewItem>().Select(item => item.Text).ToList();
            
            string headerRange = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 65) + "1";
            var worksheet = Defects_excel.Workbook.Worksheets["Defects"];

            worksheet.Cells[headerRange].LoadFromArrays(headerRow);
     
            worksheet.Cells[headerRange].Style.Font.Color.SetColor(System.Drawing.Color.Blue);
            worksheet.Cells[headerRange].Style.Font.Size = 12;
            worksheet.Cells[headerRange].AutoFitColumns();

            
            worksheet.Cells[headerRange].Style.Border.Top.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[headerRange].Style.Border.Left.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[headerRange].Style.Border.Right.Style = ExcelBorderStyle.Thin;
            worksheet.Cells[headerRange].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "Save Excel sheet";
            saveFileDialog.Filter = "Excel files|*.xlsx|All files|*.*";
            saveFileDialog.FileName = "Defects_" + DateTime.Now.ToString("dd_MM_yyyy") + ".xlsx";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo excelFile_Defects = new FileInfo(saveFileDialog.FileName);
                Defects_excel.SaveAs(excelFile_Defects);
            }

共1个答案

匿名用户

尝试字符串(HeaderRow.ToArray())

编辑:您可能要使用

var headerRow = string(Selectedfields.Items.Cast<ListViewItem>().Select(item => item.Text).ToArray());