如何把DataGrid导出xls到服务端并客户端下载
DataGrid dg = m_objData as DataGrid;
m_sFileName = HttpUtility.UrlEncode(m_sFileName, Encoding.UTF8);
System.IO.StringWriter oStringWriter = null;
HtmlTextWriter oHtmlTextWriter = null;
Response.Clear();
Response.ClearHeaders ();
Response.ClearContent();
Response.Buffer= true;
Response.Charset= "utf-8 ";
Response.AppendHeader( "Content-Disposition ", "attachment;filename= " + m_sFileName);
Response.ContentEncoding = Encoding.GetEncoding( "utf-8 ");
Response.ContentType = "application/vnd.ms-excel ";
this.EnableViewState = false;
oStringWriter = new StringWriter();
oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
dg.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
以上代码是dg导出到xls并下载。
小弟想得到的是导出到服务器后再下载?
导出到服务器的时候不要弹出框。
导出到客户端的时候需要弹出框。
------解决方案--------------------SpreadsheetClass xlsheet=new SpreadsheetClass();
conn.Open();
cmd=new OleDbCommand(sql,conn);
OleDbDataReader reader=cmd.ExecuteReader();
int numbercols=reader.FieldCount;
int row=2;
int i=0;
//输出标题
for(i=0;i <numbercols;i++)
{
xlsheet.ActiveSheet.Cells[1, i + 1] = reader.GetName(i).ToString();
}
// 输出字段内容
while(reader.Read())
{
for(i=0;i <numbercols;i++)
{
xlsheet.ActiveSheet.Cells[row, i + 1] = reader.GetValue(i).ToString();
}
row = row + 1;
}
reader.Close();
conn.Close();
try
{
xlsheet.ActiveSheet.Export(Server.MapPath( ". ") + "\\mfExcel\\ " + this.xlfile.Text+ ".xls ",OWC.SheetExportActionEnum.ssExportActionNone);
}
catch(System.Runtime.InteropServices.COMException e )
{
Response.Write( "错误: " + e.Message);
}
using System.Web.Security;
using OWC;
using System.Data.OleDb;
using System.IO;
别人写的
------解决方案--------------------也可以用FarPoint控件(第三方)
http://blog.csdn.net/blackhero/archive/2006/08/25/1116399.aspx