请问在asp.net中怎样把分页中的数据全部导出到excel中。我现在只能实现把分页中的某一页数据导出到excel中,而不能全部把数据导出,请高手相助?绑定页面展示的分页代码:
private void BindListShow()
{
AspNetPager1.PageSize = 10;//初始化分页中每页显示的信息条数
AspNetPager1.RecordCount = Convert.ToInt32(BLL.MileageReportManage.getMileageCount(DropDown_year.SelectedValue.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "").ToString());
DataList1.DataSource= mileageReportManage.getMileageReport(DropDown_year.SelectedValue.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "").Tables[0].DefaultView;
DataList1.DataBind();
}
导出excel的代码:
protected void ibtnDR_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();//清空缓存
Response.Buffer = true;//启用缓存
Response.Charset = "GB2312";//设置编码格式
Response.AppendHeader("Content-Disposition","attachment;filename=filename.xls");//设置输出文件格式
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-Excel";//设置输出文件类型为Excel文件
this.EnableViewState = false;
System.Globalization.CultureInfo mycitrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter swriter = new System.IO.StringWriter(mycitrad);
System.Web.UI.HtmlTextWriter htmltextw = new System.Web.UI.HtmlTextWriter(swriter);
this.DataList1.RenderControl(htmltextw);//读取DataList1中的数据
Response.Write(swriter.ToString());//输出到stringwriter
Response.End();
}标注:我用的是DataList控件展示的数据
private void BindListShow()
{
AspNetPager1.PageSize = 10;//初始化分页中每页显示的信息条数
AspNetPager1.RecordCount = Convert.ToInt32(BLL.MileageReportManage.getMileageCount(DropDown_year.SelectedValue.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "").ToString());
DataList1.DataSource= mileageReportManage.getMileageReport(DropDown_year.SelectedValue.ToString(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "").Tables[0].DefaultView;
DataList1.DataBind();
}
导出excel的代码:
protected void ibtnDR_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();//清空缓存
Response.Buffer = true;//启用缓存
Response.Charset = "GB2312";//设置编码格式
Response.AppendHeader("Content-Disposition","attachment;filename=filename.xls");//设置输出文件格式
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-Excel";//设置输出文件类型为Excel文件
this.EnableViewState = false;
System.Globalization.CultureInfo mycitrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter swriter = new System.IO.StringWriter(mycitrad);
System.Web.UI.HtmlTextWriter htmltextw = new System.Web.UI.HtmlTextWriter(swriter);
this.DataList1.RenderControl(htmltextw);//读取DataList1中的数据
Response.Write(swriter.ToString());//输出到stringwriter
Response.End();
}标注:我用的是DataList控件展示的数据
解决方案 »
- DropDownlist 下拉内容显示问题
- 大哥大姐帮我进来看下我的ajax哪里错了
- Sys.UI.DomEvent.addHandler($get("Button1"), "click", formatDate); 里面的$是什么意思?
- ASP.NET上传文件路径
- 在studio 2005工具箱找不到ADO.NET类对象:SqlDataAdapter,Sqlconnection,Sqlcommand
- 跪求一条sql语句,如何取得"今日"的发表的数量
- 在 .cs 文件中, 怎么调用 其他 .cs(未编译)
- 关于控件事件的疑问
- 如何动态生成menu中的菜单项
- 求C# asp.net数据不用dataGRid分页的源代码!
- 求一正则
- 通过当前页面的操作,实现对特定的网页访问并实现该网页的功能操作
你这个数据源似乎查询的是当前页的数据 既然datalist的数据源都只有当前页的数据你怎么能够导出所有数据呢
string query = "Select cSendUnitName As 出货方,cPeriod As 流向期段,(datename(yyyy,dBillDate) + '-'+ datename(mm,dBillDate) +'-'+ datename(dd,dBillDate)) As 开票日,cRecMarketName As 收货市场,cRecUnitName As 收货方,cInvName As 品种,cInvStd As 规格,cBatch As 批次,iQuantity As 数量 From v_FlowRecordsList ";
query += cond + " Order By cInvName,cPeriod,cSendUnitName,dDate Desc"; DataTable dt = dbt.ExecuteTable(query, param); NOPIHelper.ExportByWeb(dt, "流向数据列表", "流向数据列表.xls");
第一种 数据量不是非常大 那查询的时候一次查出所有数据放在datatable中 分页的时候根据datatable查询分页,导出excle时为datatable的数据(这种情况可能第一次查询很慢 但分页很快)。
第二种 数据量庞大 查询的时候肯定只能一次查一页了 因为导出功能基本不会常用 所以在导出的时候再查询所有并导出,在导出的时候加点特效 让客户知道系统正在进行导出工作就好了
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.POIFS;
using NPOI.Util; namespace Galsun.Common
{
public class NOPIHelper
{
public static void Export(DataTable dtSource, string strHeaderText, string strFileName)
{
using (MemoryStream ms = Export(dtSource, strHeaderText))
{
using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
} }
} public static MemoryStream Export(DataTable dtSource, string strHeaderText)
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet();
#region 右击文件 属性信息
{
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
dsi.Company = "";
workbook.DocumentSummaryInformation = dsi;
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
si.Author = "";
si.ApplicationName = "";
si.Title = strHeaderText;
si.CreateDateTime = DateTime.Now;
workbook.SummaryInformation = si;
}
#endregion HSSFCellStyle dateStyle = workbook.CreateCellStyle();
//HSSFDataFormat format = workbook.CreateDataFormat();
//dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
//取得列宽
int[] arrColWidth = new int[dtSource.Columns.Count];
foreach (DataColumn item in dtSource.Columns)
{
arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
}
for (int i = 0; i < dtSource.Rows.Count; i++)
{
for (int j = 0; j < dtSource.Columns.Count; j++)
{
int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;
if (intTemp > arrColWidth[j])
{
arrColWidth[j] = intTemp;
}
}
}
int rowIndex = 0;
foreach (DataRow row in dtSource.Rows)
{
#region 新建表,填充表头,填充列头,样式
if (rowIndex == 65535 || rowIndex == 0)
{
if (rowIndex != 0)
{
sheet = workbook.CreateSheet();
}
#region 表头及样式
{
HSSFRow headerRow = sheet.CreateRow(0);
headerRow.HeightInPoints = 25;
headerRow.CreateCell(0).SetCellValue(strHeaderText);
HSSFCellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = CellHorizontalAlignment.CENTER;
HSSFFont font = workbook.CreateFont();
font.FontHeightInPoints = 20;
font.Boldweight = 700;
headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle; sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1));
headerRow.Dispose();
}
#endregion #region 列头及样式
{
HSSFRow headerRow = sheet.CreateRow(1);
HSSFCellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = CellHorizontalAlignment.CENTER;
HSSFFont font = workbook.CreateFont();
font.FontHeightInPoints = 10;
font.Boldweight = 700;
headStyle.SetFont(font);
foreach (DataColumn column in dtSource.Columns)
{
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
headerRow.GetCell(column.Ordinal).CellStyle = headStyle; //设置列宽
sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256); }
headerRow.Dispose();
}
#endregion
rowIndex = 2; }
#endregion #region 填充内容
HSSFRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in dtSource.Columns)
{
HSSFCell newCell = dataRow.CreateCell(column.Ordinal); string drValue = row[column].ToString(); switch (column.DataType.ToString())
{
case "System.String"://字符串类型
newCell.SetCellValue(drValue);
break;
case "System.DateTime"://日期类型
DateTime dateV;
DateTime.TryParse(drValue, out dateV);
newCell.SetCellValue(dateV); newCell.CellStyle = dateStyle;//格式化显示
break;
case "System.Boolean"://布尔型
bool boolV = false;
bool.TryParse(drValue, out boolV);
newCell.SetCellValue(boolV);
break;
case "System.Int16"://整型
case "System.Int32":
case "System.Int64":
case "System.Byte":
int intV = 0;
int.TryParse(drValue, out intV);
newCell.SetCellValue(intV);
break;
case "System.Decimal"://浮点型
case "System.Double":
double doubV = 0;
double.TryParse(drValue, out doubV);
newCell.SetCellValue(doubV);
break;
case "System.DBNull"://空值处理
newCell.SetCellValue("");
break;
default:
newCell.SetCellValue("");
break;
} }
#endregion rowIndex++; } using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
sheet.Dispose();
workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet
return ms;
}
} public static void ExportByWeb(DataTable dtSource, string strHeaderText, string strFileName)
{ HttpContext curContext = HttpContext.Current; // 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = Encoding.UTF8;
curContext.Response.Charset = "";
curContext.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8)); curContext.Response.BinaryWrite(Export(dtSource, strHeaderText).GetBuffer());
curContext.Response.End(); } public static void ExportByWeb(DataTable dtSource, string templatePath,string Sheet, int rowIndex,string strHeaderText, string strFileName)
{ HttpContext curContext = HttpContext.Current; // 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = Encoding.UTF8;
curContext.Response.Charset = "";
curContext.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8)); curContext.Response.BinaryWrite(Export(dtSource, templatePath, Sheet, rowIndex, strHeaderText,null).GetBuffer());
curContext.Response.End(); }
public static void ExportByWeb(DataTable dtSource, string templatePath, string Sheet, int rowIndex, string strHeaderText, string strFileName,short[] backColor)
{ HttpContext curContext = HttpContext.Current; // 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = Encoding.UTF8;
curContext.Response.Charset = "";
curContext.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8)); curContext.Response.BinaryWrite(Export(dtSource, templatePath, Sheet, rowIndex, strHeaderText, backColor).GetBuffer());
curContext.Response.End(); }
}
}
或使用隐藏的 gridview绑定所有数据,再导出