求助~~想实现数据导出到excel,原程序是ASP的,能不能加上ASP.NET代码来实现啊!! 原程序是ASP的,实现数据导出比较困难,小弟刚学asp.net不久,不知道ASP.NET能不能和以前的ASP兼容且并存在一个系统中,然后用ASP.NET来实现数据导出!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 整个系统重做吗,这个太麻烦了,因为现在这个系统都是ASP开发的,但是数据导出有困难,希望能用.net来实现! 我试了建立.NET架构然后新建.aspx的页面(用C#),加入原来的ASP代码,但是高度通不过。我刚学。NET 是菜鸟,请大家给点指点!! 我这里有一个导出Excel的类,你参考一下.using System;using System.Data;using System.IO;using System.Web;using DAL;namespace LL.LCommon{ /// <summary> /// ToExcel 的摘要说明。 /// </summary> public class ToExcel { public ToExcel() { } /// <summary> /// 导出Excel /// </summary> /// <param name="ColName"></param> /// <param name="Col"></param> /// <param name="SqlStr"></param> /// <returns>//0,成功。1,没有数据。3,获取DataSet失败。</returns> public int GetExcel(string[] ColName,string[] Col,string SqlStr) { DataAccess MyDA=new DataAccess(); DataSet MyDS=new DataSet(); int MyResult=MyDA.GetDataSet(SqlStr,MyDS); MyDA.Dispose(); if(MyResult==0) { return GetExcel(ColName,Col,MyDS); } else { return MyResult; } } /// <summary> /// 导出Excel /// </summary> /// <param name="ColName"></param> /// <param name="Col"></param> /// <param name="MyDS"></param> /// <returns>//0,成功。1,没有数据。</returns> public int GetExcel(string[] ColName,string[] Col,DataSet MyDS) { DataTable MyDT=MyDS.Tables[0]; return GetExcel(ColName,Col,MyDT); } /// <summary> /// 导出Excel /// </summary> /// <param name="ColName"></param> /// <param name="Col"></param> /// <param name="MyDT"></param> /// <returns>0,成功。1,没有数据。</returns> public int GetExcel(string[] ColName,string[] Col,DataTable MyDT) { StringWriter MySW=new StringWriter(); if(ColName.Length!=0) { for(int i=0;i<ColName.Length;i++) { MySW.Write(ColName[i]+"\t"); } MySW.WriteLine(); } if(MyDT.Rows.Count==0) { return 1; } else { for(int j=0;j<MyDT.Rows.Count;j++) { for(int k=0;k<Col.Length;k++) { MySW.Write(MyDT.Rows[j][Col[k]]+"\t"); } MySW.WriteLine(); } } HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer= false; HttpContext.Current.Response.Charset="GB2312"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls"); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.Write(MySW); HttpContext.Current.Response.End(); return 0; } }} fileInfo 获取的文件列表如何排序? VS2010版面 读取网页生成的html代码发现有些奇怪 asp.net url重写问题 使用varchar存储文本时如何不去掉空格 找一个免费的日历控件 请问如何用C#实现在浏览器中打开指定URL的页面 想做一个留言本,Html标签过滤问题 asp.net下如何使用windows media play? 如何实现在DataGrid中缩进功能 我用邹键的分页储存过程,当表有主键或自动增长字段时翻页时出错,请解解决办法 asp.net2+sql server2005+c#页面自动居中
然后新建.aspx的页面(用C#),加入原来的ASP代码,但是高度通不过。
我刚学。NET 是菜鸟,请大家给点指点!!
using System;
using System.Data;
using System.IO;
using System.Web;
using DAL;namespace LL.LCommon
{
/// <summary>
/// ToExcel 的摘要说明。
/// </summary>
public class ToExcel
{
public ToExcel()
{
}
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="ColName"></param>
/// <param name="Col"></param>
/// <param name="SqlStr"></param>
/// <returns>//0,成功。1,没有数据。3,获取DataSet失败。</returns>
public int GetExcel(string[] ColName,string[] Col,string SqlStr)
{
DataAccess MyDA=new DataAccess();
DataSet MyDS=new DataSet();
int MyResult=MyDA.GetDataSet(SqlStr,MyDS);
MyDA.Dispose();
if(MyResult==0)
{
return GetExcel(ColName,Col,MyDS);
}
else
{
return MyResult;
}
} /// <summary>
/// 导出Excel
/// </summary>
/// <param name="ColName"></param>
/// <param name="Col"></param>
/// <param name="MyDS"></param>
/// <returns>//0,成功。1,没有数据。</returns>
public int GetExcel(string[] ColName,string[] Col,DataSet MyDS)
{
DataTable MyDT=MyDS.Tables[0];
return GetExcel(ColName,Col,MyDT);
} /// <summary>
/// 导出Excel
/// </summary>
/// <param name="ColName"></param>
/// <param name="Col"></param>
/// <param name="MyDT"></param>
/// <returns>0,成功。1,没有数据。</returns>
public int GetExcel(string[] ColName,string[] Col,DataTable MyDT)
{
StringWriter MySW=new StringWriter();
if(ColName.Length!=0)
{
for(int i=0;i<ColName.Length;i++)
{
MySW.Write(ColName[i]+"\t");
}
MySW.WriteLine();
}
if(MyDT.Rows.Count==0)
{
return 1;
}
else
{
for(int j=0;j<MyDT.Rows.Count;j++)
{
for(int k=0;k<Col.Length;k++)
{
MySW.Write(MyDT.Rows[j][Col[k]]+"\t");
}
MySW.WriteLine();
}
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer= false;
HttpContext.Current.Response.Charset="GB2312";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.Write(MySW);
HttpContext.Current.Response.End();
return 0;
} }
}