日期:2014-05-18  浏览次数:20396 次

如何控制客户端的excel格式
我想实现的excel格式,导出到客户端
  报表
年度:2007-2008 地点:中国 面积: 100 
这里是导出的gridview数据
制表人:张三 日期:系统日期

请问怎么实现?

------解决方案--------------------
//试试看下边的代码

StringWriter sw = new StringWriter();
sw.WriteLine("年度: " + "\t" + "2007-2008" + "\t" + " 地点:" + "\t" + " 中国" + "\t" + "面积:"+ "\t"+"100");
foreach (GridViewRow dr in GridView1.Rows)
{
int i = dr.Cells.Count;
int j=0;
string strInfo = "";
while(j<i)
{
if(j!=0)
{
strInfo = strInfo + "\t" + dr.Cells[j].Text.ToString();
}
else
{
strInfo = dr.Cells[j].Text.ToString();
}
j++;
}
sw.WriteLine(strInfo);
}
sw.WriteLine("制表人: " + "\t" + "张三" + "\t" + "日期: "+ "\t"+"系统日期");
sw.Close();

Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + ".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();