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
- winform将listview数据导出到excel中
- winform 将Oracle数据导出到excel中
- 将ListView中的数据导出到Excel中
- C#如何将ListView中的数据导出到Excel中
- Winform中导出数据到Excel
- C# Winform中导出数据到Excel
- winform中 将listview的数据导出至 excel (最简单的方法)
- winform中 以excel的格式导出 listview的数据 到指定目标文件夹
- 将数据导出到EXCEL中
- 将DataTable数据导出到Excel中
- 将dataview中数据导出到Excel
- C# 将ListView数据导出到Excel文本之方法
- 如何将ListView中的数据导出到Excel
- 通过OLE将ListView数据导出到Excel
- C# 将ListView数据导出到Excel文本之方法
- C# 将ListView数据导出到Excel文本之方法
- 将ListView控件中的数据导出到Excel文
- C# 将listview 中的数据导出到excel 文件
- 学习各种语言初级知识语法了解
- org.asynchttpclient.AsyncHttpClient
- ubuntu1604 server安装及图形界面安装
- POI 设置单元格背景色,背景色编码与实际颜色对照表
- 《Spring Boot in Action》【4. 测试】
- winform将listview数据导出到excel中
- hitTest和pointInside方法你真的熟吗?
- css3弹性盒模型flex快速入门与上手1
- JavaScript匿名自执行函数的定义
- 基于主动学习的高光谱图像分类方法研究
- Django中的一些常用内置函数
- JavaScript的封装
- [VS2010]_[Windows]_[Debug模式下LoadLibrary错误87]
- flume高级配置——数据流的复制、分流、负载均衡、故障转移