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

None.gif//1、由dataset生成
None.gif

None.gif
public void CreateExcel(DataSet ds,string typeid,string FileName) 
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   HttpResponse resp;
InBlock.gif   resp 
= Page.Response;
InBlock.gif   resp.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
InBlock.gif   resp.AppendHeader(
"Content-Disposition", "attachment;filename=" + FileName);   
InBlock.gif   
string colHeaders= "", ls_item="";
InBlock.gif   
int i=0;
InBlock.gif
InBlock.gif   
//定义表对象与行对像,同时用DataSet对其值进行初始化
InBlock.gif
   DataTable dt=ds.Tables[0];
InBlock.gif   DataRow[] myRow
=dt.Select(""); 
InBlock.gif   
// typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
InBlock.gif
   if(typeid=="1")
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
InBlock.gif
    for(i=0;i     colHeaders+=dt.Columns[i].Caption.ToString()+"\t";
InBlock.gif    colHeaders 
+=dt.Columns[i].Caption.ToString() +"\n";   
InBlock.gif    
//向HTTP输出流中写入取得的数据信息
InBlock.gif
    resp.Write(colHeaders); 
InBlock.gif    
//逐行处理数据  
InBlock.gif
    foreach(DataRow row in myRow)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
InBlock.gif
     for(i=0;i      ls_item +=row[i].ToString() + "\t";     
InBlock.gif     ls_item 
+= row[i].ToString() +"\n";
InBlock.gif     
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据    
InBlock.gif
     resp.Write(ls_item);
InBlock.gif     ls_item
="";
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
if(typeid=="2")
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{ 
InBlock.gif     
//从DataSet中直接导出XML数据并且写到HTTP输出流中
InBlock.gif
     resp.Write(ds.GetXml());
ExpandedSubBlockEnd.gif    }
    
ExpandedSubBlockEnd.gif   }

InBlock.gif   
//写缓冲区中的数据到HTTP头文件中
InBlock.gif
   resp.End();
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif  }

None.gif
None.gif 
None.gif
None.gif
//2、使用微软的C++写的ACTIVEX控件:http://download.microsoft.com/download/OfficeXPDev/sample/1.0/WIN98MeXP/EN-US/Dsoframerctl.exe
None.gif
//3、由datagrid生成:
None.gif

None.gif
public void ToExcel(System.Web.UI.Control ctl)  
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{
InBlock.gif   HttpContext.Current.Response.AppendHeader(
"Content-Disposition","attachment;filename=Excel.xls");
InBlock.gif   HttpContext.Current.Response.Charset 
="UTF-8";    
InBlock.gif   HttpContext.Current.Response.ContentEncoding 
=System.Text.Encoding.Default;
InBlock.gif   HttpContext.Current.Response.ContentType 
="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
InBlock.gif
   ctl.Page.EnableViewState =false;   
InBlock.gif   System.IO.StringWriter  tw 
= new System.IO.StringWriter() ;
InBlock.gif   System.Web.UI.HtmlTextWriter hw 
= new System.Web.UI.HtmlTextWriter (tw);
InBlock.gif   ctl.RenderControl(hw);
InBlock.gif   HttpContext.Current.Response.Write(tw.ToString());
InBlock.gif   HttpContext.Current.Response.End();
ExpandedBlockEnd.gif  }

None.gif
None.gif用法:ToExcel(datagrid1);
None.gif
None.gif
None.gif
//4、这个用dataview ,代码好长
None.gif

