第一种:导出gridVIEW中的数据,用hansTABLE做离线表,将数据库中指定表中的所有数据按GRIDVIEW中绑定的ID导出

只能导出数据不能去操作相应的EXCEl表格,不能对EXCEL中的数据进行格式化操作,如:字体颜色,大小,单元格合并等

/// <summary>
    /// 导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        string IDList = "'0'";
        for (int i = 0; i < GVData.Rows.Count; i++)
        {
            Label LabVis = (Label)GVData.Rows[i].FindControl("LabVisible");
            IDList = IDList + ",'" + LabVis.Text.ToString() + "'";
        }
        Hashtable MyTable = new Hashtable();
        MyTable.Add("TrueName", "姓名");
        MyTable.Add("DateType", "请假类别及事由");
        MyTable.Add("DateFR", "开始时间");
        MyTable.Add("DateTo", "结束时间");
        MyTable.Add("BuMenName", "组别");
        MyTable.Add("Days", "休假天数");
        MyTable.Add("SDays", "实修天数");
        MyTable.Add("Note", "备注");
        string sql = "select  TrueName "+"姓名"+",DateType "+"请假类别及事由"+",DateFR "+"开始时间"+",DateTO "+"结束时间"+",Note "+"备注"+",BuMenName "+"所属部门"+",Days "+"休假天数"+",SDays "+"实修天数"+" from T_ProjectLeave,Erpuser,erpbumen where T_ProjectLeave.userid= Erpuser.id and T_projectLeave.bumenID=erpbumen.ID and T_projectLeave.ID in (" + IDList + ") order by userID desc";
        ZWL.Common.DataToExcel.GridViewToExcel(ZWL.DBUtility.DbHelperSQL.GetDataSet(sql), MyTable, "Excel报表");
    }

GRIDVIEWToExcel方法:

#region 将DataTable的数据导出显示为报表(不使用Excel对象,使用COM.Excel)这里必须要引入Com.EXCEl.dll文件

#region 使用示例
        public static void GridViewToExcel(DataSet MyData, Hashtable nameList,string ReportTitle)
        {
            string FilePath = System.Web.HttpContext.Current.Server.MapPath("../") + "ReportFile\\";
            //利用excel对象
            DataToExcel dte = new DataToExcel();
            string filename = "";
            try
            {
                if (MyData.Tables[0].Rows.Count > 0)
                {
                    filename = dte.DataExcel(MyData.Tables[0], ReportTitle, FilePath, nameList);
                }
            }
            catch
            {}
            if (filename != "")
            {
                System.Web.HttpContext.Current.Response.Redirect("../ReportFile/" + filename, true);
            }
        }

#endregion

/// <summary>
        /// 将DataTable的数据导出显示为报表(不使用Excel对象)
        /// </summary>
        /// <param name="dt">数据DataTable</param>
        /// <param name="strTitle">标题</param>
        /// <param name="FilePath">生成文件的路径</param>
        /// <param name="nameList"></param>
        /// <returns></returns>
        public string DataExcel(System.Data.DataTable dt, string strTitle, string FilePath, Hashtable nameList)
        {
            COM.Excel.cExcelFile excel = new COM.Excel.cExcelFile();
            ClearFile(FilePath);
            string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xls";
            excel.CreateFile(FilePath + filename);
            excel.PrintGridLines = false;

COM.Excel.cExcelFile.MarginTypes mt1 = COM.Excel.cExcelFile.MarginTypes.xlsTopMargin;
            COM.Excel.cExcelFile.MarginTypes mt2 = COM.Excel.cExcelFile.MarginTypes.xlsLeftMargin;
            COM.Excel.cExcelFile.MarginTypes mt3 = COM.Excel.cExcelFile.MarginTypes.xlsRightMargin;
            COM.Excel.cExcelFile.MarginTypes mt4 = COM.Excel.cExcelFile.MarginTypes.xlsBottomMargin;

double height = 1.5;
            excel.SetMargin(ref mt1, ref height);
            excel.SetMargin(ref mt2, ref height);
            excel.SetMargin(ref mt3, ref height);
            excel.SetMargin(ref mt4, ref height);

COM.Excel.cExcelFile.FontFormatting ff = COM.Excel.cExcelFile.FontFormatting.xlsNoFormat;
            string font = "宋体";
            short fontsize = 9;
            excel.SetFont(ref font, ref fontsize, ref ff);

byte b1 = 1,
                b2 = 12;
            short s3 = 12;
            excel.SetColumnWidth(ref b1, ref b2, ref s3);

string header = "页眉";
            string footer = "页脚";
            excel.SetHeader(ref header);
            excel.SetFooter(ref footer);

COM.Excel.cExcelFile.ValueTypes vt = COM.Excel.cExcelFile.ValueTypes.xlsText;
            COM.Excel.cExcelFile.CellFont cf = COM.Excel.cExcelFile.CellFont.xlsFont0;
            COM.Excel.cExcelFile.CellAlignment ca = COM.Excel.cExcelFile.CellAlignment.xlsCentreAlign;
            COM.Excel.cExcelFile.CellHiddenLocked chl = COM.Excel.cExcelFile.CellHiddenLocked.xlsNormal;

// 报表标题
            int cellformat = 1;
            //   int rowindex = 1,colindex = 3;     
            //   object title = (object)strTitle;
            //   excel.WriteValue(ref vt, ref cf, ref ca, ref chl,ref rowindex,ref colindex,ref title,ref cellformat);

int rowIndex = 1;//起始行
            int colIndex = 0;

//取得列标题    
            foreach (DataColumn colhead in dt.Columns)
            {
                colIndex++;
                string name = colhead.ColumnName.Trim();
                object namestr = (object)name;
                IDictionaryEnumerator Enum = nameList.GetEnumerator();
                while (Enum.MoveNext())
                {
                    if (Enum.Key.ToString().Trim() == name)
                    {
                        namestr = Enum.Value;
                    }
                }
                excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref namestr, ref cellformat);

}

