最近在做一个ASP.NET项目,需要生成一些Excel文件。我查了MSDN和网上的资料,总结了一下,大致有这样一些方法:
1)服务器端自动化(Server-Side Automation):MSDN中强烈建议不要采用这种技术!但我看到很多例子都是这样用的。到底实际的效果如何?
2)Office Web Components:没有找到它们的具体说明文档。粗粗看了一下,好像主要支持显示、编辑、计算等这些有用户界面的工作,不知道能不能隐藏在页面中,将数据输出到Excel文件?数据又是如何获得的?
3)Document Streaming:在客户端调用Excel,再从服务器端接收数据。接收的方法又有XML Web Service和MIME两种。XML Web Service又可以用Soap Toolkit或Microsoft Office XP Web Services Toolkit。我对此不太熟悉,但感觉XML Web Service只能发送数据,而我的Excel文件不是单纯的数据,还有一些显示的格式;另一种方法,MIME,由于也是在服务器端发送,好像也不能处理客户端的Excel格式的问题。
4)客户端脚本:用客户端脚本启动Excel,并直接从脚本中查询数据库,再将数据写入到excel中。我现在要将一些数据输出到Excel文件中,数据是显示在DataGrid中,但需要分页,而且Excel文件还有一些格式的要求。我比较了这几种方法,方法4)技术上可行,但很不理想。其它的方法,好像都不太适合。项目的时间很紧,也不允许我一一试过去。我刚接触ASP.NET,很多东西不甚了解,很想听听各位大侠的意见。另外,我也想乘此机会总结一下ASP.NET中使用Office的各种方法,也请大家谈谈自己的经验,谢谢!
1)服务器端自动化(Server-Side Automation):MSDN中强烈建议不要采用这种技术!但我看到很多例子都是这样用的。到底实际的效果如何?
2)Office Web Components:没有找到它们的具体说明文档。粗粗看了一下,好像主要支持显示、编辑、计算等这些有用户界面的工作,不知道能不能隐藏在页面中,将数据输出到Excel文件?数据又是如何获得的?
3)Document Streaming:在客户端调用Excel,再从服务器端接收数据。接收的方法又有XML Web Service和MIME两种。XML Web Service又可以用Soap Toolkit或Microsoft Office XP Web Services Toolkit。我对此不太熟悉,但感觉XML Web Service只能发送数据,而我的Excel文件不是单纯的数据,还有一些显示的格式;另一种方法,MIME,由于也是在服务器端发送,好像也不能处理客户端的Excel格式的问题。
4)客户端脚本:用客户端脚本启动Excel,并直接从脚本中查询数据库,再将数据写入到excel中。我现在要将一些数据输出到Excel文件中,数据是显示在DataGrid中,但需要分页,而且Excel文件还有一些格式的要求。我比较了这几种方法,方法4)技术上可行,但很不理想。其它的方法,好像都不太适合。项目的时间很紧,也不允许我一一试过去。我刚接触ASP.NET,很多东西不甚了解,很想听听各位大侠的意见。另外,我也想乘此机会总结一下ASP.NET中使用Office的各种方法,也请大家谈谈自己的经验,谢谢!
偶也新手,都是抄别人的:(
http://dotnet.aspx.cc/Exam/OutPutExcel.aspx
在我们把DataGrid上的数据导入到Excel的时候,如果遇到比较长的数字字符串,比如身份证号码,就会在Excel里当成数字看待,并转换成科学计数法的格式,造成数据的丢失,下面这个方法就解决了这个问题,并示例如何进行其它的格式化。查看例子OutPutExcel.aspx<%@ Page language="c#" Codebehind="OutPutExcel.aspx.cs"
AutoEventWireup="false" Inherits="eMeng.Exam.OutPutExcel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
<asp:Button id="Button1" runat="server" Text="输出到Excel"></asp:Button>
</form>
</body>
</HTML>OutPutExcel.aspx.csusing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace eMeng.Exam
{
/// <summary>
/// OutPutExcel 的摘要说明。
/// </summary>
public class OutPutExcel : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataGrid1.DataSource=CreateDataSource();
DataGrid1.DataBind();
}
/// <summary>
/// 创建数据源
/// </summary>
/// <returns>DataView</returns>
ICollection CreateDataSource()
{DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("身份证号码", typeof(string)));
dt.Columns.Add(new DataColumn("图书单价",typeof(decimal)));
dt.Columns.Add(new DataColumn("购买数量",typeof(Int32)));
dt.Columns.Add(new DataColumn("总价格",typeof(decimal)));
for (int i = 0; i < 30; i++)
{
dr = dt.NewRow();dr[0] = "123456789123456789";
dr[1] = 100 * i /3.0;
dr[2] = i + 5;
dr[3] = (decimal)dr[1] * (Int32)dr[2];
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
/// <summary>
/// 输出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);}
#endregionprivate void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");
e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");
}
}
}
}
前几天一同事问我如何利用C#将数据导到Excel文件当中,当时比较忙没有
顾得上去研究,今天特地研究了一下,基本搞定,下面就具体介绍如何将
DataView中的数据按照一定格式存到Excel文件当中。
正文:
一、首先要引用一个Excel的组件,我一开始是在Office XP下尝试的,不
成功,后来把XP给干掉,装2k,就成功了,所以这里分享的是Office 2k下
引用相关组件来实现功能的,在工程中引用COM标签中的Microsoft
Excel 9.0 Object Library,添加成功后,引用中会多出三个引用项:
Excel、Office、VBIDE。 二、具体代码。
using System;
using System.Data;
using Excel;
using System.IO;
namespace Test.ExcelCom
{
/// <summary>
/// 将DataView中的数据导入Excel文件中
/// 作者:Rexsp
/// 创建:2004-4-4
/// </summary>
public class OutputExcel
{
#region 私有成员
/// <summary>
/// 数据的DataView
/// </summary>
private DataView dv=null;
/// <summary>
/// 表格标题
/// </summary>
private string title=null;
/// <summary>
/// 输出文件路径
/// </summary>
private string outFilePath=null;
/// <summary>
/// 输入文件名
/// </summary>
private string inputFilePath=null;
#endregion
#region 公共属性
/// <summary>
/// 数据的DataView
/// </summary>
public DataView DV
{
set{dv=value;}
}
/// <summary>
/// 表格标题
/// </summary>
public string Title
{
set{title=value;}
get{return title;}
}
/// <summary>
/// 输出文件路径
/// </summary>
public string OutFilePath
{
set{outFilePath=value;}
get{return outFilePath;}
}
/// <summary>
/// 输入文件路径
/// </summary>
public string InputFilePath
{
set{inputFilePath=value;}
get{return inputFilePath;}
}
#endregion #region 构造函数
public OutputExcel()
{
}
public OutputExcel(DataView dv,string title)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion
#region 公共方法
public void CreateExcel()
{
int rowIndex=4;//行起始坐标
int colIndex=1;//列起始坐标
ApplicationClass myApp=null;
Workbook myBook=null;
Worksheet mySheet=null;
//如果文件不存在,则将模板文件拷贝一份作为输出文件
//这里如果通过File.Create来创建文件是不行的,因为xls
//的空文件也有固定的格式,跟文本不一样的,也许有其它
//通过程序直接生成excel的方法,大家可以尝试尝试的
if(!File.Exists(outFilePath))
{
File.Copy(inputFilePath,outFilePath,true);
}
myApp= new ApplicationClass();
myApp.Visible=false;
object oMissiong=System.Reflection.Missing.Value;
myApp.Workbooks.Open(outFilePath,oMissiong,oMissiong,oMissiong,oMissiong,
oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
myBook=myApp.Workbooks[1];
mySheet=(Worksheet)myBook.ActiveSheet; //
//取得标题
//
foreach(DataColumn col in dv.Table.Columns)
{
colIndex++;
mySheet.Cells[4,colIndex] = col.ColumnName;
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
//设置标题格式为居中对齐
}
//
//取得表格中的数据
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
mySheet.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
mySheet.Cells[rowSum,2] = "合计";
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//设置选中的部分的颜色
//
mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
mySheet.Cells[2,2] = title;
//
//设置整个报表的标题格式
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Bold = true;
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).Select();
mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
myBook.Save();;
myBook.Close( true,outFilePath,true);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);
GC.Collect(); }
#endregion
} }
一点说明:操作Excel的时候,可能会发生Excel进程被锁定,无法退
出,解决方法是在保存完并关闭myBook(工作簿)后,别关闭Excel进
程(//myApp.Quit();)。这样的结果是服务器上始终有一个Excel的
进程。可能会出现asp_net用户操作Excel的权限不够,配置Dcom。运
行Dcomcnfg.exe,找到Excel应用程序,配置其属性,身份验证级别
选"无",身份标识选"交互式用户",安全性页面,启动和访问均给
everyone。注意:查看当前进程中是否有Winword进程存在,如果有且
不能被结束,那么重启动计算机。再次运行你的代码即OK。这样以后
就不会出现权限不够的情况了。
三、调用
#region 测试Excel
QuickItemCollection qic =new QuickItemCollection();
qic.GetAllInfo();
DataView dv= new DataView();
DataTable dt = new DataTable("Excel");
dt.Columns.Add("ID",System.Type.GetType("System.String"));
dt.Columns.Add("ItemName",System.Type.GetType("System.String"));
int qicCount=qic.Count;
for(int i=0;i<qicCount;i++)
{
DataRow dr= dt.NewRow();
dr[0] = qic[i].ID;
dr[1] = qic[i].ItemName;
dt.Rows.Add(dr);
}
OutputExcel ope = new OutputExcel();
ope.DV=dt.DefaultView;
ope.Title="测试生成Excel";
ope.InputFilePath=Server.MapPath("Sample.xls");
ope.OutFilePath=Server.MapPath("Test.xls");
ope.CreateExcel();
#endregion
一点说明:这段代码的前半部分读过我那篇《一种快速存取订阅条目的方
案》的读者应该认得的,其实也就是一个把集合类中数据填充到
DataView中的过程,后面的就是调用。Sample.xls是个新建的空的
Sample.xls,然后执行完毕后,就会生成Test.xls文档
2、用:public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}用法:ToExcel(datagrid1);
前一个项目,也是生成excel,我就到网上查,基本都是服务器端自动化
本来也打算用这个,但被老大制止,原因是微软不提倡这种做法(具体原因忘了,好像是交互性有问题),所以没用,后来尝试了owc由于开发周期比较紧,技术上有点棘手,所以也没用这个方法(主要是版本兼容问题)
客户端生成要降低安全属性也没用
最后和客户商量,采用了vba,先生成csv文件,然后给用户download,再调用vba读取
有两个问题我不太清楚:1)如果DataGrid采用分页的方式,this.DataGrid1.RenderControl(oHtmlTextWriter); 是不是只会将当前页的数据进行输出?如果可以全部输出的话,那实际的数据在往返过程中保存在何处(我在 Button1_Click()中没有看到数据重新绑定的代码)?因为分页的话,视图状态只会保存当前页的内容。2)如何控制Excel文件的格式?包括表格标题、栏标题、文字对齐方式、栏宽、行高等。To morality(逆水行舟,勇进):你的方法功能上没有问题,但采用的是第一种方法(服务器端自动化)。按照MSDN说法,尽量不要采用这种方式。到底实际使用的情况如何?整个系统的负荷怎样?又如何处理并发的问题?To spland(spland):等于说,你们最后只是通过Web进行文件的发布。文件的生成和查看都是采用其它的程序?另外,有谁用过 XML 的方式,也请谈谈你们的经验。谢谢!
{
//写入Excel的方法:
//定义需要参数。
string SourceFile="Data.XLS"; //源文件名称。
string TemplatePath=Server.MapPath("ExcelTemplate"); //存放源文件的文件夹路径。
string DownloadPath=Server.MapPath("ExcelDownload"); //副本的文件夹路径。
//副本的文件名。
string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";
object missing = System.Reflection.Missing.Value;
Excel.Application myExcel=new Excel.Application();
//打开新文件
myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing);
Excel.Workbook myBook=myExcel.Workbooks[1];
Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];
string DownloadFilePath=DownloadPath+"\\"+TempFileName;
int i=0;
while (i<=10)
{
myExcel.Cells[4+i,2]=i.ToString();
myExcel.Cells[4+i,3]=i.ToString();
myExcel.Cells[4+i,4]=i.ToString();
myExcel.Cells[4+i,5]=i.ToString();
myExcel.Cells[4+i,6]=i.ToString();
i++;
}
myBook.Saved=true;
//myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);
myBook.PrintPreview(0);
//myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
myBook.Close(false, null,null);
myExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
myBook = null;
myExcel = null;
GC.Collect();
//Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
}