在.NET中excel导出方法汇总(收集)

http://search.csdn.net/Expert/topic/2346/2346423.xml?temp=.3901941
http://search.csdn.net/Expert/topic/2387/2387301.xml?temp=3.222293E-02
http://search.csdn.net/Expert/topic/2581/2581246.xml?temp=.9223444
http://search.csdn.net/Expert/topic/2414/2414749.xml?temp=5.735415E-02
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302096
1。
None.gifprotected void toExcel_Click(object sender,EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif
InBlock.gif         ToExcel(parentList); 
ExpandedBlockEnd.gif      }
 
None.gif
None.gif      
private void ToExcel(System.Web.UI.Control ctl) 
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif
InBlock.gif         Response.Charset
="GB2312"
InBlock.gif         Response.AppendHeader(
"Content-Disposition","attachment;filename=Excel.xls");  
InBlock.gif         Response.ContentEncoding 
= System.Text.Encoding.UTF8; 
InBlock.gif         Response.ContentType 
= "application/ms-excel"
InBlock.gif
InBlock.gif         ctl.Page.EnableViewState 
= false
InBlock.gif         System.IO.StringWriter tw 
= new System.IO.StringWriter(); 
InBlock.gif         System.Web.UI.HtmlTextWriter hw 
= new HtmlTextWriter(tw); 
InBlock.gif
InBlock.gif         ctl.RenderControl(hw); 
InBlock.gif
InBlock.gif         Response.Write(tw.ToString()); 
InBlock.gif         Response.End(); 
ExpandedBlockEnd.gif      }
2。导入excel写的太麻烦了,弹出窗口可以!用这个导入
ExpandedBlockStart.gifContractedBlock.gif  Protected Sub toExcel_Click()Sub toExcel_Click(ByVal sender As ObjectByVal e As ImageClickEventArgs)
InBlock.gif            ToExcel1(tblResults)
ExpandedBlockEnd.gif        
End Sub