//取得表格中的数据   
            foreach (DataRow row in dt.Rows)
            {
                rowIndex++;
                colIndex = 0;
                foreach (DataColumn col in dt.Columns)
                {
                    colIndex++;
                    if (col.DataType == System.Type.GetType("System.DateTime"))
                    {
                        object str;
                        try
                        {
                             str = (object)(Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                        }
                        catch
                        {
                            str = "";
                        }
                        excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
                    }
                    else
                    {
                        object str = (object)row[col.ColumnName].ToString();
                        excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
                    }
                }
            }
            int ret = excel.CloseFile();

//   if(ret!=0)
            //   {
            //    //MessageBox.Show(this,"Error!");
            //   }
            //   else
            //   {
            //    //MessageBox.Show(this,"请打开文件c:\\test.xls!");
            //   }
            return filename;

}

#endregion

#region  清理过时的Excel文件

private void ClearFile(string FilePath)
        {
            String[] Files = System.IO.Directory.GetFiles(FilePath);
            if (Files.Length > 10)
            {
                for (int i = 0; i < 10; i++)
                {
                    try
                    {
                        System.IO.File.Delete(Files[i]);
                    }
                    catch
                    {
                    }

}
            }
        }
        #endregion

ASP.NET导出EXCEl方法使用COM.EXCEL不使用EXCEl对象的更多相关文章

  1. 【转】asp.net导出数据到Excel的三种方法

    来源:http://www.cnblogs.com/lishengpeng1982/archive/2008/04/03/1135490.html 原文出处:http://blog.csdn.net/ ...

  2. ASP.NET导出excel表方法汇总

    asp.net里导出excel表方法汇总  1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...

  3. asp.net导出EXCEL的好方法!(好用,导出全部数据)

    1.调用方法: ExportExcel("application/ms-excel", "EXCEL名称.xls", GridView1, this.Page) ...

  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出-自定义表模导入

    系列目录 前言 上一节使用了LinqToExcel和CloseXML对Excel表进行导入和导出的简单操作,大家可以跳转到上一节查看: ASP.NET MVC5+EF6+EasyUI 后台管理系统(6 ...

  5. Asp.net导出Excel续章(自定义合并单元格,非Office组件)

    结合上次写的导出Excel方法,这次上头要求我将列头进行一下合并 以前的效果: 改进后的效果: 在上篇文章中写到了Excel的导出方法,这次为了避免在生产环境中使用Office组件,服务器各种权限配置 ...

  6. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...

  7. [转] Asp.Net 导出 Excel 数据的9种方案

    湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...

  8. ASP.NET导出数据到Excel 实例介绍

    ASP.NET导出数据到Excel  该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...

  9. asp.net 导出excel文件

    之前做过winfrom程序的导出excel文件的功能,感觉非常简单.现在试着做asp.net中导出excel的功能,之前用的是Microsoft.Office.Interop.Excel这个对象来实现 ...

随机推荐

  1. keil MDK的信号函数

    keil的信号函数用于模拟和测试串行IO,模拟IO,端口通讯等重复发生的外部事件. 信号函数以关键字signal开头.在函数中必须调用twatch用于延时,不然keil会陷入死循环.twatch函数的 ...

  2. TCP粘包分析与处理

    http://www.cnblogs.com/liyux/p/5594423.html http://www.cnblogs.com/liyux/p/5603826.html

  3. 项目与软件推荐之编辑器-QOwnNotes(刺激自己)

    项目与软件推荐之编辑器-QOwnNotes 今天推荐一款软件 QOwnNotes,是一款普通文本笔记软件.以某个路径为目录,罗列出目录下所有的 md 文件或者 txt 文件. 有如下亮点: 启动速度快 ...

  4. poj1504--求两个数的反转数的和的反转数

    题意:给定4321 5678,结果再反转(1234+8756) 一开始以为是poj1503一样,就稀里糊涂的敲代码,实际上有不同 如题:先求1234 + 8765 ------------------ ...

  5. openStack CI(Continuous interaction)/CD(Continuous delivery) Gerrit/Jenkins安装及集成,插件配置

    preFace: CI/CD practice part contains the following action items and fields of expertise: Gerrit ins ...

  6. MyGui笔记(1)建立第一个工程

    记录下学习 MyGui的一些笔记,从建立第一个工程开始. 步骤: 1.右键MYGUI解决方案,添加→新建项目,选择“Win32 项目”,名称为:TestHello.下一步,勾选“空项目”. 2.设置工 ...

  7. AndroidUI 布局动画-为列表添加布局动画效果

    新建一个Android project ,使MainActivity 继承自 ListActivity: public class MainActivity extends ListActivity ...

  8. 常用的ASCII码对照表

  9. redis 错误。

    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Com ...

  10. Java提高学习之Object(3)

    终止 问: finalize()方法是用来做什么的? 答: finalize()方法可以被子类对象所覆盖,然后作为一个终结者,当GC被调用的时候完成最后的清理工作(例如释放系统资源之类).这就是终止. ...