(转)数据导出

看见一段DG数据导出的代码,还没来得及Test,先COPY过来.
<%@   Page   language="c#"   Codebehind="LastReportManage.aspx.cs"   AutoEventWireup="false"   Inherits="ScoreOnLine.LastReportManage"   %>   
  
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN"   >   
  
<HTML>   
      
<HEAD>   
  
<title>LastReportManage</title>   
  
<meta   name="GENERATOR"   Content="Microsoft   Visual   Studio   .NET   7.1">   
  
<meta   name="CODE_LANGUAGE"   Content="C#">   
  
<meta   name="vs_defaultClientScript"   content="JavaScript">   
  
<meta   name="vs_targetSchema"   content="http://schemas.microsoft.com/intellisense/ie5">   
  
<link   href="CSS/style.css"   rel="stylesheet"   type="text/css">   
      
</HEAD>   
  
<body   MS_POSITIONING="GridLayout">   
  
<form   id="Form1"   method="post"   runat="server">   
  
<TABLE   id="Table1"   style="Z-INDEX:   101;   LEFT:   8px;   WIDTH:   760px;   POSITION:   absolute;   TOP:   8px;   HEIGHT:   533px"   
  cellSpacing
="1"   cellPadding="1"   width="760"   border="0">   
  
<TR>   
  
<TD   noWrap   style="HEIGHT:   22px">   
  
<asp:Label   id="Label1"   runat="server">当前位置:未按时报到新生管理</asp:Label></TD>   
  
</TR>   
  
<TR>   
  
<TD   style="HEIGHT:   59px"   noWrap   align="center">   
  
<TABLE   id="Table2"   style="WIDTH:   736px;   HEIGHT:   51px"   cellSpacing="1"   cellPadding="1"   width="736"   
  border
="0">   
  
<TR>   
  
<TD   noWrap   style="HEIGHT:   10px">   
  
<asp:Label   id="Label6"   runat="server">所在学院</asp:Label></TD>   
  
<TD   noWrap   style="WIDTH:   121px;   HEIGHT:   10px">   
  
<asp:DropDownList   id="DropdownlistCollege"   runat="server"   Width="136px"   AutoPostBack="True"></asp:DropDownList></TD>   
  
<TD   noWrap   style="HEIGHT:   10px">   
  
<asp:Label   id="Label2"   runat="server">所在系别</asp:Label></TD>   
  
<TD   noWrap   style="WIDTH:   142px;   HEIGHT:   10px">   
  
<asp:DropDownList   id="DropDownListDepartment"   runat="server"   Width="120px"   AutoPostBack="True"></asp:DropDownList></TD>   
  
<TD   noWrap   style="HEIGHT:   10px">   
  
<asp:Label   id="Label3"   runat="server">所在专业</asp:Label></TD>   
  
<TD   style="WIDTH:   125px;   HEIGHT:   10px"   noWrap>   
  
<asp:DropDownList   id="DropDownListMajor"   runat="server"   Width="160px"   AutoPostBack="True"></asp:DropDownList></TD>   
  
<TD   noWrap   style="HEIGHT:   10px">   
  
<asp:Label   id="Label7"   runat="server">班级</asp:Label></TD>   
  
<TD   noWrap   style="HEIGHT:   10px"><FONT   face="宋体">   
  
<asp:DropDownList   id="DropDownListClass"   runat="server"   AutoPostBack="True"></asp:DropDownList></FONT></TD>   
  
</TR>   
  
<TR>   
  
<TD   noWrap>   
  
<asp:Label   id="Label4"   runat="server">身份证号</asp:Label></TD>   
  
<TD   noWrap   style="WIDTH:   121px">   
  
<asp:TextBox   id="TextBoxStudentIDcard"   runat="server"></asp:TextBox></TD>   
  
<TD   noWrap>   
  
<asp:Label   id="Label5"   runat="server"   Width="40px">姓名</asp:Label></TD>   
  
<TD   noWrap   style="WIDTH:   142px">   
  
<asp:TextBox   id="TextBoxStudentName"   runat="server"   Width="164px"></asp:TextBox></TD>   
  
<TD   noWrap   colSpan="2"   style="WIDTH:   178px">   
  
<asp:Button   id="ButtonQuery"   runat="server"   Width="72px"   Text="查   询"></asp:Button></TD>   
  
</TR>   
  
</TABLE>   
  
</TD>   
  
</TR>   
  
<TR>   
  
<TD   style="HEIGHT:   149px"   noWrap   align="center">   
  
<asp:DataGrid   id="DataGrid1"   runat="server"   Width="759px"   AutoGenerateColumns="False"   Height="139px"   
  AllowPaging
="True"   PageSize="15">   
  
<Columns>   
  
<asp:BoundColumn   DataField="studentname"   HeaderText="姓名"></asp:BoundColumn>   
  
<asp:BoundColumn   DataField="studentsex"   HeaderText="性别"></asp:BoundColumn>   
  
<asp:BoundColumn   DataField="studentno"   HeaderText="学号"></asp:BoundColumn>   
  
<asp:BoundColumn   DataField="collegename"   HeaderText="学院"></asp:BoundColumn>   
  
<asp:BoundColumn   DataField="departmentname"   HeaderText="系别"></asp:BoundColumn>   
  
<asp:BoundColumn   DataField="majorname"   HeaderText="专业"></asp:BoundColumn>   
  
<asp:BoundColumn   DataField="classname"   HeaderText="班级"></asp:BoundColumn>   
  
<asp:BoundColumn   DataField="StudentidNo"   HeaderText="身份证号"></asp:BoundColumn>   
  
