我导出的EXCEL读取出现"外部表不是预期的格式",那位有做过.可以把源代码帖上让我看看吗?
解决方案 »
- TextBox的onblur事件
- 窗口插件AeroWindow最小化没了
- 一个较笨的问题
- PasswordRecovery控件使用模板方式,为什么输入用户名点提交按钮没反应,不进入QuestionTemplate
- 请问这个功能如何实现呢
- 求助!!!!!!!!!!!!!!!!!!!!!!!!!!
- 一个时间段之间的时长统计(剔除某个时间段)
- 关于把带“”号和《》号的数据保存到数据库的问题。。。怎么解决呢???
- EF中T4模板EdmProperty 类属性找不到的问题
- dropdownlist 如果实现这样的功能
- !!!!!!!!!!!!!!!!!!!!如何给文本框添加,获取焦点,和失去焦点事件!!!!!!!!!!!!!!!
- 急,100分,是用GB2312还是utf-8??急
SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
DataTable dt=ds.Tables["table1"];
string name=System.Configuration.ConfigurationSettings.AppSettings["downloadurl"].ToString()+DateTime.Today.ToString("yyyyMMdd")+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+".xls";//存放到 web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数
FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
sw.WriteLine("自动编号,姓名,年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.WriteFile(name); // 把文件流发送到客户端
Response.End();
1、有固定的格式,样子好看(datagrid的样子)局限性:
1、不适合数据交换,里面有html代码,比较乱,没有固定格式
2、datagrid不能有分页、排序等,否则出错实现方法:
Response.Clear();
Response.Buffer= false;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=test.xls");
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);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();