可以使用这个类来实现此功能:
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Jeky.Web


{

/**//// <summary>
/// 输入文件的类型。
/// </summary>
public enum ExportType

{

/**//// <summary>
/// 格式:*.htm、*.html等。
/// </summary>
Html,

/**//// <summary>
/// 格式:*.doc。
/// </summary>
Word,

/**//// <summary>
/// 格式:*.xls。
/// </summary>
Excel,
} ;



/**//// <summary>
/// 有关文件的一些操作方法。
/// </summary>
public class FileHandle

{

/**//// <summary>
/// 初始化 <see cref="Jeky.Web.FileHandle">FileHandle</see> 类的新实例。
/// </summary>
public FileHandle()

{
}


/**//// <summary>
/// 将 Control 信息导出为某种特定类型的文件。
/// </summary>
/// <param name="ctl">Control,如 DataGrid 对象。</param>
/// <param name="strFileName">导出后的文件名称。</param>
/// <param name="et">导出类型。</param>
/// <example>将 Customers 表中的一组数据导出为 Word 文档
/// <code escaped="true">
/// void bt_click(object sender,EventArgs e){
/// SqlType ST=new SqlType();
/// Common CM=new Common();
/// // 创建一个 SqlConnection 对象
/// SqlConnection conn=ST.GetConn("server=localhost; database=Northwind; uid=sa; pwd=",false);
/// conn.Open();
/// string strSql="Select Top 10 CustomerID,CompanyName,ContactName,Address,City From Customers";
/// SqlCommand cmd=new SqlCommand(strSql,conn);
/// // 设置 DataGird 对象的数据源
/// myDataGrid.DataSource=cmd.ExecuteReader();
/// // 绑定信息
/// myDataGrid.DataBind();
/// conn.Close();
/// // 将 DataGrid 对象中的信息导出为指定格式的文件
/// CM.ToExportFile(myDataGrid,"Customers.doc",ExportType.Word);
/// }
/// </code>
/// </example>
/// <remarks>
/// 一、当对象(如DataGrid)有分页时,该方法会出现错误。
/// 二、当数据集中有不能认别的符号时,可能会出现乱码,没法处理。
/// </remarks>
public static void ToExportFile(Control ctl, string strFileName, ExportType et)

{
string[] ETArray = new string[]

{
"text/HTML", "application/msword", "application/ms-excel"
};
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName)); // 使用 UrlEncode 方法可以正确地输出中文名称。
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = Encoding.Default;
HttpContext.Current.Response.ContentType = ETArray[(int) et];
ctl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
}
}
另外,如果DataGrid中存在较长的数字值,例如“身份证”字段时,导出的Excel可能会以下格式出现:4.11303E+14,解决方法:
// 在 DataBound 事件中处理
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)


{
e.Item.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
namespace Jeky.Web

{
/**//// <summary>
/// 输入文件的类型。
/// </summary>
public enum ExportType
{
/**//// <summary>
/// 格式:*.htm、*.html等。
/// </summary>
Html,
/**//// <summary>
/// 格式:*.doc。
/// </summary>
Word,
/**//// <summary>
/// 格式:*.xls。
/// </summary>
Excel,
} ;


/**//// <summary>
/// 有关文件的一些操作方法。
/// </summary>
public class FileHandle
{
/**//// <summary>
/// 初始化 <see cref="Jeky.Web.FileHandle">FileHandle</see> 类的新实例。
/// </summary>
public FileHandle()
{
}

/**//// <summary>
/// 将 Control 信息导出为某种特定类型的文件。
/// </summary>
/// <param name="ctl">Control,如 DataGrid 对象。</param>
/// <param name="strFileName">导出后的文件名称。</param>
/// <param name="et">导出类型。</param>
/// <example>将 Customers 表中的一组数据导出为 Word 文档
/// <code escaped="true">
/// void bt_click(object sender,EventArgs e){
/// SqlType ST=new SqlType();
/// Common CM=new Common();
/// // 创建一个 SqlConnection 对象
/// SqlConnection conn=ST.GetConn("server=localhost; database=Northwind; uid=sa; pwd=",false);
/// conn.Open();
/// string strSql="Select Top 10 CustomerID,CompanyName,ContactName,Address,City From Customers";
/// SqlCommand cmd=new SqlCommand(strSql,conn);
/// // 设置 DataGird 对象的数据源
/// myDataGrid.DataSource=cmd.ExecuteReader();
/// // 绑定信息
/// myDataGrid.DataBind();
/// conn.Close();
/// // 将 DataGrid 对象中的信息导出为指定格式的文件
/// CM.ToExportFile(myDataGrid,"Customers.doc",ExportType.Word);
/// }
/// </code>
/// </example>
/// <remarks>
/// 一、当对象(如DataGrid)有分页时,该方法会出现错误。
/// 二、当数据集中有不能认别的符号时,可能会出现乱码,没法处理。
/// </remarks>
public static void ToExportFile(Control ctl, string strFileName, ExportType et)
{
string[] ETArray = new string[]
{
"text/HTML", "application/msword", "application/ms-excel"
};
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName)); // 使用 UrlEncode 方法可以正确地输出中文名称。
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = Encoding.Default;
HttpContext.Current.Response.ContentType = ETArray[(int) et];
ctl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
}
}另外,如果DataGrid中存在较长的数字值,例如“身份证”字段时,导出的Excel可能会以下格式出现:4.11303E+14,解决方法:
// 在 DataBound 事件中处理
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 

{
e.Item.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
} posted @ 2005-12-18 03:09 jeky 阅读(372) 评论(0) 编辑
主要是运用CSS实现的效果,代码如下:
rect ( number number number number )
依据上-右-下-左的顺序提供自对象左上角为(0,0)坐标计算的四个偏移数值,其中任一数值都可用 auto 替换,即此边不剪切;必须将 position 属性的值设为 absolute ,此属性方可使用。
年龄:<asp:TextBox ID="tbxAge" Runat="server" Style="width:100px; position:absolute; margin-top:-5px" />
<asp:DropDownList ID="ddlAge" Runat="server"
Style="width:118px; margin-top:-5px; margin-left:-5px; Clip:rect(auto auto auto 100px); position:absolute"
onchange="document.getElementById('tbxAge').value = this.options[this.selectedIndex].text"
>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlAge" Runat="server"
Style="width:118px; margin-top:-5px; margin-left:-5px; Clip:rect(auto auto auto 100px); position:absolute"
onchange="document.getElementById('tbxAge').value = this.options[this.selectedIndex].text"
>
<asp:ListItem>22</asp:ListItem>
<asp:ListItem>23</asp:ListItem>
</asp:DropDownList>
rect ( number number number number )
依据上-右-下-左的顺序提供自对象左上角为(0,0)坐标计算的四个偏移数值,其中任一数值都可用 auto 替换,即此边不剪切;必须将 position 属性的值设为 absolute ,此属性方可使用。
下拉菜单长度 = 文本框长度 + 18px
rect设置 :(auto auto auto 文本框长度)
嘿嘿,有时间自己也弄一个控件来。
参考文档:http://kwklover.cnblogs.com/archive/2005/11/23/282879.html
rect设置 :(auto auto auto 文本框长度)
嘿嘿,有时间自己也弄一个控件来。
参考文档:http://kwklover.cnblogs.com/archive/2005/11/23/282879.html
posted @ 2005-12-18 00:55 jeky 阅读(338) 评论(0) 编辑

