怎样将一个页面的内容导出为Excel ??不是用sql,是直接将整个页面打印成Excel .........
这个页面是一个比较复杂的报表,
如果通过sql查询导出,在Excel中的显示将会非常麻烦,实现起来很困难。
所以,俺想省点事,
将整个页面都导为Excel,就像打印一样。
能否实现呢?
Help.!
这个页面是一个比较复杂的报表,
如果通过sql查询导出,在Excel中的显示将会非常麻烦,实现起来很困难。
所以,俺想省点事,
将整个页面都导为Excel,就像打印一样。
能否实现呢?
Help.!
解决方案 »
- 请教asp下挂一个aspx子目录问题
- DropDownList刷新的问题,谢谢各位
- 请教高手,Windows2003 IIS 虚拟目录 有可能建在网络映射盘吗?
- 页面URL传参的问题! 十万万万万万万万万万万万分火急.
- 紧急求救,还没睡的朋友帮忙看看
- 谁有asp.net视频教程
- 像CSDN上这种网页找不着了,出错了,但是还能显示出CSDN信息的页是怎么做的
- 关于DropDownlist无刷新提交获取选中值的问题
- 如何可以让网页内容置中显示,不管客户端的分辨率是多少?
- 有人买过asp.net编程实作教程这本书吗?(进者有分!!!!)
- 什么输出啊
- gridview 数据绑定列通过datasource来update如何设置参数
//text = aaa.document.body
function savePageASxls(text,title)
{
if(document.queryCommandEnabled('saveAS')) {
var para = 'toolbar=0,titlebar=0,menubar=0,resizable=0,Height=7,Width=8,status=0,top=2000,left=2000,scrollbars=0';
win=open('about:blank','',para);
win.charset='GB2312';
win.document.write(text);
win.document.execCommand('SaveAs',true,title + '.xls');
win.close();
} }
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<link rel="stylesheet" href="Style.css" type="text/css">
<body>
<form id="Form1" method="post" runat="server">
<table width="170%"><tr><td>
<asp:datagrid
id="DataGrid1"
bgcolor="#efefef"
HeaderStyle-BackColor="#718BD6"
HeaderStyle-ForeColor="#FFFF66"
AlternatingItemStyle-BackColor="#FFFFFF"
itemstyle-backcolor="#FFFFFF"
runat="server">
<ItemStyle HorizontalAlign="center" Height="20"></ItemStyle>
<Columns> </Columns>
</asp:datagrid>
</td></tr></table>
<P>
<asp:button id="Btn_Import_Excel" runat="server" Text="轉到EXCEL"></asp:button></P>
</form>
</body>
</HTML>
------------------------------
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;
using System.IO;
using System.Data.SqlClient ;
using System.Text;
using System.Configuration;namespace DataGrid_import_WordExcel
{ public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnImportWord;
protected System.Web.UI.WebControls.Button Btn_Import_Excel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public DataRow dr;
private DataSet myDS =new DataSet();private void Page_Load(object sender, System.EventArgs e)
{
//CreateDataSet();
Data_Load();
if(!IsPostBack)
{
//DataBind();
}
} #region Web 敦极扢數?汜傖腔測鎢
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
} private void InitializeComponent()
{
this.Btn_Import_Excel.Click += new System.EventHandler(this.Btn_Import_Excel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion private void ExportDataGrid(string FileType, string FileName) //植DataGrid絳堤
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Btn_Import_Excel_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-excel", "Excel.xls"); //絳善Excel
} private void Data_Load()
{ SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["produce1"]);
SqlCommand cmd=new SqlCommand("excel",myConnection);
cmd.CommandType=CommandType.StoredProcedure;if(Session["companyno"]!="")
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = Session["companyno"];
}
else
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = "";
} myConnection.Open(); DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}DataView CreateDataSource()
{
string nowDSN=ConfigurationSettings.AppSettings["produce1"];
SqlConnection myConnection=new SqlConnection(nowDSN);SqlCommand cmd=new SqlCommand("excel",myConnection);
cmd.CommandType=CommandType.StoredProcedure;if(Session["companyno"]!="")
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = Session["companyno"];
}
else
{
cmd.Parameters.Add("@sql1",SqlDbType.NVarChar,50);
cmd.Parameters["@sql1"].Value = "";
} DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
return ds.Tables["block2"].DefaultView;
myConnection.Close();
Page.DataBind();
} void DataBind()
{
DataView source=CreateDataSource();
if(!IsPostBack)
{
}
DataGrid1.DataSource = source;
DataGrid1.DataBind();
} }
}
private void ExportDataGrid2(string FileType, string FileName) //植DataGrid絳堤
{
Response.Clear();
Response.Buffer= true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
this.EnableViewState =false;
System.IO.StringWriter tw = new StringWriter();
System.Web.UI.HtmlTextWriter hw =new HtmlTextWriter(tw);
MyList.RenderControl(hw); //注:MyList.RenderControl(hw); //只转数据,但MyList里有<asp:CheckBox id="selected" runat="server"></asp:CheckBox>就出错
//this.RenderControl(hw); //按钮等也转出来了Response.Write(tw.ToString());
Response.End();
}
private void Button8_Click(object sender, System.EventArgs e)
{
//Save("application/ms-excel", "Excel.xls"); //絳善Excel
//ExportDataGrid("application/ms-excel", "Excel.xls"); //絳善Excel
ExportDataGrid2("application/ms-excel", "Excel.xls"); //絳善Excel
}