日期:2014-05-17 浏览次数:20898 次
c#语言导出数据到Excel出错,显示在页面上的信息:您试图在此 Web 服务器上访问的 Web 应用程序当前不可用。请点击 Web 浏览器中的'刷新'按钮重试您的请求。
代码:db.RunProc( "sp_downDataList ",prams,out dr);
grdexcel.DataSource = dr;
grdexcel.DataBind();
Button1.Enabled=false;
Response.Clear();
Response.Buffer= true;
Response.Charset= "gb2312 ";
Response.AppendHeader( "Content-Disposition ", "attachment;filename=sheet "+DateTime.Now.ToString( "yyyyMMddss ")+ ".xls ");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.ContentEncoding=System.Text.Encoding.GetEncoding( "gb2312 ");
Response.ContentType = "application/ms-excel ";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
grdexcel.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
------解决方法--------------------
不大赞同你的思路,我之前一般采用在服务器端生成.csv文件方式导出数据。
.csv是Excel可以直接读取的文件格式。多个字段以逗号分隔。写完之后使用Redirect语句即可。
使用StringBuilder可以提高性能。
------解决方法--------------------
孟老大的:
http://dotnet.aspx.cc/article/8a4cbf47-b888-4832-3389-ed3a3a3c8aab/read.aspx
------解决方法--------------------
up了,最近要做sharepoint,估计和excel少不了关系。mark
------解决方法--------------------
读取到dataset`` 应该和 reader 差不多`
private DataSet InstrumentExcelToDataSet(string Path)
{
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source= " + Path + "; " + "Extended Properties= 'Excel 8.0;IMEX=1 ' ";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter myCommand = null;
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][2].ToString().Trim();