加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 22:21:06 所属栏目:大数据 来源:网络整理
导读://先添加一个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;
//先添加一个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);
            }

          } 

        


    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读