---恢复内容开始---

using Microsoft.Office.Interop.Excel; 
针对office 2003需添加引用Microsoft   Excel   11.0   Object   Library

如添加之后还不能用再using microsoft.office.core和excel

引用的dll为

Interop.Microsoft.Office.Core.dll

Interop.Excel.dll

Excel导出类:

using System;
using System.IO;
using System.Data;
using Microsoft.Office.Interop.Excel;
 
    ///   <summary> 
    ///   导出Excal文件
    ///   </summary> 
public class ImportExcel
{
    private string title;
    private string rpt_name;
    private string outFilePath;
    private System.Data.DataTable dt;
    private System.Data.DataTable outTable;
 
    public ImportExcel()
    {
 
    }
 
    ///   <summary> 
    ///   设置Excel标题 
    ///   </summary> 
    public string rptName
    {
        get
        {
            return title;
        }
        set
        {
            title = value;
        }
    }
 
    ///   <summary> 
    ///   需要导入EXCEL的表 
    ///   </summary> 
    public System.Data.DataTable RptData
    {
        get
        {
            return dt;
        }
        set
        {
            dt = value;
        }
    }
 
    ///   <summary> 
    ///   设置输出文件的存放位置 
    ///   </summary> 
    public string OutFilePath
    {
        get
        {
            return outFilePath;
        }
        set
        {
            outFilePath = value;
        }
    }
 
    ///   <summary> 
    ///   构造函数,使用该类需传入Excel标题,DataTable,文件的存放路径 
    ///   </summary> 
    ///   <param   name="rptName">要设置的Excel标题</param> 
    ///   <param   name="dtable">要填充Excel的表</param> 
    ///   <param   name="FilePath">要存放的路径</param> 
    public ImportExcel(string rptName, System.Data.DataTable dtable, string file_path)
    {
        rpt_name = rptName;
        outTable = dtable;
        outFilePath = file_path;
 
        try
        {
            this.InsertExcel();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }
 
    ///   <summary> 
    ///   该函数向Excel表中插入记录,要插入的数据为一个内存表.其中内存表中的列名为表中的列名 
    ///   该函数所使用的动态连接库Excel.dll,Office.dll,VBIDE.dll. 
    ///   </summary> 
    public void InsertExcel()
    {
        int col_count = 0;
        int row_count = 0;
        int k = 0;
        int l = 0;
 
        //检查文件是否存在 
        if (File.Exists(outFilePath))
        {
            throw new Exception("文件已存在,创建失败!");
        }
 
        Microsoft.Office.Interop.Excel.ApplicationClass rptApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
 
        rptApp.Application.Workbooks.Add(true);
 
        rptApp.Visible = false;
 
        object missing = System.Reflection.Missing.Value;
 
        Microsoft.Office.Interop.Excel.Workbook rptBook = rptApp.Workbooks[1];
        Microsoft.Office.Interop.Excel.Worksheet rptSheet = (Microsoft.Office.Interop.Excel.Worksheet)rptBook.ActiveSheet;
 
 
        //设置报表的标题 
        rptSheet.Cells[1, 1] = rpt_name;
 
        //插入列标题 
        foreach (DataColumn col in outTable.Columns)
        {
            col_count++;
            rptSheet.Cells[2, col_count] = col.ColumnName.ToString();
        }
        //设置列标题格式
        rptSheet.get_Range(rptSheet.Cells[2, 1],rptSheet.Cells[2, col_count]).Font.Bold = true; // 是否加粗:是
 
        //设置单元格的格式 
        foreach (DataRow dr in outTable.Rows)
        {
            row_count++;
            col_count = 0;
            foreach (DataColumn col in outTable.Columns)
            {
                col_count++;
                if (col.DataType == System.Type.GetType("System.String"))
                {
                    rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count + 2, col_count]).NumberFormatLocal = "@";
                }
                else if (col.DataType == System.Type.GetType("System.DateTime"))
                {
                    rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count + 2, col_count]).NumberFormatLocal = "yyyy-m-d";
                }
            }
        }
 
        //向报表插入记录 
        for (int i = 0; i < outTable.Rows.Count; i++)
        {
            for (int j = 0; j < outTable.Columns.Count; j++)
            {
                k = i + 3;
                l = j + 1;
                rptSheet.Cells[k, l] = outTable.Rows[i][j].ToString();
                rptSheet.get_Range(rptSheet.Cells[1, l], rptSheet.Cells[1, l]).Borders.LineStyle = 7;
                rptSheet.get_Range(rptSheet.Cells[2, l], rptSheet.Cells[2, l]).Borders.LineStyle = 1;
                rptSheet.get_Range(rptSheet.Cells[k, l], rptSheet.Cells[k, l]).Borders.LineStyle = 1;
            }
        }
 
        //设置标题的格式 
        rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, 1]).Font.Bold = true;
        rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, 1]).Font.Size = 22;
 
        //设置报表的标题为跨列居中合并单元格 
        rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, col_count]).Select();
        rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[1, col_count]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenterAcrossSelection;
 
        //设置报表表格为最适应宽度 
        rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count, col_count]).Select();
        rptSheet.get_Range(rptSheet.Cells[1, 1], rptSheet.Cells[row_count, col_count]).Columns.AutoFit();
 
        //另存文件到指定路径下 
        rptBook.SaveAs(outFilePath, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
        //关闭文件 
        rptBook.Close(false, outFilePath, true);
        rptApp.Quit();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rptSheet);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rptBook);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(rptApp);
        //强制释放无用资源 
        GC.Collect();
    }
}

