asp.net导出Excel类库
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using Excel = Microsoft.Office.Interop.Excel;
/// <summary>
///ExcelClass 的摘要说明
/// </summary>
public class ExcelClass
{
/// <summary>
/// 构建ExcelClass类
/// </summary>
public ExcelClass()
{
this.m_objExcel = new Excel.Application();
}
/// <summary>
/// 构建ExcelClass类
/// </summary>
/// <param name="objExcel">Excel.Application</param>
public ExcelClass(Excel.Application objExcel)
{
this.m_objExcel = objExcel;
}
/// <summary>
/// 列标号
/// </summary>
private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/// <summary>
/// 获取描述区域的字符
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public string GetAix(int x, int y)
{
char[] AChars = AList.ToCharArray();
if (x >= 26) { return ""; }
string s = "";
s = s + AChars[x - 1].ToString();
s = s + y.ToString();
return s;
}
/// <summary>
/// 给单元格赋值1
/// </summary>
/// <param name="x">行号</param>
/// <param name="y">列号</param>
/// <param name="align">对齐(CENTER、LEFT、RIGHT)</param>
/// <param name="text">值</param>
public void setValue(int y, int x, string align, string text)
{
Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);
range.set_Value(miss, text);
if (align.ToUpper() == "CENTER")
{
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
}
if (align.ToUpper() == "LEFT")
{
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
}
if (align.ToUpper() == "RIGHT")
{
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
}
}
/// <summary>
/// 给单元格赋值2
/// </summary>
/// <param name="x">行号</param>
/// <param name="y">列号</param>
/// <param name="text">值</param>
public void setValue(int y, int x, string text)
{
Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);
range.set_Value(miss, text);
}
/// <summary>
/// 给单元格赋值3
/// </summary>
/// <param name="x">行号</param>
/// <param name="y">列号</param>
/// <param name="text">值</param>
/// <param name="font">字符格式</param>
/// <param name="color">颜色</param>
public void setValue(int y, int x, string text, System.Drawing.Font font, System.Drawing.Color color)
{
this.setValue(x, y, text);
Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);
range.Font.Size = font.Size;
range.Font.Bold = font.Bold;
range.Font.Color = color;
range.Font.Name = font.Name;
range.Font.Italic = font.Italic;
range.Font.Underline = font.Underline;
}
/// <summary>
/// 插入新行
/// </summary>
/// <param name="y">模板行号</param>
public void insertRow(int y)
{
Excel.Range range = sheet.get_Range(GetAix(1, y), GetAix(25, y));
range.Copy(miss);
range.Insert(Excel.XlDirection.xlDown, miss);
range.get_Range(GetAix(1, y), GetAix(25, y));
range.Select();
sheet.Paste(miss, miss);
}
/// <summary>
/// 把剪切内容粘贴到当前区域
/// </summary>
public void past()
{
string s = "a,b,c,d,e,f,g";
sheet.Paste(sheet.get_Range(this.GetAix(10, 10), miss), s);
}
/// <summary>
/// 设置边框
/// </summary>
/// <param name="x1"></param>
/// <param name="y1"></param>
/// <param name="x2"></param>
/// <param name="y2"></param>
/// <param name="Width"></param>
public void setBorder(int x1, int y1, int x2, int y2, int Width)
{
Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), miss);
((Excel.Range)range.Cells[x1, y1]).ColumnWidth = Width;
}
public void mergeCell(int x1, int y1, int x2, int y2)
{
Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2));
range.Merge(true);
}
public Excel.Range getRange(int x1, int y1, int x2, int y2)
{
Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2));
return range;
}
private object miss = Missing.Value; //忽略的参数OLENULL
private Excel.Application m_objExcel;//Excel应用程序实例
private Excel.Workbooks m_objBooks;//工作表集合
private Excel.Workbook m_objBook;//当前操作的工作表
private Excel.Worksheet sheet;//当前操作的表格
public Excel.Worksheet CurrentSheet
{
get
{
return sheet;
}
set
{
this.sheet = value;
}
}
public Excel.Workbooks CurrentWorkBooks
{
get
{
return this.m_objBooks;
}
set
{
this.m_objBooks = value;
}
}
public Excel.Workbook CurrentWorkBook
{
get
{
return this.m_objBook;
}
set
{
this.m_objBook = value;
}
}
/// <summary>
/// 打开Excel文件
/// </summary>
/// <param name="filename">路径</param>
public void OpenExcelFile(string filename)
{
UserControl(false);
m_objExcel.Workbooks.Open(filename, miss, miss, miss, miss, miss, miss, miss,
miss, miss, miss, miss, miss, miss, miss);
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = m_objExcel.ActiveWorkbook;
sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
}
public void UserControl(bool usercontrol)
{
if (m_objExcel == null) { return; }
m_objExcel.UserControl = usercontrol;
m_objExcel.DisplayAlerts = usercontrol;
m_objExcel.Visible = usercontrol;
}
public void CreateExceFile()
{
UserControl(false);
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
}
public void SaveAs(string FileName)
{
m_objBook.SaveAs(FileName, miss, miss, miss, miss,
miss, Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlLocalSessionChanges,
miss, miss, miss, miss);
//m_objBook.Close(false, miss, miss);
}
public void ReleaseExcel()
{
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objExcel);
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)sheet);
m_objExcel = null;
m_objBooks = null;
m_objBook = null;
sheet = null;
GC.Collect();
}
/////////////////////////////////
public bool KillAllExcelApp()
{
try
{
if (m_objExcel != null) // isRunning是判断xlApp是怎么启动的flag.
{
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
//释放COM组件,其实就是将其引用计数减1
//System.Diagnostics.Process theProc;
foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
{
//先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,
//但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它:p
if (theProc.CloseMainWindow() == false)
{
theProc.Kill();
}
}
m_objExcel = null;
return true;
}
}
catch
{
return false;
}
return true;
}
}
/// <summary>
/// 点击打印按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Sendbu_Click(object sender, EventArgs e)
{
try
{
//查找部门分类用户
DataTable Duser = EduOA.DBUtility.DbHelperSQL.Query("select count(*) as count,d.Id as DId FROM OA_User u,OA_Department d where u.DepartmentID=d.Id group by d.Id").Tables[0];
ExcelClass Ec = new ExcelClass();//创建Excel操作类对象
int Ycount = 1;
Ec.CreateExceFile();//创建Excel文件
Ec.setValue(Ycount, 1, "CENTER", "组织部门");
Ec.setValue(Ycount, 2, "CENTER", "姓名");
Ec.setValue(Ycount, 3, "CENTER", "性别");
Ec.setValue(Ycount, 4, "CENTER", "职位");
Ec.setValue(Ycount, 5, "CENTER", "移动电话");
Ec.setValue(Ycount, 6, "CENTER", "电话");
Ec.setValue(Ycount, 7, "CENTER", "电子邮箱");
Ec.setBorder(1, 1, 1, 1, 50);
Ec.setBorder(1, 2, 2, 2, 20);
Ec.setBorder(1, 5, 5, 5, 20);
Ec.setBorder(1, 6, 6, 6, 20);
Ec.setBorder(1, 7, 7, 7, 20);
for (int i = 0; i < Duser.Rows.Count; i++)
{
Ycount += 1;
Ec.setValue(Ycount, 1, "CENTER", Common.DeleteHtml(Getdept(Duser.Rows[i]["count"], Duser.Rows[i]["DId"])));
DataTable dtuser = GetData(Duser.Rows[i]["DId"]);
for (int k = 0; k < dtuser.Rows.Count; k++)
{
Ec.setValue(Ycount, 2, "CENTER", dtuser.Rows[k]["TrueName"].ToString());
Ec.setValue(Ycount, 3, "CENTER", dtuser.Rows[k]["sex"].ToString());
Ec.setValue(Ycount, 4, "CENTER", dtuser.Rows[k]["PositionId"].ToString());
Ec.setValue(Ycount, 5, "CENTER", dtuser.Rows[k]["Telephone"].ToString());
Ec.setValue(Ycount, 6, "CENTER", dtuser.Rows[k]["Mobile"].ToString());
Ec.setValue(Ycount, 7, "CENTER", dtuser.Rows[k]["Email"].ToString());
Ycount += 1;
}
}
string path = Server.MapPath("Contactfiles\\");
Ec.SaveAs(path+"通讯录.xlsx");
//*******释放Excel资源***********
Ec.ReleaseExcel();
Response.Redirect("Contactfiles/通讯录.xlsx");
}
catch (Exception ex)
{
PageError("导出出错!"+ex.ToString(),"");
}
}
asp.net导出Excel类库的更多相关文章
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续3篇-导出时动态生成多Sheet EXCEL)
ExcelUtility 类库经过我(梦在旅途)近期不断的优化与新增功能,现已基本趋向稳定,功能上也基本可以满足绝大部份的EXCEL导出需求,该类库已在我们公司大型ERP系统全面使用,效果不错,今天应 ...
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续2篇-模板导出综合示例)
自ExcelUtility类推出以来,经过项目中的实际使用与不断完善,现在又做了许多的优化并增加了许多的功能,本篇不再讲述原理,直接贴出示例代码以及相关的模板.结果图,以便大家快速掌握,另外这些示例说 ...
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续篇)
上周六我发表的文章<分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility>受到了大家的热烈支持与推荐,再此表示感谢,该ExcelUtility ...
- 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页面和DATAGRID(WebControl)数据,可以导出表单头 using System;using System.Data;usi ...
- asp.net导出excel并弹出保存提示框
asp.net导出excel并弹出保存提示框 2013-07-12 | 阅:1 转:78 | 分享 腾讯空间 人人网 开心网 新浪微博 腾讯微博 搜狐空间 推荐给朋友 举报 ...
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility
1. ExcelUtility功能: 1.将数据导出到EXCEL(支持XLS,XLSX,支持多种类型模板,支持列宽自适应) 类名:ExcelUtility. Export 2.将EXCEL ...
随机推荐
- NSNumber与NSInteger的区别
Objective-C 支持的类型有两种:基本类型 和 类. 基本类型,如同C 语言中的 int 类型一样,拿来就可以直接用. 而类在使用时,必须先创建一个对象,再为对象分配空间,接着做初始化和赋值 ...
- hdu3306 Another kind of Fibonacci【矩阵快速幂】
转载请注明出处:http://www.cnblogs.com/KirisameMarisa/p/4187670.html 题目链接:http://acm.hdu.edu.cn/showproblem. ...
- STL之stack
一.stack(栈) 栈:LIFO 后进先出: 首先要指出的是,stack并非和STL的其他类模板是独立的容器,stack是自适应容器(容器适配器) stack<int, deque<in ...
- 完整的Android手机短信验证源码
短信验证功能我分两个模块来说,短信验证码的后台和代码实现短信验证码的功能. 一.短信验证码的后台 1.注册Mob账号:http://www.mob.com/#/login 2.注册成功之后, ...
- 实战nginx 基础知识总结(一)1.1
squid Squid是一个缓存Internet数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载一个主页时,可以向Squid发出一个申请,要Squid代替其进行下载,然后S ...
- php抓取ajax页面返回图片。
要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...
- Python文本处理(1)
每次处理一个字符 解决方法: 创建列表 thestring='abcdefg' thelist=list(thestring) print thelist 结果 ['a', 'b', 'c', 'd' ...
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
Android应用程序访问互联网资源时,在Wifi的情况下处理网络连接按照上文所讲述的方法步骤即可顺利实现:但如果当前Android设备的联网方式是通过移动运营商的网络服务为中转,间接访问的互联网资源 ...
- 具体解释EBS接口开发之WIP模块接口
整体说明 文档目的 本文档针对WIP模块业务功能和接口进行分析和研究,对採用并发请求方式和调用API方式分别进行介绍 内容 WIP模块经常使用标准表简单介绍 WIP事物处理组成 WIP相关业务流程 W ...
- Java For循环效率试验
非常奇怪的结果! ..! 测试方法如以下 public class Main { public static void main(String[] args){ long ti = System.cu ...