winform将listview数据导出到excel中

来源:互联网 发布:求补码的方法编程 编辑:程序博客网 时间:2024/05/29 13:25
这是在工作中遇到要将listview中的数据保存到excel中,网上的大部分是直接保存,没怎么找到在已有excel上进行追加的,于是自己看着文档写了一个,凑合着能用吧
//导出数据到excal        private void button1_Click(object sender, EventArgs e)        {            SaveFileDialog sfd = new SaveFileDialog();            sfd.DefaultExt = "xls";            sfd.Filter = "Excel文件(*.xls)|*.xls";            if (sfd.ShowDialog() == DialogResult.OK)            {                DoExport(this.listView1, sfd.FileName);            }        }        /// <summary>        /// 具体导出的方法        /// </summary>        /// <param name="listView">ListView</param>        /// <param name="strFileName">导出到的文件名</param>        private void DoExport(ListView listView, string strFileName)        {            int rowNum = listView.Items.Count;            int columnNum = listView.Items[0].SubItems.Count;            int rowIndex = 1;//行号            int columnIndex = 0;//列号            if (rowNum == 0 || string.IsNullOrEmpty(strFileName))//列表为空或导出的文件名为空            {                return;            }            if (rowNum > 0)            {                //加载Excel                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();                if (xlApp == null)//判断是否装了Excel                {                    MessageBox.Show("无法创建excel对象,可能您的系统没有安装excel");                    return;                }                xlApp.DefaultFilePath = "";                xlApp.DisplayAlerts = true;//是否需要显示提示                xlApp.SheetsInNewWorkbook = 1;//返回或设置Microsoft Excel自动插入到新工作簿中的工作表数。                Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);//创建工作铺                //将ListView的列名导入Excel表第一行                foreach (ColumnHeader dc in listView.Columns)                {                    columnIndex++;//行号自增                    xlApp.Cells[rowIndex, columnIndex] = dc.Text;                }                //将ListView中的数据导入Excel中                for (int i = 0; i < rowNum; i++)                {                    rowIndex++;//列号自增                    columnIndex = 0;                    for (int j = 0; j < columnNum; j++)                    {                        columnIndex++;                        //注意这个在导出的时候加了“\t” 的目的就是避免导出的数据显示为科学计数法。可以放在每行的首尾。                        xlApp.Cells[rowIndex, columnIndex] = Convert.ToString(listView.Items[i].SubItems[j].Text) + "\t";                    }                }                //例外需要说明的是用strFileName,Excel.XlFileFormat.xlExcel9795保存方式时 当你的Excel版本不是95、97 而是2003、2007 时导出的时候会报一个错误:异常来自 HRESULT:0x800A03EC。 解决办法就是换成strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal。                //xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);                xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);                                //xlApp = null;                //xlBook = null;                xlBook.Close(Type.Missing, Type.Missing, Type.Missing);                xlApp.Quit();                MessageBox.Show("导出文件成功!");                GC.Collect();            }        }/// <summary>        /// 在已有路径文件追加保存        /// </summary>        private void Save(string pathFile)        {            int columnNum = listView1.Items[0].SubItems.Count;            int rowIndex = 1;//行号            int columnIndex = 0;//列号            int rowNum = this.listView1.Items.Count;            if (rowNum == 0)//列表为空            {                return;            }            else            {                //加载Excel                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();                if (xlApp == null)//判断是否装了Excel                {                    MessageBox.Show("无法创建excel对象,可能您的系统没有安装excel");                    return;                }                xlApp.DefaultFilePath = "";                //Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(pathFile);//已有模版创建工作铺                                               //Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Open(pathFile, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, false, Type.Missing, Type.Missing, false, true, Type.Missing, Type.Missing, true, Type.Missing);//创建工作铺                //Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Open(pathFile, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);                Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Open(pathFile, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, false,  Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, Type.Missing, false, true, Type.Missing, Type.Missing, true, Type.Missing);//创建工作铺                //将ListView中的数据导入Excel中                for (int i = 0; i < rowNum; i++)                {                    rowIndex = Convert.ToInt32(listView1.Items[i].Text)+2;//行号由表格行号给出,可以结合自己表格修改                    columnIndex = 0;//列号归零                    for (int j = 0; j < columnNum; j++)                    {                        columnIndex++;                        //注意这个在导出的时候加了“\t” 的目的就是避免导出的数据显示为科学计数法。可以放在每行的首尾。                        xlApp.Cells[rowIndex, columnIndex] = Convert.ToString(listView1.Items[i].SubItems[j].Text) + "\t";                    }                }                //例外需要说明的是用strFileName,Excel.XlFileFormat.xlExcel9795保存方式时 当你的Excel版本不是95、97 而是2003、2007 时导出的时候会报一个错误:异常来自 HRESULT:0x800A03EC。 解决办法就是换成strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal。                xlBook.Save();                xlBook.Close(Type.Missing, Type.Missing, Type.Missing);                xlApp.Quit();                MessageBox.Show("导出文件成功!");                GC.Collect();            }        }


阅读全文
0 0
原创粉丝点击