1
private void button3_Click(object sender, EventArgs e)
    {
        SaveFileDialog file = new SaveFileDialog { 
                Title = "Save File To",
                FileName = cmbCategory.Text + " " + Description.Text + ".csv",
                Filter = "CSV (*.csv)|*.csv",
                FilterIndex = 0,
                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        };

        if (file.ShowDialog() == DialogResult.OK) {
            string[] headers = lstResult.Columns
                .OfType<ColumnHeader>()
                .Select(header => header.Text.Trim()).ToArray();

            string[][] items = lstResult.Items
                .OfType<ListViewItem>()
                .Select(Details => Details.SubItems
                    .OfType<ListViewItem.ListViewSubItem>()
                    .Select(Detail => Detail.Text).ToArray()).ToArray();

            string table = string.Join(",", headers) + Environment.NewLine;
            foreach (string[] a in items)
            {
                table += string.Join(",", a) + Environment.NewLine;
            }
            table = table.TrimEnd('\r', '\n');
            System.IO.File.WriteAllText(file.FileName, table);
        }

    }

到目前为止,我尝试在将数据值传输到 excel 文件时自动格式化我的列,但它与“lstResult.Columns.Autofit() 为什么会这样?因为当我打开我的 excel 文件时它显示我必须根据导出的数据值的宽度调整每列的大小。请帮助

4

0 回答 0