日期:2014-05-18  浏览次数:20850 次

listview 导出 excel代码
listview 导出 excel代码listview 导出 excel代码listview 导出 excel代码listview 导出 excel代码listview 导出 excel代码

------解决方案--------------------
C# code
 if (pListView.Items.Count == 0)
            {
                MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "导出文件保存路径";

                saveFileDialog.ShowDialog();
                progreesBar.Visible = true;
                Stream myStream;
                myStream = saveFileDialog.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
                string str = "";

                try
                {
                    //写标题
                    for (int i = 0; i < pListView.Columns.Count; i++)
                    {
                        if (i > 0)
                        {
                            str += "\t";
                        }
                        str += pListView.Columns[i].Text.ToString();
                    }
                    sw.WriteLine(str);
                    //写内容
                    for (int j = 0; j < pListView.Items.Count; j++)
                    {
                        string tempStr = "";
                        for (int k = 0; k < pListView.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                tempStr += "\t";
                            }
                            tempStr += pListView.Items[j].SubItems[k].Text.ToString();
                        }
                        sw.WriteLine(tempStr);
                        progreesBar.Value += 100 / pListView.Items.Count;
                    }
                    sw.Close();
                    myStream.Close();
                    progreesBar.Value = 100;
                    MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    progreesBar.Value = 0;
                    progreesBar.Visible = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "友情提示", MessageBoxButtons.OK);
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
        }


       
    }