VB C# listview 中的数据导出到excel 文件

前端之家收集整理的这篇文章主要介绍了VB C# listview 中的数据导出到excel 文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//先添加一个button1,text为export to excel

//添加一个saveFileDialog1,filter设置为"Excel 文件(*.xls)|*.xls";


 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;
using System.Reflection;
using System.IO;


namespace TestExportExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

           
        private void button1_Click(object sender,EventArgs e)
        {

            //string saveFileName = "";
            //saveFileDialog1 = new SaveFileDialog();
            //saveFileDialog1.Filter = "Excel 文件(*.xls)|*.xls";
           // saveFileDialog1.RestoreDirectory = true;           
            //saveFileName = saveFileDialog1.FileName;

            saveFileDialog1.FileName = DateTime.Now.ToString("yyyy-MM-dd"); 
          
           if (saveFileDialog1.ShowDialog() ==DialogResult.OK)
           {             
                TurnToExcel(listView1,"LOG");

           }                        
            
           
          
        }

        
        public void TurnToExcel(ListView listView1,string sheet1)
        {
           
            string Sheetname = sheet1;
            ListView listView = listView1;
            if (listView.Items.Count < 1)
                return;
            try
            {
                ExcelApplication MyExcel = new ExcelApplication();

                MyExcel.Visible = true;   //display excel application;if value set 'false',please enable the ' finally' code below;
                if (MyExcel == null)
                {
                    return;
                }

                Workbooks MyWorkBooks = (Workbooks)MyExcel.Workbooks;

                Workbook MyWorkBook = (Workbook)MyWorkBooks.Add(Missing.Value);

                Worksheet MyWorkSheet = (Worksheet)MyWorkBook.Worksheets[1];


                Range MyRange = MyWorkSheet.get_Range("A1","H1");
                MyRange = MyRange.get_Resize(1,listView.Columns.Count);
                object[] MyHeader = new object[listView.Columns.Count];
                for (int i = 0; i < listView.Columns.Count; i++)
                {
                    MyHeader.SetValue(listView.Columns[i].Text,i);
                }
                MyRange.Value2 = MyHeader;
                MyWorkSheet.Name = Sheetname;

                if (listView.Items.Count > 0)
                {
                    MyRange = MyWorkSheet.get_Range("A2",Missing.Value);
                    object[,] MyData = new Object[listView.Items.Count,listView.Columns.Count];
                    for (int j = 0; j < listView1.Items.Count; j++)
                    {
                        ListViewItem lvi = listView1.Items[j];
                        for (int k = 0; k < listView.Columns.Count; k++)
                        {

                            MyData[j,k] = lvi.SubItems[k].Text;
                        }

                    }
                    MyRange = MyRange.get_Resize(listView.Items.Count,listView.Columns.Count);
                    MyRange.Value2 = MyData;
                    MyRange.EntireColumn.AutoFit();
                }

               
                 try
               {
                  object missing = System.Reflection.Missing.Value;
                  MyWorkBook.Saved = true;
                  MyWorkBook.SaveAs(saveFileDialog1.FileName,Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,missing,false,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,missing);
               }
               catch (Exception e1)
               {
                   MessageBox.Show("Export Error,Maybe the file is opened by other application!\n" + e1.Message);
               }
                /*
                 finally
                     {
                          MyExcel.Quit();
                          System.GC.Collect();
                      }
                 */                  

              // MyExcel = null;

            }
            catch (Exception Err)
            {
                MessageBox.Show(Err.Message);
            }

          } 

        


    }
}

猜你在找的VB相关文章