datagrid 导入 excel 问题

waxln 2005-08-08 11:00:09
private void Button1_Click(object sender, System.EventArgs e)
{
if(TextBox1.Text=="")
{
Response.Write("<SCRIPT language=javascript>");
Response.Write("window.alert(''请输入文件名'');");
Response.Write("</SCRIPT>");
}
else
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition","attachment;filename="+TextBox1.Text+".xls"); //定义输出文件和文件名
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
}
}
这段代码 是把datagrid 导入 excel中
现在有一个问题
问什么导入后excel表中第一行空白!!
谢谢!!!!!!!!
...全文
106 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
7in10 2005-09-16
  • 打赏
  • 举报
回复
private void BtnExport_Click(object sender, System.EventArgs e)
{
StringWriter sw=new StringWriter();
sw.WriteLine("所属库房\t类别名称\t号码段\t开始序号\t结尾序号\t库存状态");
dt=(System.Data.DataTable)Session["SmnoStock"];
DataRow dr;
string housename,sortname,smno,fromno,tono,stock;
for(int i=0;i<dt.Rows.Count;i++)
{
dr=dt.Rows[i];
housename=dr["housename"].ToString();
sortname=dr["sortname"].ToString();
smno=dr["smno"].ToString();
fromno=dr["fromno"].ToString();
tono=dr["tono"].ToString();
stock=dr["stock"].ToString();
sw.WriteLine(housename+"\t"+sortname+"\t"+smno+"\t"+fromno+"\t"+tono+"\t"+stock);
}

sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=sm"+DateTime.Now.ToShortDateString()+".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
tigerwen01 2005-08-08
  • 打赏
  • 举报
回复
excel的SHEET单元的第一行是标题行吧,你要自己做处理。
xiaowangtian1117 2005-08-08
  • 打赏
  • 举报
回复
StringWriter tw=new StringWriter();
HtmlTextWriter hw=new HtmlTextWriter(tw);
grd.RenderControl(hw);
int intV;
string strFileName;
Random rd=new Random();
intV=rd.Next();
strFileName=DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+intV.ToString()+".xls";
string filenew=Page.MapPath("../uploading/"+strFileName);
FileStream fl=new FileStream(filenew,FileMode.CreateNew);
StreamWriter fw=new StreamWriter(fl,System.Text.Encoding.GetEncoding("GB2312"));
fw.WriteLine(tw.ToString());
fw.Close();
fl.Close();
string strScript = "<script language=javascript> window.open('../uploading/" + strFileName + "', '', 'left=50,top=50,resizable=1,scrollbars=1,width=500,height=400,menubar=1'); </script>";
Page.RegisterStartupScript("", strScript);

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