C#中将ListView中数据导出到Excel的实例方法
时间:2023-12-07 04:00:08
添加方法:选择项目->引用->右击“添加引用”->选择COM 找到上面组件—>点击“确定”。
实现代码如下:
private void 导出数据_Click(object sender, EventArgs e)
{
ExportToExecl();
}
/// <summary>
/// 执行导出数据
/// </summary>
public void ExportToExecl()
{
System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xls";
sfd.Filter = "Excel文件(*.xls)|*.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
DoExport(this.lstPostion, 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)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("无法创建excel对象,可能您的系统没有安装excel");
return;
}
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
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, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
xlApp = null;
xlBook = null;
MessageBox.Show("OK");
}
}
标签:ListView,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java项目实现统一打印入参出参等日志
2023-05-25 18:11:06
C#如何使用Task执行异步操作
2023-01-12 03:34:41
Android实现可复用的选择页面
2022-09-25 21:08:24
![](https://img.aspxhome.com/file/2023/3/137273_0s.jpg)
Java过滤器Filter详解
2022-06-27 04:08:44
![](https://img.aspxhome.com/file/2023/5/100065_0s.png)
Java转JSON串的几种方式
2023-08-24 07:38:47
springBoot接入阿里云oss的实现步骤
2023-05-28 23:09:04
![](https://img.aspxhome.com/file/2023/2/110872_0s.png)
MyBatis学习教程(七)-Mybatis缓存介绍
2022-12-24 00:11:34
Android 实现代码混淆的实例
2023-06-01 22:13:48
使用Java程序模拟实现新冠病毒传染效果
2022-09-12 20:26:14
![](https://img.aspxhome.com/file/2023/0/129960_0s.png)
解决java 查看JDK中底层源码的实现方法
2022-03-13 20:40:14
spring-redis-session 自定义 key 和过期时间
2022-03-29 14:34:37
Android实现ListView控件的多选和全选功能实例
2023-01-04 02:26:50
浅谈java异常链与异常丢失
2023-10-24 21:53:38
Thymeleaf 3.0 自定义标签方言属性的实例讲解
2023-03-24 20:40:23
一步步教你把SpringBoot项目打包成Docker镜像
2022-01-10 12:21:02
![](https://img.aspxhome.com/file/2023/9/85439_0s.png)
Java扩展库RxJava的基本结构与适用场景小结
2022-12-27 10:03:15
java中ZXing 生成、解析二维码图片的小示例
2022-07-24 11:50:39
![](https://img.aspxhome.com/file/2023/3/125633_0s.png)
java实现的DES加密算法详解
2022-10-01 09:51:11
![](https://img.aspxhome.com/file/2023/6/94326_0s.gif)
Java多线程通信:交替打印ABAB实例
2022-04-08 06:57:28
C#用记事本编写简单WinForm窗体程序
2021-12-19 13:02:31
![](https://img.aspxhome.com/file/2023/1/84411_0s.jpg)