None.gif
public void OutputExcel(DataView dv,string str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
//
InBlock.gif   
// TODO: 在此处添加构造函数逻辑
InBlock.gif   
//
InBlock.gif                           
//dv为要输出到Excel的数据,str为标题名称
InBlock.gif
   GC.Collect();
InBlock.gif   Application excel;
// = new Application();
InBlock.gif
   int rowIndex=4;
InBlock.gif   
int colIndex=1;
InBlock.gif
InBlock.gif   _Workbook xBk;
InBlock.gif   _Worksheet xSt;
InBlock.gif
InBlock.gif   excel
= new ApplicationClass();
InBlock.gif  
InBlock.gif   xBk 
= excel.Workbooks.Add(true);
InBlock.gif   
InBlock.gif   xSt 
= (_Worksheet)xBk.ActiveSheet;
InBlock.gif
InBlock.gif   
//
InBlock.gif   
//取得标题
InBlock.gif   
//
InBlock.gif
   foreach(DataColumn col in dv.Table.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    colIndex
++;
InBlock.gif    excel.Cells[
4,colIndex] = col.ColumnName;
InBlock.gif    xSt.get_Range(excel.Cells[
4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
ExpandedSubBlockEnd.gif
   }

InBlock.gif
InBlock.gif   
//
InBlock.gif   
//取得表格中的数据
InBlock.gif   
//
InBlock.gif
   foreach(DataRowView row in dv)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    rowIndex 
++;
InBlock.gif    colIndex 
= 1;
InBlock.gif    
foreach(DataColumn col in dv.Table.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     colIndex 
++;
InBlock.gif     
if(col.DataType == System.Type.GetType("System.DateTime"))
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      excel.Cells[rowIndex,colIndex] 
= (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
InBlock.gif      xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment 
= XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
ExpandedSubBlockEnd.gif
     }

InBlock.gif     
else
InBlock.gif      
if(col.DataType == System.Type.GetType("System.String"))
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      excel.Cells[rowIndex,colIndex] 
= "'"+row[col.ColumnName].ToString();
InBlock.gif      xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment 
= XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
ExpandedSubBlockEnd.gif
     }

InBlock.gif     
else
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      excel.Cells[rowIndex,colIndex] 
= row[col.ColumnName].ToString();
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
//
InBlock.gif   
//加载一个合计行
InBlock.gif   
//
InBlock.gif
   int rowSum = rowIndex + 1;
InBlock.gif   
int colSum = 2;
InBlock.gif   excel.Cells[rowSum,
2] = "合计";
InBlock.gif   xSt.get_Range(excel.Cells[rowSum,
2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
InBlock.gif   
//
InBlock.gif   
//设置选中的部分的颜色
InBlock.gif   
//
InBlock.gif
   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
InBlock.gif   xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex 
= 19;//设置为浅黄色,共计有56种
InBlock.gif   
//
InBlock.gif   
//取得整个报表的标题
InBlock.gif   
//
InBlock.gif
   excel.Cells[2,2] = str;
InBlock.gif   
//
InBlock.gif   
//设置整个报表的标题格式
InBlock.gif   
//
InBlock.gif
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
InBlock.gif   xSt.get_Range(excel.Cells[
2,2],excel.Cells[2,2]).Font.Size = 22;
InBlock.gif   
//
InBlock.gif   
//设置报表表格为最适应宽度
InBlock.gif   
//
InBlock.gif
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
InBlock.gif   xSt.get_Range(excel.Cells[
4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
InBlock.gif   
//
InBlock.gif   
//设置整个报表的标题为跨列居中
InBlock.gif   
//
InBlock.gif
   xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
InBlock.gif   xSt.get_Range(excel.Cells[
2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
InBlock.gif   
//
InBlock.gif   
//绘制边框
InBlock.gif   
//
InBlock.gif
   xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
InBlock.gif   xSt.get_Range(excel.Cells[
4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
InBlock.gif
   xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
InBlock.gif
   xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
InBlock.gif
   xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
InBlock.gif   
//
InBlock.gif   
//显示效果
InBlock.gif   
//
InBlock.gif
   excel.Visible=true;
InBlock.gif
InBlock.gif   
//xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
InBlock.gif
   xBk.SaveCopyAs(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls");
InBlock.gif
InBlock.gif   ds 
= null;
InBlock.gif            xBk.Close(
false, null,null);
InBlock.gif   
InBlock.gif            excel.Quit();
InBlock.gif            System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
InBlock.gif            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
InBlock.gif    System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
InBlock.gif            xBk 
= null;
InBlock.gif            excel 
= null;
InBlock.gif   xSt 
= null;
InBlock.gif            GC.Collect();
InBlock.gif   
string path = Server.MapPath(this.xlfile.Text+".xls");
InBlock.gif
InBlock.gif   System.IO.FileInfo file 
= new System.IO.FileInfo(path);
InBlock.gif   Response.Clear();
InBlock.gif   Response.Charset
="GB2312";
InBlock.gif   Response.ContentEncoding
=System.Text.Encoding.UTF8;
InBlock.gif   
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
InBlock.gif
   Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
InBlock.gif   
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
InBlock.gif
   Response.AddHeader("Content-Length", file.Length.ToString());
InBlock.gif   
InBlock.gif   
// 指定返回的是一个不能被客户端读取的流,必须被下载
InBlock.gif
   Response.ContentType = "application/ms-excel";
InBlock.gif   
InBlock.gif   
// 把文件流发送到客户端
InBlock.gif
   Response.WriteFile(file.FullName);
InBlock.gif   
// 停止页面的执行
InBlock.gif
  
InBlock.gif   Response.End();
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/acelove/archive/2005/02/21/106886.html

asp.net 教你如何导出PDF报表 asp.net 教你如何导出PDF报表asp.net 教你如何导出PDF报表asp.net 教你如何导出PDF报表asp.net 教你如何导出PDF报表asp.net 教你如何导出PDF报表asp.net 教你如何导出PDF报表asp.net 教你如何导出PDF报表 立即下载

相关推荐

Dataset导出到Excel

将Dataset导出到Excel,本程序阐述基本功能-Dataset exported to Excel, the program described the basic functions of

Asp.net 导出Excel

<br />Asp.net导出到Excel 支持的各种类型的格式:<br />1) 文本:vnd.ms-excel.numberformat:@<br />2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd<br />3) 数字:vnd.ms-excel.numberformat:#,##0.00<br />4) 货币:vnd.ms-excel.numberformat:¥#,##0.00<br />5) 百分比:vnd.ms-excel.numberformat: #0.

Amy的随笔 333

ASP.NET用DataSet导出到Excel的方法

ASP.NET用DataSet导出到Excel的方法,需要的朋友可以参考一下

C#导出excel表格

HttpResponse resp= HttpContext.Current.Response; //设置内容编码 resp.ContentEncoding= System.Text.Encoding.GetEncoding("GB2312"); //设置标题编码 resp.HeaderEncoding= System.Text.Encoding.GetEncoding("GB2312")

wangqiuchen_Study的专栏 580

Hi3516CV610 YOLOv8模型转换与部署实战:从源码到板端推理全流程解析

本文详细解析了将YOLOv8模型部署到Hi3516CV610开发板的全流程。内容涵盖从Ubuntu环境搭建、模型获取与验证,到关键的PyTorch模型转换为ONNX格式,并利用海思工具链量化生成板端可执行的.wk文件。最后,指导开发者编写板端C程序,完成视频流处理、NNIE推理及结果后处理,并提供了部署调试与性能优化的实用技巧。

weixin_29215815的博客 273

使用ACOLITE对OLCI影像进行大气校正+批处理

使用ACOLITE对OLCI影像进行大气校正+批处理

mrzhy1的博客 7369

ASP.NET基础之HttpContext学习

上一个随笔主要针对HttpModeul[ASP.NET基础之HttpModule学习]内容进行一个简单的学习;本章内容我将对ASP.NET另一个主要的知识点HttpContext进行学习;首先我们简单了解一些理论的知识,然后结合一些实例演示我们开发时经常要用到的一些功能,其中有些部分是从网上整理所得,希望对您的阅读有所帮助; 一:HttpContext理论知识: 1:HttpContext类它...

weixin_34179968的博客 461

全覆盖路径规划开源项目Clean-robot-turtlebot3原理及流程概括总结

本系列文章主要介绍全覆盖路径规划开源项目Clean-robot-turtlebot3的相关内容,包含如下四篇文章,分别介绍了开源项目Clean-robot-turtlebot3流程的概括总结、ROS坐标系常用坐标及其变换、Clean-robot-turtlebot3关键函数解析等内容。

慕羽★的博客 6114

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

asp.net里导出excel表方法汇总 1、由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) { HttpResponse resp; resp = Page.Response; resp.ContentEncoding = System.Text.Encod...

weixin_30652271的博客 136

AFL源码分析之afl-as.c详细注释

前言 afl-as注释好了,明白了是如何进行插桩的,以及插在哪里 本文主要参考Sakura大佬文章:https://eternalsakura13.com/2020/08/23/afl/ 编写不易,如果能够帮助到你,希望能够点赞收藏加关注哦Thanks♪(・ω・)ノ /* Copyright 2013 Google LLC All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); y

hollk’s blog 2176

利用requests库下载bilibili字幕文件

文章目录1. 前言2. 程序下载大法好2.1 挖掘思路2.2 核心知识点2.3 汇总代码 1. 前言   之前的博客介绍了使用工具下载B站视频,但美中不足的是无法下载字幕。为了提高效率,将下载字幕的工作用程序自动化处理。 2. 程序下载大法好 2.1 挖掘思路   在谷歌浏览器中按下F12,打开谷歌开发者工具。搜索subtitle,则会得到一个json文件,如下图所示:   在网页中空白位置点击查看网页源代码,然后通过搜索(Ctrl+f)查找subtitle,则会发现在subtitle_url中包含了对应

herosunly的博客 4万+

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

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...

weixin_39852121的博客 479

华为OD机试 - 整型数组按照个位数排序(Python/JS/C/C++ 双机位C卷 100分)

本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。给定一个非空数组(列表),其元素数据类型为整型,请按照数组元素十进制最低位从小到大进行排序,十进制最低位相同的元素,相对位置保持不变。当数组元素为负值时,十进制最低位等同于去除符号位后对应十进制值最低位。给定一个非空数组,其元素数据类型为32位有

学Java,找哪吒 402
上一篇: Gmail ! 邀请多多!(有好几万个喔)
下一篇: 关注laszlo
weixin_30318645
博客等级 码龄11年 110粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值