知识大全 asp.net(C#)套用模板操作Excel
Posted 文件
篇首语:会当凌绝顶,一览众山小。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 asp.net(C#)套用模板操作Excel相关的知识,希望对你有一定的参考价值。
asp.net(C#)套用模板操作Excel 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
当需要输出带大量公式的Excel文档的时候 在代码里写公式就太累了
用设计好的Excel模板 复制一下 往里面添加数据比较省事
模板
导出文件
大气象
using System;
using System Data;
using System Configuration;
using System Web;
using System Web Security;
using System Web UI;
using System Web UI WebControls;
using System Web UI WebControls WebParts;
using System Web UI HtmlControls;
using System IO;
using System Reflection;
using Microsoft Office Interop Excel;
public partial class _Default : System Web UI Page
protected void Page_Load(object sender EventArgs e)
if (!IsPostBack)
Bind();
private void Bind()
//模板文件
string TempletFileName = Server MapPath( template/ ) + template xlsx ;
//导出文件
string ReportFileName = Server MapPath( xls/ ) + out xlsx ;
string strTempletFile = Path GetFileName(TempletFileName);
//将模板文件复制到输出文件
FileInfo mode = new FileInfo(TempletFileName);
mode CopyTo(ReportFileName true);
//打开excel
object missing = Missing Value;
Application app = null;
Workbook wb = null;
Worksheet ws = null;
Range r = null;
//
app = new Microsoft Office Interop Excel Application();
wb = app Workbooks Open(ReportFileName false missing missing missing missing missing missing missing missing missing missing missing missing missing);
app Visible = true;
//得到WorkSheet对象
ws = (Worksheet)wb Worksheets get_Item( );
//添加或修改WorkSheet里的数据
ws Cells[ ] = ;
ws Cells[ ] = ;
ws Cells[ ] = ;
//代码里写个公式
r = (Range)ws Cells[ ];
r Formula = =A *B ;
//输出Excel文件并退出
wb Save();
wb Close(null null null);
app Workbooks Close();
app Application Quit();
app Quit();
System Runtime InteropServices Marshal ReleaseComObject(ws);
System Runtime InteropServices Marshal ReleaseComObject(wb);
System Runtime InteropServices Marshal ReleaseComObject(app);
ws = null;
wb = null;
app = null;
using System;
using System IO;
using System Data;
using System Reflection;
using System Diagnostics;
using cfg = System Configuration;
//using Excel;
namespace ExcelHelperTest
/**//// <summary>
/// 功能说明 套用模板输出Excel 并对数据进行分页
/// 作 者 Lingyun_k
/// 创建日期
/// </summary>
public class ExcelHelper
protected string templetFile = null;
protected string outputFile = null;
protected object missing = Missing Value;
/**//// <summary>
/// 构造函数 需指定模板文件和输出文件完整路径
/// </summary>
/// <param name= templetFilePath >Excel模板文件路径</param>
/// <param name= outputFilePath >输出Excel文件路径</param>
public ExcelHelper(string templetFilePath string outputFilePath)
if(templetFilePath == null)
throw new Exception( Excel模板文件路径不能为空! );
if(outputFilePath == null)
throw new Exception( 输出Excel文件路径不能为空! );
if(!File Exists(templetFilePath))
throw new Exception( 指定路径的Excel模板文件不存在! );
this templetFile = templetFilePath;
this outputFile = outputFilePath;
/**//// <summary>
/// 将DataTable数据写入Excel文件(套用模板并分页)
/// </summary>
/// <param name= dt >DataTable</param>
/// <param name= rows >每个WorkSheet写入多少行数据</param>
/// <param name= top >行索引</param>
/// <param name= left >列索引</param>
/// <param name= sheetPrefixName >WorkSheet前缀名 比如 前缀名为 Sheet 那么WorkSheet名称依次为 Sheet Sheet </param>
public void DataTableToExcel(DataTable dt int rows int top int left string sheetPrefixName)
int rowCount = dt Rows Count; //源DataTable行数
int colCount = dt Columns Count; //源DataTable列数
int sheetCount = this GetSheetCount(rowCount rows); //WorkSheet个数
DateTime beforeTime;
DateTime afterTime;
if(sheetPrefixName == null || sheetPrefixName Trim() == )
sheetPrefixName = Sheet ;
//创建一个Application对象并使其可见
beforeTime = DateTime Now;
Excel Application app = new Excel ApplicationClass();
app Visible = true;
afterTime = DateTime Now;
//打开模板文件 得到WorkBook对象
Excel Workbook workBook = app Workbooks Open(templetFile missing missing missing missing missing
missing missing missing missing missing missing missing);
//得到WorkSheet对象
Excel Worksheet workSheet = (Excel Worksheet)workBook Sheets get_Item( );
//复制sheetCount 个WorkSheet对象
for(int i= ;i<sheetCount;i++)
((Excel Worksheet)workBook Worksheets get_Item(i)) Copy(missing workBook Worksheets[i]);
将源DataTable数据写入Excel#region 将源DataTable数据写入Excel
for(int i= ;i<=sheetCount;i++)
int startRow = (i ) * rows; //记录起始行索引
int endRow = i * rows; //记录结束行索引
//若是最后一个WorkSheet 那么记录结束行索引为源DataTable行数
if(i == sheetCount)
endRow = rowCount;
//获取要写入数据的WorkSheet对象 并重命名
Excel Worksheet sheet = (Excel Worksheet)workBook Worksheets get_Item(i);
sheet Name = sheetPrefixName + + i ToString();
//将dt中的数据写入WorkSheet
for(int j= ;j<endRow startRow;j++)
for(int k= ;k<colCount;k++)
sheet Cells[top + j left + k] = dt Rows[startRow + j][k] ToString();
//写文本框数据
Excel TextBox txtAuthor = (Excel TextBox)sheet TextBoxes( txtAuthor );
Excel TextBox txtDate = (Excel TextBox)sheet TextBoxes( txtDate );
Excel TextBox txtVersion = (Excel TextBox)sheet TextBoxes( txtVersion );
txtAuthor Text = KLY NET的Blog ;
txtDate Text = DateTime Now ToShortDateString();
txtVersion Text = ;
#endregion
//输出Excel文件并退出
try
workBook SaveAs(outputFile missing missing missing missing missing Excel XlSaveAsAccessMode xlExclusive missing missing missing missing);
workBook Close(null null null);
app Workbooks Close();
app Application Quit();
app Quit();
System Runtime InteropServices Marshal ReleaseComObject(workSheet);
System Runtime InteropServices Marshal ReleaseComObject(workBook);
System Runtime InteropServices Marshal ReleaseComObject(app);
workSheet=null;
workBook=null;
app=null;
GC Collect();
catch(Exception e)
throw e;
finally
Process[] myProcesses;
DateTime startTime;
myProcesses = Process GetProcessesByName( Excel );
//得不到Excel进程ID 暂时只能判断进程启动时间
foreach(Process myProcess in myProcesses)
startTime = myProcess StartTime;
if(startTime > beforeTime && startTime < afterTime)
myProcess Kill();
/**//// <summary>
/// 获取WorkSheet数量
/// </summary>
/// <param name= rowCount >记录总行数</param>
/// <param name= rows >每WorkSheet行数</param>
private int GetSheetCount(int rowCount int rows)
int n = rowCount % rows; //余数
if(n == )
return rowCount / rows;
else
return Convert ToInt (rowCount / rows) + ;
/**//// <summary>
/// 将二维数组数据写入Excel文件(套用模板并分页)
/// </summary>
/// <param name= arr >二维数组</param>
/// <param name= rows >每个WorkSheet写入多少行数据</param>
/// <param name= top >行索引</param>
/// <param name= left >列索引</param>
/// <param name= sheetPrefixName >WorkSheet前缀名 比如 前缀名为 Sheet 那么WorkSheet名称依次为 Sheet Sheet </param>
public void ArrayToExcel(string[ ] arr int rows int top int left string sheetPrefixName)
int rowCount = arr GetLength( ); //二维数组行数(一维长度)
int colCount = arr GetLength( ); //二维数据列数(二维长度)
int sheetCount = this GetSheetCount(rowCount rows); //WorkSheet个数
DateTime beforeTime;
DateTime afterTime;
if(sheetPrefixName == null || sheetPrefixName Trim() == )
sheetPrefixName = Sheet ;
//创建一个Application对象并使其可见
beforeTime = DateTime Now;
Excel Application app = new Excel ApplicationClass();
app Visible = true;
afterTime = DateTime Now;
//打开模板文件 得到WorkBook对象
Excel Workbook workBook = app Workbooks Open(templetFile missing missing missing missing missing
missing missing missing missing missing missing missing);
//得到WorkSheet对象
Excel Worksheet workSheet = (Excel Worksheet)workBook Sheets get_Item( );
//复制sheetCount 个WorkSheet对象
for(int i= ;i<sheetCount;i++)
((Excel Worksheet)workBook Worksheets get_Item(i)) Copy(missing workBook Worksheets[i]);
将二维数组数据写入Excel#region 将二维数组数据写入Excel
for(int i= ;i<=sheetCount;i++)
int startRow = (i ) * rows; //记录起始行索引
int endRow = i * rows; //记录结束行索引
//若是最后一个WorkSheet 那么记录结束行索引为源DataTable行数
if(i == sheetCount)
endRow = rowCount;
//获取要写入数据的WorkSheet对象 并重命名
Excel Worksheet sheet = (Excel Worksheet)workBook Worksheets get_Item(i);
sheet Name = sheetPrefixName + + i ToString();
//将二维数组中的数据写入WorkSheet
for(int j= ;j<endRow startRow;j++)
for(int k= ;k<colCount;k++)
sheet Cells[top + j left + k] = arr[startRow + j k];
Excel TextBox txtAuthor = (Excel TextBox)sheet TextBoxes( txtAuthor );
Excel TextBox txtDate = (Excel TextBox)sheet TextBoxes( txtDate );
Excel TextBox txtVersion = (Excel TextBox)sheet TextBoxes( txtVersion );
txtAuthor Text = KLY NET的Blog ;
txtDate Text = DateTime Now ToShortDateString();
txtVersion Text = ;
#endregion
//输出Excel文件并退出
try
workBook SaveAs(outputFile missing missing missing missing missing Excel XlSaveAsAccessMode xlExclusive missing missing missing missing);
workBook Close(null null null);
app Workbooks Close();
app Application Quit();
app Quit();
System Runtime InteropServices Marshal ReleaseComObject(workSheet);
System Runtime InteropServices Marshal ReleaseComObject(workBook);
System Runtime InteropServices Marshal ReleaseComObject(app);
workSheet=null;
workBook=null;
app=null;
GC Collect();
catch(Exception e)
throw e;
finally
Process[] myProcesses;
DateTime startTime;
myProcesses = Process GetProcessesByName( Excel );
//得不到Excel进程ID 暂时只能判断进程启动时间
foreach(Process myProcess in myProcesses)
startTime = myProcess StartTime;
if(startTime > beforeTime && startTime < afterTime)
myProcess Kill();
cha138/Article/program/net/201311/12695
相关参考
知识大全 在ASP.NET访问Excel文件 (VB and C#)
在ASP.NET访问Excel文件(VBandC#) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
Asp.Net用OWC操作Excel的实例代码 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!这篇文
ASP.NET项目开发指南:C#操作XML(2) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
知识大全 ASP.NET项目开发指南:C#操作XML(1)[2]
ASP.NET项目开发指南:C#操作XML(1)[2] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
知识大全 ASP.NET项目开发指南:C#操作XML(1)[1]
ASP.NET项目开发指南:C#操作XML(1)[1] 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧
ASP.NET模板控件开发浅析 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! ASPNET模板控
如何制作Asp.Net界面模板 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 一基础知识 C:
ASP.NET新特性之工程模板支持 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 一简介 在A
ASP.NET2.0中实现模板中的数据绑定 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 模板化
微软发布ASP.NETMVC设计模板库 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 微软最近公