100分求使用OWC类生成excel客户端报表,在线等,解决了立即结贴 我使用的是asp.net c#要把datagrid中的数据导入到客户端的excel,最好能让用户打开继续进行格式调整同时,大部分office版本是2003,可能用到的是owc11,大哥大姐们帮忙! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用了这个方法解决了:public void ToExcel(System.Web.UI.Control ctl) { // HttpContext.Current.Response.Charset ="GB2312"; HttpContext.Current.Response.Charset ="utf-8"; HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=memory.xls"); HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("utf-8"); HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ctl.Page.EnableViewState =false; System.IO.StringWriter tw = new System.IO.StringWriter() ; System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); } public void Export(string filename,string sql) { SpreadsheetClass xlsheet = new SpreadsheetClass(); GTSH.Database d=new atabase(Globals.DatabaseConnectionString); SqlCommand com=d.GetCommand(sql); SqlDataReader reader=com.ExecuteReader(); int cols=reader.FieldCount; int row=1; d.OpenConnection(); while(reader.Read()) { for(int i=0;i<cols;i++) { xlsheet.ActiveSheet.Cells[row,i+1]=reader.GetValue(i).ToString(); } row++; } reader.Close(); d.CloseConnection(); xlsheet.Export(HttpContext.Current.Server.MapPath("../../")+"ExcelFolder\\"+filename+".xls",OWC11.SheetExportActionEnum.ssExportActionNone,OWC11.SheetExportFormat.ssExportAsAppropriate); } http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50 怎样建立自己的个人网站? C#操作IIS,建立虚拟目录后,如何更改ASP.NET版本? dictionary中在key不变的时候如何改变它的value值啊? 关于调用开心001 OAuth的问题 请教各位大哥 vs中怎么在a.aspx.cs中的左边显示各行的行号啊 下虾求救 VS2008无法启动调试 怎样在论坛里发图片的问题 求以下正则表达式的写法?? (高难度)像Notes那样可伸缩(在线等待) 高手请进,小弟请教一关于asp.net中script的使用 为什么总是没有对齐?
public void ToExcel(System.Web.UI.Control ctl)
{
// HttpContext.Current.Response.Charset ="GB2312";
HttpContext.Current.Response.Charset ="utf-8";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=memory.xls");
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("utf-8");
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
{
SpreadsheetClass xlsheet = new SpreadsheetClass();
GTSH.Database d=new atabase(Globals.DatabaseConnectionString);
SqlCommand com=d.GetCommand(sql);
SqlDataReader reader=com.ExecuteReader();
int cols=reader.FieldCount;
int row=1;
d.OpenConnection();
while(reader.Read())
{
for(int i=0;i<cols;i++)
{
xlsheet.ActiveSheet.Cells[row,i+1]=reader.GetValue(i).ToString();
}
row++;
}
reader.Close();
d.CloseConnection();
xlsheet.Export(HttpContext.Current.Server.MapPath("../../")+"ExcelFolder\\"+filename+".xls",OWC11.SheetExportActionEnum.ssExportActionNone,OWC11.SheetExportFormat.ssExportAsAppropriate);
}