错误:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。

解决方法一:
控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Excel 应用程序
之后
单击属性打开此应用程序的属性对话框。  
2. 单击标识选项卡,然后选择交互式用户。 
3.单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后
自定义->编辑->添加ASP.NET账户和IUSER_计算机名: 需要本地激活,和本地访问两个权限

4."标识"选项卡 选择交互式用户

---恢复内容结束---

asp.net(C#) Excel导出类 导出.xls文件的更多相关文章

  1. 用javah 导出类的头文件, 常见的错误及正确的使用方法

    ******************************************************************************** 用javah 导出类的头文件, 常见的 ...

  2. Java解析Excel工具类(兼容xls和xlsx)

    依赖jar <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml&l ...

  3. Asp .Net Core Excel导入和导出

    ASP .Net Core使用EPPlus实现Api导入导出,这里使用是EPPlus 4.5.2.1版本,.Net Core 2.2.在linux上运行的时候需要安装libgdiplus . 下面我们 ...

  4. C#:导入Excel通用类(Xls格式)

    PS:在CSV格式和XLSX格式中有写到通用调用的接口和引用的插件,所以在这个xls格式里面并没有那么详细,只是配上xls通用类. 一.引用插件NPOI.dll.NPOI.OOXML.dll.NPOI ...

  5. Android 导入导出CSV,xls文件 .

    1 . http://www.bangchui.org/read.php?tid=62 2 .http://blog.csdn.net/xinzheng_wang/article/details/77 ...

  6. winform NPOI excel 导出并选择保存文件路径

    public void ExcelOp(DataGridView gdv,ArrayList selHead) { if (selHead.Count==0) { MessageBox.Show(&q ...

  7. php中使用PHPExcel读写excel(xls)文件的方法

    首先从GitHub上下载 excel的相关类库 下载地址:https://github.com/PHPOffice/PHPExcel 以下是从excel中获取数据 <?php /** * * @ ...

  8. 导入导出Excel工具类ExcelUtil

    前言 前段时间做的分布式集成平台项目中,许多模块都用到了导入导出Excel的功能,于是决定封装一个ExcelUtil类,专门用来处理Excel的导入和导出 本项目的持久化层用的是JPA(底层用hibe ...

  9. MVC NPOI Linq导出Excel通用类

    之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...

随机推荐

  1. 深入浅出讲解:php的socket通信

    对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问:1.         什么是TCP/IP.UDP?2.         Socke ...

  2. golang的channel使用

    chan使用前必须用make来创建,可以有多个槽位 0个槽位相当于消息,1个槽位相当于锁,多个槽位就是读者-写者 go func

  3. select 多表查询

    连接查询实际上是通过各个表之间共同列的关联性来查询数据的,它是关系数据库查询最主要的特征. select 表1.字段名1,表2.字段名2,... from 表1,表2 where 连接条件 连接查询分 ...

  4. Acadia Lab 203 + Lab 231

    在做完 Lab 6 之后,惊觉选做实验缺口很大,于是遍历了一遍夏任务,找到了一条最省力的路线. 做完 Lab 6 的连线不用拆,可以接下来做以下两个实验: Lab 203 网络时钟 核心代码如下: v ...

  5. php底层

    http://www.phpchina.com/member.php?mod=logging&action=login 我们从未手动开启过PHP的相关进程,它是随着Apache的启动而运行的: ...

  6. 循序渐进Python3(十一) --6--  Ajax 实现跨域请求 jsonp 和 cors

    Ajax操作如何实现跨域请求?       Ajax (XMLHttpRequest)请求受到同源策略的限制.       Ajax通过XMLHttpRequest能够与远程的服务器进行信息交互,另外 ...

  7. ViewPager+PagerTabStrip实现页面的切换

    页面切换效果图 首先创建布局: 代码: <?xml version="1.0" encoding="utf-8"?><LinearLayout ...

  8. 转载:Solr的自动完成实现方式(第三部分:Suggester方式续)

    转自:http://www.cnblogs.com/ibook360/archive/2011/11/30/2269126.html 在之前的两个部分(part1.part2)中,我们学会了如何配置和 ...

  9. 什么是业务运维,企业如何实现互联网+业务与IT的融合

    业务运维并不是一个新概念,针对传统信息架构提出的业务服务管理就是把以业务为核心的IT系统与IT基础设施性能进行整合运维的解决方案.然而随着互联网+转型的不断推进,基础设施的智能化和广泛云化成为IT发展 ...

  10. maven project 更新总是jre-1.5

    解决如下: <build>    <plugins>          <plugin>                <groupId>org.apa ...