ASP.NET导出EXCEl方法使用COM.EXCEL不使用EXCEl对象
第一种:导出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对象的更多相关文章
- 【转】asp.net导出数据到Excel的三种方法
来源:http://www.cnblogs.com/lishengpeng1982/archive/2008/04/03/1135490.html 原文出处:http://blog.csdn.net/ ...
- ASP.NET导出excel表方法汇总
asp.net里导出excel表方法汇总 1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...
- asp.net导出EXCEL的好方法!(好用,导出全部数据)
1.调用方法: ExportExcel("application/ms-excel", "EXCEL名称.xls", GridView1, this.Page) ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-Excel导入和导出-自定义表模导入
系列目录 前言 上一节使用了LinqToExcel和CloseXML对Excel表进行导入和导出的简单操作,大家可以跳转到上一节查看: ASP.NET MVC5+EF6+EasyUI 后台管理系统(6 ...
- Asp.net导出Excel续章(自定义合并单元格,非Office组件)
结合上次写的导出Excel方法,这次上头要求我将列头进行一下合并 以前的效果: 改进后的效果: 在上篇文章中写到了Excel的导出方法,这次为了避免在生产环境中使用Office组件,服务器各种权限配置 ...
- asp.net导出excel示例代码
asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> ); ; ...
- [转] Asp.Net 导出 Excel 数据的9种方案
湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...
- ASP.NET导出数据到Excel 实例介绍
ASP.NET导出数据到Excel 该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...
- asp.net 导出excel文件
之前做过winfrom程序的导出excel文件的功能,感觉非常简单.现在试着做asp.net中导出excel的功能,之前用的是Microsoft.Office.Interop.Excel这个对象来实现 ...
随机推荐
- 实用chrome插件
2015年最实用的9款chrome插件 随着14年chrome浏览器的市场超过IE浏览器,chrome凭借它强劲性能和出色的使用体验真正的登上了平民级的殿堂.今天小编就为大家推荐9款自己常用的chro ...
- Egret的若干局限
Egret是个好东西,整套workflow用下来,特别顺手,对于移动端游戏的开发来说,选择Egret无疑是个不二的选择. 当然,小学语文课上老师就教过一种写作手法,欲扬先抑,笔者今天倒过来,来说说Eg ...
- #include <sstream>
1 std::istringstream 2 std::stringstream 1 std::istringstream input 1 在一个字符串string里提取部分数据,这些数据以空格' ' ...
- C# base和this[转]
new关键字引起了大家的不少关注,尤其感谢Anders Liu的补充,让我感觉博客园赋予的交流平台真的无所不在.所以,我们就有必要继续这个话题,把我认为最值得关注的关键字开展下去,本文的重点是访问关键 ...
- POJ 1556 计算几何+最短路
代码1: #include<iostream> #include<stdio.h> #include<string> #include<string.h> ...
- 位运算及在java中的应用整理
计算机编码: 原码 符号位为0表示正数,为1表示负数: 其余各位等同于真值的绝对值. 如:0000 0000 0000 0010 =2,1000 0000 0000 0010 =-2 反码 符号位的用 ...
- 无法识别的属性“targetFramework”
问题描述:无法识别的属性“targetFramework”.请注意属性名称区分大小写. 解决办法:修改.NET Framework 版本为相应版本即可,例如2.0换成4.0. 参考:http://bl ...
- 【转】使用Navicat for Oracle新建表空间、用户及权限赋予
首先.我们来新建一个表空间.打开Navicat for Oracle,输入相关的的连接信息.如下图: 填入正确的信息,连接后.我们点击面板上的“其他”下的选项“表空间”,如下图: 进入表空间的界面,我 ...
- Guestinfo.hbm(1)The markup declarations contained or pointed to by the document type declaration must be well-formed
今天启动ssh项目是居然报错了,还提示要联网启动,看了看错误信息发现,肯定是Hibernate映射文件的声明头出错了,仔细一下: hbm.xml中的dtd头直接是连接www.hibernate.org ...
- 十全大补DBA学习资源
学习oracle已经有1年多了,从开始的菜鸟到现在的DBA,一路走来~迷茫过.兴奋过.但我仍然会在DBA的道路上走下去!oracle要学的有很多,会遇到很多难题,网上有很多学习oracle好的学习资料 ...