asp数据导出 我想实现数据导出功能,根据页面显示的数据,当然这些数据都是从数据库里查出来的,根据所选择的记录导出数据,在页面添加一个导出按钮或者是链接,请各位高手指教,最好能写出一个大概的代码。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 根据所选择的记录导出数据导出EXCEL 还是?点击按钮获取你选择的数据组成一个DataTable 导出/// <summary> /// 将DataTable数据导出到EXCEL,调用该方法后自动返回可下载的文件流 /// </summary> /// <param name="dtData">要导出的数据源</param> public static void DataTable1Excel(System.Data.DataTable dtData) { System.Web.UI.WebControls.GridView gvExport = null; // 当前对话 System.Web.HttpContext curContext = System.Web.HttpContext.Current; // IO用于导出并返回excel文件 System.IO.StringWriter strWriter = null; System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null) { // 设置编码和附件格式 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); curContext.Response.Charset = "utf-8"; // 导出excel文件 strWriter = new System.IO.StringWriter(); htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); // 为了解决gvData中可能进行了分页的情况,需要重新定义一个无分页的GridView gvExport = new System.Web.UI.WebControls.GridView(); gvExport.DataSource = dtData.DefaultView; gvExport.AllowPaging = false; gvExport.DataBind(); // 返回客户端 gvExport.RenderControl(htmlWriter); curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />" + strWriter.ToString()); curContext.Response.End(); } } /// <summary> /// 直接输出Excel /// </summary> /// <param name="dtData"></param> public static void DataTable2Excel(System.Data.DataTable dtData) { System.Web.UI.WebControls.DataGrid dgExport = null; // 当前对话 System.Web.HttpContext curContext = System.Web.HttpContext.Current; // IO用于导出并返回excel文件 System.IO.StringWriter strWriter = null; System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null) { // 设置编码和附件格式 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding =System.Text.Encoding.UTF8; curContext.Response.Charset = ""; // 导出excel文件 strWriter = new System.IO.StringWriter(); htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid dgExport = new System.Web.UI.WebControls.DataGrid(); dgExport.DataSource = dtData.DefaultView; dgExport.AllowPaging = false; dgExport.DataBind(); // 返回客户端 dgExport.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); } } /// <summary> /// dtData是要导出为Excel的DataTable,FileName是要导出的Excel文件名(不加.xls) /// </summary> /// <param name="dtData"></param> /// <param name="FileName"></param> private void DataTable3Excel(System.Data.DataTable dtData, String FileName) { System.Web.UI.WebControls.GridView dgExport = null; //当前对话 System.Web.HttpContext curContext = System.Web.HttpContext.Current; //IO用于导出并返回excel文件 System.IO.StringWriter strWriter = null; System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null) { //设置编码和附件格式 //System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码 curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls"); curContext.Response.ContentType = "application nd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.UTF8; curContext.Response.Charset = "GB2312"; //导出Excel文件 strWriter = new System.IO.StringWriter(); htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); //为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView dgExport = new System.Web.UI.WebControls.GridView(); dgExport.DataSource = dtData.DefaultView; dgExport.AllowPaging = false; dgExport.DataBind(); //下载到客户端 dgExport.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); } }导出方法也可以自己去网上搜 很多 你点击按钮的时候将你写那个查询数据库的方法,绑定在Page_Load()事件里面 ,要判断if(!IsPostBack){ 绑定数据}就这样 显示在相应的asp控件上面 [Quote=引用 1 楼 wxr0323 的回复:]根据所选择的记录导出数据导出EXCEL 还是?点击按钮获取你选择的数据组成一个DataTable 导出我的每条记录都有checkbox,根据所选择的数据导出成Excel文件,请子夜详细说明以下,我是新手,这些都不懂,谢谢!辛苦了! 推荐你C#下一个很好用的Excel导出工具Myxls,用这个,服务器端都不用安装excel和一些权限设置.public static void ExportToTable(System.Data.DataTable dtData, string filename) { XlsDocument xls = new XlsDocument(); Worksheet sheet = xls.Workbook.Worksheets.AddNamed(filename);//状态栏标题名称 int rowNum = dtData.Rows.Count; int columnNum = dtData.Columns.Count; int rowIndex = 1; int columnIndex = 0; //生成格式列 ColumnInfo colInfo = new ColumnInfo(xls, sheet); colInfo.ColumnIndexStart = 0; colInfo.ColumnIndexEnd = Convert.ToUInt16(columnNum); colInfo.Width = 20 * 256; sheet.AddColumnInfo(colInfo); //绑定标题 Cell cell = null; foreach (DataColumn dc in dtData.Columns) { columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dc.ColumnName); //cell.HorizontalAlignment = HorizontalAlignments.Centered; //cell.Font.Weight = FontWeight.Bold; //cell.PatternColor = Colors.Default30; //cell.RightLineColor = Colors.Black; //cell.RightLineStyle = 1; //cell.BottomLineColor = Colors.Black; //cell.BottomLineStyle = 1; //cell.TopLineColor = Colors.Black; //cell.TopLineStyle = 1; cell.HorizontalAlignment = HorizontalAlignments.Centered; cell.VerticalAlignment = VerticalAlignments.Centered; cell.Pattern = 1; cell.PatternColor = Colors.EgaBlue; cell.UseBorder = true; cell.TopLineStyle = 1; cell.TopLineColor = Colors.Black; cell.BottomLineStyle = 1; cell.BottomLineColor = Colors.Black; cell.LeftLineStyle = 1; cell.LeftLineColor = Colors.Black; cell.RightLineStyle = 1; cell.RightLineColor = Colors.Black; cell.Font.Bold = true; cell.Font.Height = 11 * 20; cell.Font.ColorIndex = 1; } //绑定子项数据 for (int i = 0; i < rowNum; i++) { rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++) { columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dtData.Rows[i][j].ToString()); cell.HorizontalAlignment = HorizontalAlignments.Centered; cell.TextWrapRight = true; cell.VerticalAlignment = VerticalAlignments.Top; if (j == 0) { cell.LeftLineColor = Colors.Black; cell.LeftLineStyle = 1; } cell.RightLineColor = Colors.Black; cell.RightLineStyle = 1; cell.BottomLineColor = Colors.Black; cell.BottomLineStyle = 1; } } //导出 xls.FileName = filename; xls.Send(); } 求Asp.Net方面的书籍下载连接 ASP.NET实现推技术聊天室 哪里有问题?急....... 怎么做C#统计图 fckeditor 在asp.net(2.0)中使用的问题! 一个奇怪的问题,请高手指教 郁闷了 无法以sqlconnection接口操作SQL数据库的问题 没装IIS能不能把图片放在网站中? 在前台页面中能否用Jscript获取页面中WEB服务器控件的值?在线等…… google maps api3 画矩形的问题 怎么样使table不换行显示
/// 将DataTable数据导出到EXCEL,调用该方法后自动返回可下载的文件流
/// </summary>
/// <param name="dtData">要导出的数据源</param>
public static void DataTable1Excel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.GridView gvExport = null;
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null)
{
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
curContext.Response.Charset = "utf-8"; // 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 为了解决gvData中可能进行了分页的情况,需要重新定义一个无分页的GridView
gvExport = new System.Web.UI.WebControls.GridView();
gvExport.DataSource = dtData.DefaultView;
gvExport.AllowPaging = false;
gvExport.DataBind(); // 返回客户端
gvExport.RenderControl(htmlWriter);
curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />" + strWriter.ToString());
curContext.Response.End();
}
}
/// <summary>
/// 直接输出Excel
/// </summary>
/// <param name="dtData"></param>
public static void DataTable2Excel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.DataGrid dgExport = null;
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null)
{
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;
curContext.Response.Charset = "";
// 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); // 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind(); // 返回客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
} /// <summary>
/// dtData是要导出为Excel的DataTable,FileName是要导出的Excel文件名(不加.xls)
/// </summary>
/// <param name="dtData"></param>
/// <param name="FileName"></param>
private void DataTable3Excel(System.Data.DataTable dtData, String FileName)
{
System.Web.UI.WebControls.GridView dgExport = null;
//当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
//IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null; if (dtData != null)
{
//设置编码和附件格式
//System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application nd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = "GB2312"; //导出Excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); //为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView
dgExport = new System.Web.UI.WebControls.GridView();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind(); //下载到客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}导出方法也可以自己去网上搜 很多
if(!IsPostBack){
绑定数据
}
就这样 显示在相应的asp控件上面
根据所选择的记录导出数据导出EXCEL 还是?点击按钮获取你选择的数据组成一个DataTable 导出
我的每条记录都有checkbox,根据所选择的数据导出成Excel文件,请子夜详细说明以下,我是新手,这些都不懂,谢谢!辛苦了!
用这个,服务器端都不用安装excel和一些权限设置.public static void ExportToTable(System.Data.DataTable dtData, string filename)
{
XlsDocument xls = new XlsDocument();
Worksheet sheet = xls.Workbook.Worksheets.AddNamed(filename);//状态栏标题名称 int rowNum = dtData.Rows.Count; int columnNum = dtData.Columns.Count; int rowIndex = 1; int columnIndex = 0; //生成格式列
ColumnInfo colInfo = new ColumnInfo(xls, sheet);
colInfo.ColumnIndexStart = 0;
colInfo.ColumnIndexEnd = Convert.ToUInt16(columnNum);
colInfo.Width = 20 * 256; sheet.AddColumnInfo(colInfo); //绑定标题
Cell cell = null;
foreach (DataColumn dc in dtData.Columns)
{
columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dc.ColumnName);
//cell.HorizontalAlignment = HorizontalAlignments.Centered;
//cell.Font.Weight = FontWeight.Bold;
//cell.PatternColor = Colors.Default30;
//cell.RightLineColor = Colors.Black;
//cell.RightLineStyle = 1;
//cell.BottomLineColor = Colors.Black;
//cell.BottomLineStyle = 1;
//cell.TopLineColor = Colors.Black;
//cell.TopLineStyle = 1; cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.VerticalAlignment = VerticalAlignments.Centered;
cell.Pattern = 1;
cell.PatternColor = Colors.EgaBlue;
cell.UseBorder = true;
cell.TopLineStyle = 1;
cell.TopLineColor = Colors.Black;
cell.BottomLineStyle = 1;
cell.BottomLineColor = Colors.Black;
cell.LeftLineStyle = 1;
cell.LeftLineColor = Colors.Black;
cell.RightLineStyle = 1;
cell.RightLineColor = Colors.Black;
cell.Font.Bold = true;
cell.Font.Height = 11 * 20;
cell.Font.ColorIndex = 1; } //绑定子项数据
for (int i = 0; i < rowNum; i++)
{ rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++)
{ columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dtData.Rows[i][j].ToString());
cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.TextWrapRight = true;
cell.VerticalAlignment = VerticalAlignments.Top;
if (j == 0)
{
cell.LeftLineColor = Colors.Black;
cell.LeftLineStyle = 1;
} cell.RightLineColor = Colors.Black;
cell.RightLineStyle = 1;
cell.BottomLineColor = Colors.Black;
cell.BottomLineStyle = 1; } } //导出
xls.FileName = filename;
xls.Send();
}