<asp:BoundColumn   Visible="False"   DataField="reportid"   HeaderText="reportid"></asp:BoundColumn>   
  
<asp:HyperLinkColumn   Text="未按时报到记录"   DataNavigateUrlField="reportid"   DataNavigateUrlFormatString="LastReportNote.aspx?reportid={0}"   HeaderText="操作"></asp:HyperLinkColumn>   
  
</Columns>   
    
  
<PagerStyle   PageButtonCount="5"   Mode="NumericPages">   
  
</PagerStyle>   
  
</asp:DataGrid>   
  
</TD>   
  
</TR>   
  
<TR>   
  
<TD   noWrap   align="center">   
  
<asp:Button   id="ButtonOutput"   Width="88px"   Text="导出文档"   Runat="server"></asp:Button>&nbsp;   
  
<asp:DropDownList   id="DropDownListDocType"   runat="server">   
  
<asp:ListItem   Value="Word">导出为Word文档</asp:ListItem>   
  
<asp:ListItem   Value="Excel">导出为Excel文档</asp:ListItem>   
  
</asp:DropDownList></TD>   
  
</TR>   
  
</TABLE>   
  
</form>   
  
</body>   
  
</HTML>   

private   void   DataGrid1_ItemDataBound(object   sender,   System.Web.UI.WebControls.DataGridItemEventArgs   e)   
  
{   
  
if(e.Item.ItemIndex>-1)   
  
{   
  
if(e.Item.Cells[2].Text=="1")   
  
{   
  e.Item.Cells[
2].Text="";   
  }
   
  
if(e.Item.Cells[2].Text=="2")   
  
{   
  e.Item.Cells[
2].Text="";   
  }
   
  }
   
  System.Collections.ArrayList   myarraylist
=new   ArrayList();   
  myarraylist.Add(
1);   
  myarraylist.Add(
2);   
  myarraylist.Add(
3);   
  myarraylist.Add(
4);   
  myarraylist.Add(
5);   
  myarraylist.Add(
6);   
  myarraylist.Add(
7);   
  myarraylist.Add(
8);   
  output.ItemBound(sender,e,myarraylist);   
  }
   
  
private   void   ButtonOutput_Click(object   sender,   System.EventArgs   e)   
  
{   
  output.Save(DataGrid1,DropDownListDocType);   
  }
导出类代码:
 using   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   ScoreOnLine.DataAccess   
  
{   
  
///   <summary>   
  
///   GridSaveToFile   的摘要说明。   
  
///   </summary>   

  public   class   GridSaveToFile:System.Web.UI.Page {   
    
  
public     void   Save(System.Web.UI.WebControls.DataGrid   DataGrid1,System.Web.UI.WebControls.DropDownList   ddl)   
  
{   
  System.Web.HttpContext.Current.Response.Clear();     
  System.Web.HttpContext.Current.Response.Buffer
=   true;     
  System.Web.HttpContext.Current.Response.Charset
="GB2312";           
    
  System.Web.HttpContext.Current.Response.AppendHeader(
"Content-Disposition","attachment;filename="+getFileName(ddl.SelectedValue)+"");     
  System.Web.HttpContext.Current.Response.ContentEncoding
=System.Text.Encoding.GetEncoding("utf-8");   
  System.Web.HttpContext.Current.Response.ContentType   
=   "application/"+getContentType(ddl.SelectedValue)+"";//设置输出文件类型为excel文件。     
  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);   
  DataGrid1.RenderControl(oHtmlTextWriter);     
  System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());   
  System.Web.HttpContext.Current.Response.End();   
  }
   
  
private   string   getContentType(string   filetype)     
  
{   
  
switch   (filetype)   
  
{   
  
case   "Excel":   
  
return   "ms-excel";   
  
case   "Word":   
  
return   "ms-word";   
  
case   "Text":   
  
return   "text/plain";   
  
case   "Xml":   
  
return   "text/xml";   
  
case   "Html":   
  
return   "text/html";   
  
default:   
  
return   "ms-excel";   
    
  }
     
    
  }
   
  
private   string   getFileName(string   filetype)     
  
{   
  
string   FileName="ScoreOnLine"+System.DateTime.Now.ToString().Replace("-","").Replace("   ","").Replace(":","");   
  
switch   (filetype)   
  
{   
  
case   "Excel":   
  
return   FileName+".xls";   
  
case   "Word":   
  
return   FileName+".doc";   
  
case   "Text":   
  
return   FileName   +".txt";   
  
case   "Xml":   
  
return     FileName+".xml";   
  
case   "Html":   
  
return   FileName+".html";   
  
default:   
  
return   FileName+".xls";   
    
  }
   
  }
   
  
///   <summary>   
  
///     
  
///   </summary>   
  
///   <param   name="sender"></param>   
  
///   <param   name="e"></param>   
  
///   <param   name="al">ArrayList数组,传入将要控制的列号</param>   

  public     void   ItemBound(object   sender,   System.Web.UI.WebControls.DataGridItemEventArgs   e,System.Collections.ArrayList   al)   
  
{   
  
if(e.Item.ItemType   ==   ListItemType.Item   ||   e.Item.ItemType   ==   ListItemType.AlternatingItem)     
  
{   
  
for(int   i=0;i<al.Count;i++)   
  
{   
  e.Item.Cells[i].Attributes.Add(
"style","vnd.ms-excel.numberformat:@");   
  
//e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");   
  }
   
    
  }
   
  }
   
  }
   
  }
   
posted on 2007-12-28 15:37  冷月孤峰  阅读(213)  评论(0)    收藏  举报