首先 你需要添加引用Microsoft Excel 11.0 Object Library

添加方法:选择项目->引用->右击“添加引用”->选择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");               
    }
}

C#中将ListView中数据导出到Excel的更多相关文章

  1. 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>

    前台datagrid数据绑定 #region 导出到excel中    /// <summary>    /// 2014-6-6    /// </summary>    / ...

  2. Extjs中数据导出到Excel

    1.前端代码(URL+前端传入参数) window.location.href="studnetMaintainAction!exportExcel"               ...

  3. mysql中数据导出成excel文件语句

    代码如下 复制代码 mysql>select * from xi_table into outfile ’d:test.xls’; 导出为txt文件:  代码如下 复制代码 select * f ...

  4. oracle 数据库中数据导出到excel

    确保安装了PLSQL Developer工具.连接数据库. FIle--new--SQL window 运行查询,选中要导出的数据,右键--copy to excel. 或者 运行查询后.右键--se ...

  5. 将数据库中数据导出为excel表格

    public class Excel { private static Logger logger = LoggerFactory.getLogger(Excel.class); /** * 导出项目 ...

  6. Magic xpa 3.x很容易将数据导出到Excel中

    Magic xpa 3.x很方便的将表中数据导出到Excel文件中,还可以自动将表中数据生成各种图表. 通过使用自带的打印数据内部事即可实现. 1.首先将打印数据任务属性设置为“是”. 2.可使用主程 ...

  7. Asp.net网页中DataGridView数据导出到Excel

    经过上网找资料,终于找到一种可以直接将GridView中数据导出到Excel文件的方法,归纳方法如下: 1. 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码: 导出之前需要关闭分 ...

  8. 学习笔记 DataGridView数据导出为Excel

    DataGridView数据导出为Excel   怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...

  9. 机房收费系统——在VB中将MSHFlexGrid控件中的数据导出到Excel

    机房收费系统中,好多查询的窗体都包含同一个功能:将数据库中查询到的数据显示在MSHFlexGrid控件中,然后再把MSHFlexGrid控件中的数据导出到Excel表格中. 虽然之前做过学生信息管理系 ...

随机推荐

  1. sockaddr struct 类型重定义

    windows.h和winsock2.h有类型重定义我是知道的,本来就一个库来说没问题,把winsock2放到windows.h前或先定义WIN32_LEAN_AND_MEAN都能解决问题但现的出了问 ...

  2. Java for LeetCode 165 Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  3. 快速排序模板qsort(转载)

     qsort  用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 各 ...

  4. ubuntu 图形界面查看隐藏文件

    在 Linux 下以 . 开头的文件或文件夹为隐藏文件,在图形界面(nautilus)下可用 CTRL + H 显示隐藏文件,终端下者可以用 ls -a 显示所有文件.

  5. python——生成器

    python——生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个 ...

  6. AsyncHttpClient

    package com.jingle.httpstudy;   import org.apache.http.Header;   import android.app.Activity; import ...

  7. hadoop的关键进程

    hadoop集群中主要进程有master:   NameNode, ResourceManager,slaves:   DataNode, NodeManager,  RunJar, MRAppMas ...

  8. DOS下常用网络命令技巧

    DOS,即使对于许多自称了解计算机的人而言,也是一个比较陌生的词汇.然而,在网络管理过程中,DOS命令却是一个不可逾越的障碍,几乎所有的网络命令都运行在DOS界面.对初级用户而言,掌握一些常用网络命令 ...

  9. 查看MySQL配置文件路径及相关配置

    [root@DB ~]# /usr/local/mysql/bin/mysqld --verbose --help |grep -A 1 'Default options' Default optio ...

  10. UVa 11524:In-Circle(解析几何)

    Problem EIn-CircleInput: Standard Input Output: Standard Output In-circle of a triangle is the circl ...