None.gif
ExpandedBlockStart.gifContractedBlock.gif        
Sub ToExcel1()Sub ToExcel1(ByVal ctl As System.Web.UI.Control)
InBlock.gif            Response.Charset 
= "GB2312"
InBlock.gif            Response.AppendHeader(
"Content-Disposition""attachment;filename=totalreport.xls")
InBlock.gif            Response.ContentEncoding 
= System.Text.Encoding.UTF8
InBlock.gif            Response.ContentType 
= "application/ms-excel"
InBlock.gif
InBlock.gif            ctl.Page.EnableViewState 
= False
InBlock.gif            
Dim tw As System.IO.StringWriter = New System.IO.StringWriter()
InBlock.gif            
Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(tw)
InBlock.gif
InBlock.gif            ctl.RenderControl(hw)
InBlock.gif
InBlock.gif            Response.Write(tw.ToString())
InBlock.gif            Response.End()
ExpandedBlockEnd.gif        
End Sub
3.//将数据集导到excel 以 XLS 格式里
None.gifprivate void DataSetToExcelXls(DataSet ds) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{    
ContractedSubBlock.gifExpandedSubBlockStart.gif            
用例说明#region 用例说明
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//* 
InBlock.gif            【输入】 
InBlock.gif             sender:       
InBlock.gif             e:             
InBlock.gif            【输出】
InBlock.gif             无
InBlock.gif            【流程定义】
InBlock.gif             1、将生成的DataSet的数据导到指定的Execl文件里,并显示出来
InBlock.gif            【可选流程】
InBlock.gif                            
InBlock.gif            【问题】
InBlock.gif            
ExpandedSubBlockEnd.gif            
*/

ExpandedSubBlockEnd.gif            
#endregion

InBlock.gif            
InBlock.gif            Random rd
=new Random(int.Parse(DateTime.Now.ToString("MMddhhmmss")));
InBlock.gif            
string strFileName =DateTime.Now.ToString("yyyyMMdd")+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second+txtBeginTime.Text.Trim()+txtFinishTime.Text.Trim()+rd.Next(999999).ToString()+ ".XLS";
InBlock.gif            
InBlock.gif            
//Excel.Application excel= new Excel.ApplicationClass();//Excel.Application();
InBlock.gif
            Excel.Application excel ;
InBlock.gif            
//excel = new Excel.Application();
InBlock.gif
            excel = new Excel.ApplicationClass();
InBlock.gif            Excel.WorkbookClass oWB;
InBlock.gif            Excel.Worksheet oSheet;
InBlock.gif            oWB 
= (Excel.WorkbookClass)(excel.Workbooks.Add(true));
InBlock.gif            oSheet 
= (Excel.Worksheet)oWB.ActiveSheet;
InBlock.gif            
InBlock.gif            oSheet.Cells[
11=lblReportTime.Text;    //合并项
InBlock.gif
            oSheet.Cells[21= lblReportCount.Text;//合并项
InBlock.gif
            oSheet.Cells[41= "话机号码" ;//合并项
InBlock.gif
            oSheet.Cells[42= "月份" ;//合并项
InBlock.gif
            oSheet.Cells[33= "通话总次数(次)";
InBlock.gif            oSheet.Cells[
37= "通话总时长(分)";
InBlock.gif            oSheet.Cells[
3,11= "通话总金额(元)";
InBlock.gif            oSheet.Cells[
43= "国际";
InBlock.gif            oSheet.Cells[
44= "国内";
InBlock.gif            oSheet.Cells[
45= "港澳台";
InBlock.gif            oSheet.Cells[
46= "合计";
InBlock.gif            oSheet.Cells[
47= "国际";
InBlock.gif            oSheet.Cells[
48= "国内";
InBlock.gif            oSheet.Cells[
49= "港澳台";
InBlock.gif            oSheet.Cells[
4,10= "合计";
InBlock.gif            oSheet.Cells[
4,11= "国际";
InBlock.gif            oSheet.Cells[
4,12= "国内";
InBlock.gif            oSheet.Cells[
4,13= "港澳台";
InBlock.gif            oSheet.Cells[
4,14= "合计";
InBlock.gif
InBlock.gif            oSheet.get_Range(
"A1","N4").Font.Size= 9 ;
InBlock.gif            oSheet.get_Range(
"A1","N4").Font.Bold = true;
InBlock.gif            oSheet.get_Range(
"A1","N4").VerticalAlignment =  Excel.XlVAlign.xlVAlignCenter;
InBlock.gif            oSheet.get_Range(
"A1","N4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
InBlock.gif            
InBlock.gif            
//该范围内设置外面一层边框
InBlock.gif
            oSheet.get_Range("A1","N4").BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin ,Excel.XlColorIndex.xlColorIndexAutomatic,Color.Black.ToArgb()) ;
InBlock.gif
InBlock.gif            
//该范围内设置里面所有单元格的边框
InBlock.gif
            oSheet.get_Range("A1","N4").Borders.Weight = Excel.XlBorderWeight.xlThin ;
InBlock.gif            oSheet.get_Range(
"A1","N4").Borders.Color = Color.Black.ToArgb() ;
InBlock.gif            
//范围内单元格添加背景颜色
InBlock.gif
            oSheet.get_Range("A1","N2").Cells.Interior.Color =Color.FromArgb(153,255,204).ToArgb();
InBlock.gif            oSheet.get_Range(
"A1","N2").Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;
InBlock.gif        
InBlock.gif            oSheet.get_Range(
"A3","N4").Cells.Interior.Color = Color.LemonChiffon.ToArgb();
InBlock.gif            oSheet.get_Range(
"A3","N4").Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;
InBlock.gif            
InBlock.gif            DataTable dt 
=ds.Tables[0];
InBlock.gif            DataRow dr;
InBlock.gif            
int count=dt.Rows.Count+5;
InBlock.gif            
string strExcelLength = "N"+count.ToString().Trim();
InBlock.gif
InBlock.gif            
int I,J;
InBlock.gif            
for(I = 5;I< count;I++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                dr
=dt.Rows[I-5];
InBlock.gif                
InBlock.gif                
for(J = 1;J<(dt.Columns.Count +1) ;J++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    oSheet.Cells[I,J] 
= dr[J-1].ToString().Trim() ;
InBlock.gif                
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            oSheet.get_Range(
"A5",strExcelLength).Font.Size= 9 ;
InBlock.gif            oSheet.get_Range(
"A5",strExcelLength).Font.Bold = false;
InBlock.gif            oSheet.get_Range(
"A5",strExcelLength).VerticalAlignment =  Excel.XlVAlign.xlVAlignCenter;
InBlock.gif            oSheet.get_Range(
"A5",strExcelLength).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
InBlock.gif            
//该范围内设置外面一层边框
InBlock.gif
            oSheet.get_Range("A5",strExcelLength).BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin ,Excel.XlColorIndex.xlColorIndexAutomatic,Color.Black.ToArgb()) ;
InBlock.gif
InBlock.gif            
//该范围内设置里面所有单元格的边框
InBlock.gif
            oSheet.get_Range("A5",strExcelLength).Borders.Weight = Excel.XlBorderWeight.xlThin ;
InBlock.gif            oSheet.get_Range(
"A5",strExcelLength).Borders.Color = Color.Black.ToArgb() ;
InBlock.gif            
//范围内单元格添加背景颜色
InBlock.gif
            oSheet.get_Range("A5",strExcelLength).Cells.Interior.Color = Color.PaleTurquoise.ToArgb();
InBlock.gif            oSheet.get_Range(
"A5",strExcelLength).Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;
InBlock.gif
InBlock.gif            
InBlock.gif            oSheet.get_Range(
"A1","N1").Merge(true);
InBlock.gif            oSheet.get_Range(
"A2","N2").Merge(true);            
InBlock.gif
InBlock.gif            oSheet.get_Range(
"A3","A4").Merge(true);
InBlock.gif            oSheet.get_Range(
"B3","B4").Merge(true);
InBlock.gif            oSheet.get_Range(
"C3","F3").Merge(true);
InBlock.gif            oSheet.get_Range(
"G3","J3").Merge(true);
InBlock.gif            oSheet.get_Range(
"K3","N3").Merge(true);
InBlock.gif
InBlock.gif
InBlock.gif            
string path=Server.MapPath("."+ "\\..\\..\\excel-file\\" + strFileName;            
InBlock.gif            
InBlock.gif            oWB.SaveAs(path,Missing.Value,
"","",true,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,Missing.Value,Missing.Value);
InBlock.gif            
InBlock.gif            
//oWB.SaveCopyAs(path);                
InBlock.gif
            
InBlock.gif            oWB.Close(
false,null,null);
InBlock.gif            excel.Workbooks.Close();            
InBlock.gif            excel.Quit();
InBlock.gif
InBlock.gif            System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
InBlock.gif            System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
InBlock.gif            System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
InBlock.gif
InBlock.gif            oSheet 
= null;
InBlock.gif            oWB
= null;
InBlock.gif            excel 
= null;
InBlock.gif
InBlock.gif            GC.Collect();        
InBlock.gif                    
InBlock.gif            
string test="../../excel-file/" + strFileName;
InBlock.gif            Response.Redirect(test);        
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/RedStarLiu/archive/2006/03/06/343692.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/464454.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

进程之间的同步机制

多进程的系统中避免不了进程间的相互关系。本讲将介绍进程间的两种主要关系——同步与互斥&#xff0c;然后着重讲解解决进程同步的几种机制。 进程互斥是进程之间发生的一种间接性作用&#xff0c;一般是程序不希望的。通常的情况是两个或两个以上的进程需要同时访问某…

(二)AS给button添加点击事件

三种方法给Button添加点击事件 &#xff08;一&#xff09;通过button的id&#xff0c;添加继承View.OnClickListener的监听实现 <Buttonandroid:id"id/btn_button2"android:text"按钮2"android:layout_width"match_parent"android:layout_he…

MongoDB(4)--MongoDB服务的启动

原始方式 只有启动了MongoDB的服务&#xff0c;才能使用MongoDB的功能&#xff0c;通常情况下会开一个命令窗口&#xff0c;输入下面的命令来启动服务&#xff1a; 配置文件方式 如果不想每次启动的时候都在命令行中输入很多繁琐的参数&#xff0c;可以把参数信息保存在配置文件…

我张哥做的这ARM开发板,真酸爽!

本文导读&#xff1a;市场普及度最高的A7处理器&#xff0c;核心板所有设计资料、生产资料全部开放&#xff01;包含核心板原理图、PCB、BOM、uboot源码、Linux内核所有驱动源码、文件系统等&#xff0c;并提供测试与验证方案&#xff01;武汉万象奥科&#xff08;www.vanxoak.…

oracle rac维护命令,2015年oracle rac日常基本维护命令.doc

Oracle RAC 资料收集http://www.D数据库吧oracle rac日常基本维护命令2Oracle RAC性能调整12详解Oracle RAC入门和提高27ORACLE RAC数据库配置Dataguard环境49老白对于RAC应用调优的建议51oracle rac日常基本维护命令所有实例和服务的状态$ srvctl status database -d orclInst…

linux嵌入式开发流程,听听牛人怎么说

很多学习嵌入式的人来说&#xff0c;都会学习嵌入式linux开发&#xff0c;在学习的过程中&#xff0c;总会有很多难题&#xff0c;相对而言&#xff0c;嵌入式linux也算是嵌入式学习中比较难的&#xff0c;那如何可以攻破这个难点&#xff0c;那么我们就需要从根本入手&#xf…

搭建LNMP遇到的问题

配置PHP的执行./buildconf --force出现一下错误 buildconf: Your version of autoconf likely contains buggy cache code. Running vcsclean for you. To avoid this, install autoconf-2.13. 解决方案&#xff1a;安装autoconf-2.13.RPM包 执行export …

从文件中读取数据,排序之后输出到另一个文件中

文件中有一组数据&#xff0c;要求排序后输出到另一个文件中去 主要有两个知识点&#xff1a; 排序、文件操作 C/C代码如下&#xff1a; [cpp] view plaincopy #include<iostream> #include<fstream> #include<vector> using namespace std; void Or…

从单片机转到嵌入式Linux的跨度大吗?

这是我今天一个同学问我的我再零散的说一些观点&#xff0c;如果大家有这方面的经验&#xff0c;也帮忙在文章下留言&#xff0c;谢谢大家。先说共同点单片机和嵌入式他们最终都是要跑硬件的&#xff0c;所以你也会遇到像GPIO口、I2C、串口、SPI、定时器、看门狗这些问题。所以…

oracle如何取uuid,oracle如何取得uuId

是想生成GUID吗&#xff1f;SQL> select sys_guid() from dual;SYS_GUID()--------------------------------F18031C69D8345DEB305D4B2E796A282-------------------------------------------------java取得uuidpackage com.hdsoft.uuid;import java.util.UUID;public class …

有一种豁达叫开源

当人们在讨论开源的时候&#xff0c;第一时间想到的是索取&#xff0c;开源对很多开发者来说是好的事情&#xff0c;但是闭源对很多科技企业是有技术保护作用的。人们对软件的态度是经历过很多次变化的。在现代计算机研发初期&#xff0c;核心问题是硬件&#xff0c;寻找实现记…

JS应用DOM入门:DOM的对象属性

DOM提供了一套属性用于导航、访问和更新文档内容&#xff0c;其中包括只读类型的属性和可读写类型的属性。下表是只读类型的属性&#xff1a; DOM对象属性返 回 值FirstChild返回一个对象&#xff08;Object&#xff09;&#xff0c;表示第一个孩子节点&#xff08;child node&…

用O(1)的时间复杂度删除单链表中的某个节点

用O(1)的时间复杂度删除单链表中的某个节点 给定链表的头指针和一个结点指针&#xff0c;在O(1)时间删除该结点。链表结点的定义如下&#xff1a; struct ListNode {int m_nKey;ListNode* m_pNext; }; 函数的声明如下&#xff1a; void DeleteNode(ListNode* pListHea…

Django之序列化

关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户&#xff0c;特别的Ajax请求一般返回的为Json格式。 1、serializers from django.core import serializersret models.BookType.objects.all()data serializers.serialize("json", ret)2、json…

linux c语言内核函数,2014-1-5_linux内核学习(1)_C语言基础

1、结构体的初始化static struct file_operations fops {.read device_read,.write device_write,.open device_open,.release device_release};以前学习C语言的时候没有见过struct的这种初始化方式。这其实是C语言的新标准。Struct一共有三种初始化的方式&#xff1a;int…

我的备忘录

http://jnesta.blogdriver.com/jnesta/index.html 转载于:https://www.cnblogs.com/oisiv/archive/2006/04/06/368663.html

怎么选工作?

选择offer&#xff0c;一直是很困难的事&#xff0c;工作不是餐桌上的美食&#xff0c;你品尝了这个菜还可以去尝那一道菜&#xff0c;所以大家都害怕因为选错一方而失去了更好的机会。而那句「选择大于努力」&#xff0c;让很多人更看重选择。我会经常遇到同学向我咨询offer选…

Selenium断言的使用,等待

自动化测试常用断言的使用方法&#xff08;python&#xff09; 自动化测试中寻找元素并进行操作&#xff0c;如果在元素好找的情况下&#xff0c;相信大家都可以较熟练地编写用例脚本了&#xff0c;但光进行操作可能还不够&#xff0c;有时候也需要对预期结果进行判断。 这里介…

oracle 表复制操作

如下&#xff0c;表a是数据库中以前存在的表&#xff0c;b是预备依据表a执行复制树立的表&#xff1a; 1、只复制表结构的sql create table b as select * from a where 1<>1 2、即复制表结构又复制表中数据的sql create table b as select * from a 3、复制表的制定字段…

linux系统运行iso,linux可以加载iso镜像文件到启动项吗

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼楼主的需求描述不清&#xff0c;不作评述。只说下关于楼上一些吧友提到的grub loopback设备可mount iso并读取其中文件的功能未读过grub1/2的源码&#xff0c;不太了解其实现原理&#xff0c;但从应用上来说&#xff0c;这个设备不…