asp.net里导出excel表方法汇总

2013 年 7 月 2 日4370

asp.net里导出excel表方法汇总 作者 roc 日期 2006-2-24 10:26:00

1、由dataset生成

public void CreateExcel(DataSet ds,string typeid,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders= "", ls_item="";
int i=0;

//定义表对象与行对像,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select("");
// typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
if(typeid=="1")
{
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+"\t";
colHeaders +=dt.Columns[i].Caption.ToString() +"\n";
//向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//逐行处理数据
foreach(DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for(i=0;i ls_item +=row[i].ToString() + "\t";
ls_item += row[i].ToString() +"\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item="";
}
}
else
{
if(typeid=="2")
{
//从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();


}
2、由datagrid生成

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);

3、这个用dataview

public void OutputExcel(DataView dv,string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
GC.Collect();
Application excel;// = new Application();
int rowIndex=4;
int colIndex=1;

_Workbook xBk;
_Worksheet xSt;

excel= new ApplicationClass();

xBk = excel.Workbooks.Add(true);

xSt = (_Worksheet)xBk.ActiveSheet;

//
//取得标题
//
foreach(DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4,colIndex],excel.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"))
{
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
excel.Cells[rowSum,2] = "合计";
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//设置选中的部分的颜色
//
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
excel.Cells[2,2] = str;
//
//设置整个报表的标题格式
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
//
//显示效果
//
excel.Visible=true;

//xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
xBk.SaveCopyAs(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls");

ds = null;
xBk.Close(false, null,null);

excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath(this.xlfile.Text+".xls");

System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行

Response.End();
}

| | 引用通告 | 编辑

上一篇:操作工具按钮办法下一篇:导出excel的另外一种方法

Re:asp.net里导出excel表方法汇总

新快报讯九寨沟 (记者 邢冉冉 通讯员 吴春燕海螺沟  陈贤君 李洁 曾彩连 赵磊)随着新生入学、四姑娘山 学生返校客流的增多,白云机场旅客量明显增加。今明两天将出现返校学生的最高峰,稻城旅客会突破16万人。 与此同时, 白云机场31日四川旅游 前及下个月15日前的机票峨眉山 价格均有所下调。 都江堰 从2001年开始,九寨沟旅游我着了魔似的接连去了4次西藏 峨眉山旅游,而每次西藏之行的四川旅行社兴奋点似乎都出现在阿里四姑娘山旅游——这处“世界屋脊上的屋脊”。都江堰旅游 那辆6缸切诺基就像一头忠实的藏獒,陪我走过了通向阿里的所有路线,成都旅行社 分别是拉萨与阿里之间的南线、小北线、大北线以及新疆与阿里之间的新藏线。 其中小北线或许无法称之为一条完整线路,它的一部分与大北线重合,另一部分又与南线重合。   新藏线:四川青旅 青城山旅游 张家界旅游连续爆胎·班公错鱼宴   从叶城出发,通常需要3天时间才能走完这条天路,西藏旅游 不过我们云南旅游 只用了2天,或许也能算是“阿里速度“的一个纪录了。   在这条路上什么都有可能发生,我们所遇到的最可怕情况,稻城亚丁 是一个小时之内连续不断地爆胎,以至于在用尽3条备胎之后,川藏线旅游只能留下几个人守车过夜,其他人搭车去百十来公里之外补胎。   在这条路上,米亚罗 连搭车都要碰运气,因为泸沽湖每天所能碰到的车基本上是在个位以内。成都旅游不过抛车荒野既有殚精竭虑的惶恐,也有独享月光的欣慰,这应该就乐山大佛 是冒险的乐趣所在。   班公错是整红原条新藏线上惟一川藏线 一处让人能感受到生机的地方,不仅是因为湖水中的裂腹鱼和湖中心的那个鸟岛,更是因为班公错的蓝,一片蓝得让人不禁要亲近的湖若尔盖 水。   这种亲近感同时还 成都租车来自那食得人间烟火的味觉体验,在湖边的管理站里,4个人只要掏上200元,就会有六七种做法的鱼端上饭桌:红烧的、麻辣的、清蒸的、酸菜的、豆豉的,还有鱼丸、鱼汤,肉质滑嫩,鲜美至极。更有趣的是,每到此时海鸥们也会寻味而来,如同一群任性的孩子,在我们这些贪吃的食客头顶盘旋飞舞,于是将吃不了的鱼拿来喂海鸥也成了件非常令人兴奋的事。这湖光山色中的鱼宴还有着另一番的特殊含义,它标志着我们已经离阿里的行政首府——狮泉河不远了。   资讯补给站   路线扫描:   全程1050公里,大部分是颠簸土路。从海拔1000多米的新疆叶城,翻越新疆和西藏交界处的海拔5356米的界山大坂,再到海拔4200多米的狮泉河,途中有200多公里的路段持续保持在海拔5000米以上,最高处达海拔5400米,接近某些雪山的顶峰高度。每个歇脚点之间的距离都很长,是4条进藏公路中难度最大的一条。   沿途主要站点:   叶城:新藏线自此起步,这里有美味的食品,舒适的驻地,足够为这次挑战准备充足的食品和体力。   三十里营房:这里有很大的兵站、不少餐厅、2家私人加油站和数处修车点,餐厅都能提供住宿,是新藏线上一个不错的

个人主页 | | | 删除 | 回复

Re:asp.net里导出excel表方法汇总

请问Application 在哪个命名空间中?

w_sabre@163.com

个人主页 | | | 删除 | 回复

发表评论:

0 0