时间:2016-02-15 23:08 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【Asp.net操作Excel更轻松的实现代码】,下面是详细的讲解!
Asp.net操作Excel更轻松的实现代码
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
/// <summary>
///ExcelHelper 的摘要说明
/// </summary>
public class ExcelHelper
{
private string reportModelPath=null;
private string outPutFilePath=null;
private object missing=Missing.Value;
Excel.Application app;
Excel.Workbook workBook;
Excel.Worksheet workSheet;
Excel.Range range;
/// <summary>
/// 获取或设置报表模板路径
/// </summary>
public string ReportModelPath
{
get { return reportModelPath; }
set { reportModelPath=value; }
}
/// <summary>
/// 获取或设置输出路径
/// </summary>
public string OutPutFilePath
{
get { return outPutFilePath; }
set { outPutFilePath=value; }
}
public ExcelHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 带参ExcelHelper构造函数
/// </summary>
/// <param name="reportModelPath">报表模板路径</param>
/// <param name="outPutFilePath">输出路径</param>
public ExcelHelper(string reportModelPath, string outPutFilePath)
{
//路径验证
if (null==reportModelPath || ("").Equals(reportModelPath))
throw new Exception("报表模板路径不能为空!");
if (null==outPutFilePath || ("").Equals(outPutFilePath))
throw new Exception("输出路径不能为空!");
if (!File.Exists(reportModelPath))
throw new Exception("报表模板路径不存在!");
//设置路径值
this.ReportModelPath=reportModelPath;
this.OutPutFilePath=outPutFilePath;
//创建一个应用程序对象
app=new Excel.ApplicationClass();
//打开模板文件,获取WorkBook对象
workBook=app.Workbooks.Open(reportModelPath, missing, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing);
//得到WorkSheet对象
workSheet=workBook.Sheets.get_Item(1) as Excel.Worksheet;
}
/// <summary>
/// 给单元格设值
/// </summary>
/// <param name="rowIndex">行索引</param>
/// <param name="colIndex">列索引</param>
/// <param name="content">填充的内容</param>
public void SetCells(int rowIndex,int colIndex,object content)
{
if (null !=content)
{
content=content.ToString();
}
else
{
content=string.Empty;
}
try
{
workSheet.Cells[rowIndex, colIndex]=content;
}
catch
{
GC();
throw new Exception("向单元格[" + rowIndex + "," + colIndex + "]写数据出错!");
}
}
/// <summary>
/// 保存文件
/// </summary>
public void SaveFile()
{
try
{
workBook.SaveAs(outPutFilePath, missing, missing, missing, missing, missing,
Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing);
}
catch
{
throw new Exception("保存至文件失败!");
}
finally
{
Dispose();
}
}
/// <summary>
/// 垃圾回收处理
/// </summary>
protected void GC()
{
if (null !=app)
{
int generation=0;
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
generation=System.GC.GetGeneration(app);
System.GC.Collect(generation);
app=null;
missing=null;
}
}
/// <summary>
/// 释放资源
/// </summary>
protected void Dispose()
{
workBook.Close(null, null, null);
app.Workbooks.Close();
app.Quit();
if (null !=workSheet)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet=null;
}
if (workBook !=null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
workBook=null;
}
if (app !=null)
{
int generation=0;
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
generation=System.GC.GetGeneration(app);
System.GC.Collect(generation);
app=null;
missing=null;
}
}
}
关于Asp.net操作Excel更轻松的实现代码的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【asp】asp.net 页面转向 Response.Redirect Ser
- 【jQuery】jQuery实现倒计时跳转的例子-倒计时跳
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
