CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

如何把数据导入到Excel文档中

楼主yunwxy2()2004-09-01 08:59:54 在 .NET技术 / ASP.NET 提问

如果发例子   希望能发测试通过的例子 问题点数:100、回复次数:6Top

1 楼net_lover(【孟子E章】)回复于 2004-09-01 09:03:34 得分 5

http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50Top

2 楼lxcc()回复于 2004-09-01 09:04:26 得分 5

http://www.alongsoft.com/tech/tech03062801.htmTop

3 楼Eddie005(♂) №.零零伍 (♂)回复于 2004-09-01 09:06:18 得分 20

///   <summary>  
  ///   将DataTable中的数据导出到指定的Excel文件中  
  ///   </summary>  
  ///   <param   name="page">Web页面对象</param>  
  ///   <param   name="tab">包含被导出数据的DataTable对象</param>  
  ///   <param   name="FileName">Excel文件的名称</param>  
  public   static   void   Export(System.Web.UI.Page   page,System.Data.DataTable   tab,string   FileName)  
  {  
  System.Web.HttpResponse   httpResponse   =   page.Response;  
  System.Web.UI.WebControls.DataGrid   dataGrid=new   System.Web.UI.WebControls.DataGrid();  
  dataGrid.DataSource=tab.DefaultView;  
  dataGrid.AllowPaging   =   false;  
  dataGrid.HeaderStyle.BackColor   =   System.Drawing.Color.Green;  
  dataGrid.HeaderStyle.HorizontalAlign   =   HorizontalAlign.Center;  
  dataGrid.HeaderStyle.Font.Bold   =   true;  
  dataGrid.DataBind();  
  httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8));   //filename="*.xls";  
  httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");  
  httpResponse.ContentType   ="application/ms-excel";  
  System.IO.StringWriter     tw   =   new   System.IO.StringWriter()   ;  
  System.Web.UI.HtmlTextWriter   hw   =   new   System.Web.UI.HtmlTextWriter   (tw);  
  dataGrid.RenderControl(hw);  
   
  string   filePath   =   page.Server.MapPath("..")+"\\Files\\"   +FileName;  
  System.IO.StreamWriter   sw   =   System.IO.File.CreateText(filePath);  
  sw.Write(tw.ToString());  
  sw.Close();  
   
  DownFile(httpResponse,FileName,filePath);  
   
  httpResponse.End();  
  }  
   
  private   static   bool   DownFile(System.Web.HttpResponse   Response,string   fileName,string   fullPath)  
  {  
  try  
  {  
  Response.ContentType   =   "application/octet-stream";  
   
  Response.AppendHeader("Content-Disposition","attachment;filename="   +    
  HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)   +   ";charset=GB2312");  
  System.IO.FileStream   fs=   System.IO.File.OpenRead(fullPath);  
  long   fLen=fs.Length;  
  int   size=102400;//每100K同时下载数据    
  byte[]   readData   =   new   byte[size];//指定缓冲区的大小    
  if(size>fLen)size=Convert.ToInt32(fLen);  
  long   fPos=0;  
  bool   isEnd=false;  
  while   (!isEnd)    
  {    
  if((fPos+size)>fLen)  
  {  
  size=Convert.ToInt32(fLen-fPos);  
  readData   =   new   byte[size];  
  isEnd=true;  
  }  
  fs.Read(readData,   0,   size);//读入一个压缩块    
  Response.BinaryWrite(readData);  
  fPos+=size;  
  }    
  fs.Close();    
  System.IO.File.Delete(fullPath);  
  return   true;  
  }  
  catch  
  {  
  return   false;  
  }  
  }Top

4 楼onlytiancai(谁染枫林醉)回复于 2004-09-01 09:12:15 得分 0

发现孟大哥的例子都是vb.net的呀Top

5 楼shoutor(www.mouxiao.com)回复于 2004-09-01 09:16:35 得分 5

http://blog.csdn.net/shoutor/archive/2004/07/01/31522.aspxTop

6 楼goody9807(http://goody9807.cnblogs.com)回复于 2004-09-01 13:55:00 得分 65

你可以dataset   导入   excel参考  
  http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912  
  http://www.dev-club.com/club/bbs/showEssence.asp?id=26350  
   
  http://dev.csdn.net/Develop/article/18/18623.shtm  
  http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861  
  http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50  
   
   
  http://expert.csdn.net/Expert/TopicView1.asp?id=2928057  
   
  www.foxhis.com/powermjtest/  
  原文代码:  
   
   
   
   
  private   void   Button1_Click(object   sender,   System.EventArgs   e)  
  {  
      //写入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); //下载文件  
  }  
  Top

相关问题

  • vb.net中把datagrid的数据导入到excel文档时,怎样结束excel进程
  • Excel中数据导入Oracle
  • 导入EXCEL进数据库!
  • datagrid数据导入excel
  • Excel导入sql数据库
  • 利用jsp页面将excel文档导入数据库时碰到的时间问题,求教!
  • 如何将WORD文档中的数据导入数据库?
  • 怎么将固定格式的word文档导入数据库????
  • 如何把word文档导入数据库
  • [求助]怎样将文档导入ORACLE数据库?

关键词

  • .net
  • excel
  • datagrid
  • 数据
  • 文件
  • application
  • missing
  • httpresponse
  • myexcel
  • headerstyle

得分解答快速导航

  • 帖主:yunwxy2
  • net_lover
  • lxcc
  • Eddie005
  • shoutor
  • goody9807

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo