listview导出到xls 取当前时间为文件名 xls导入到listview

本文介绍了一个使用C#编写的简单程序,该程序能够从ListView导出数据到Excel文件,并能将数据从指定的Excel文件(.xls)导入到ListView中。导出功能通过创建SaveFileDialog来让用户选择保存位置,并使用StreamWriter将ListView的内容写入Excel文件。导入功能则通过读取指定路径的文件,解析每一行的数据并填充到ListView中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private void buttonExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog exe = new SaveFileDialog();
            exe.Filter = "Execl files (*.xls)|*.xls";
            exe.FilterIndex = 0;
            exe.RestoreDirectory = true;
            //exe.CreatePrompt = true;
            exe.Title = "Export Excel File";
            exe.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            exe.FileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyy-MM-dd HH时mm分ss秒"), "导出");
            DialogResult dr = exe.ShowDialog();
            if (dr != DialogResult.OK)
            {
                return;
            }

            Stream ms;
            ms = exe.OpenFile();
            StreamWriter sw = new StreamWriter(ms, System.Text.Encoding.GetEncoding(-0));
            string str = "";
            try
            {
                for (int i = 0; i < listAccount.Columns.Count; i++)
                {
                    if (i > 0)
                    {
                        str += "\t";
                    }
                    str += listAccount.Columns[i].Text;
                }
                sw.WriteLine(str);
                for (int j = 0; j < listAccount.Items.Count; j++)
                {
                    string temp = "";
                    for (int k = 0; k < listAccount.Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            temp += "\t";
                        }
                        string cell = listAccount.Items[j].SubItems[k].Text;
                        //cell = cell.Replace(" ", "").Replace("\r", "").Replace("\n", "").Replace("\r\n", "");
                        temp += "\"" + cell + "\"";
                    }
                    sw.WriteLine(temp);
                }
                sw.Close();
                ms.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                sw.Close();
                ms.Close();
            }
        }
        public void xlsdaoru(string filepath, ListView listAccount)
        {
            try
            {
                if (File.Exists(filepath) == false)
                {
                    FileStream fs = File.Create(filepath);
                    fs.Close();
                }
                else
                {
                    
                    string[] arr1 = System.IO.File.ReadAllLines(@".\log.xls", Encoding.Default);
                    if(arr1.Length != 0)
                    {
                        listAccount.Columns.Clear();
                    }
                    for (int x = 0; x < arr1.Length; x++)
                    {
                        string[] resultString = arr1[x].Split('\t');
                        if (x == 0)
                        {
                            for (int i = 0; i < resultString.Length; i++)
                            {
                                ColumnHeader ch = new ColumnHeader();
                                ch.Name = "a" + i.ToString();
                                ch.Text = resultString[i];
                                if (listAccount.Width-20 - ((resultString.Length - 3) * 50) > 400)
                                {
                                    switch (i)
                                    {
                                        case 0: ch.Width = (listAccount.Width-20 - ((resultString.Length - 3) * 50))/3; break;
                                        case 1: ch.Width = (listAccount.Width -20- ((resultString.Length - 3) * 50)) / 3; ; break;
                                        case 2: ch.Width = (listAccount.Width-20 - ((resultString.Length - 3) * 50)) / 3; ; break;
                                        default: ch.Width = 50; break;
                                    }
                                }
                                else
                                {
                                    switch (i)
                                    {
                                        case 0: ch.Width = 100; break;
                                        case 1: ch.Width = 150; break;
                                        case 2: ch.Width = 150; break;
                                        default: ch.Width = 50; break;
                                    }
                                }
                                ch.TextAlign = HorizontalAlignment.Center;
                                listAccount.Columns.Add(ch);
                            }
                        }
                        else
                        {
                            ListViewItem lvi;
                            lvi = new ListViewItem();
                            lvi.Text = resultString[0];
                            for (int i = 1; i < resultString.Length; i++)
                            {
                                lvi.SubItems.Add(resultString[i]);
                            }
                            listAccount.Items.Add(lvi);
                            listAccount.EndUpdate();
                        }
                    }
                }
            }
            catch (Exception)
            {
                DialogResult dr = MessageBox.Show("数据存储文件被打开,无法加载,请关闭文件后运行程序。", "错误!!!");
                System.Environment.Exit(0);
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值