asp.net如何导出excel

萤火架构 2007-05-30 04:09:06
如题:

请给出一个具体的例子!
...全文
477 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
mark~~
ChengKing 2007-06-03
  • 打赏
  • 举报
回复
Many:
http://blog.csdn.net/ChengKing/category/292667.aspx
penglewen 2007-06-03
  • 打赏
  • 举报
回复
mark
lin19850223 2007-06-03
  • 打赏
  • 举报
回复
kan le zai shuo
namhyuk 2007-06-02
  • 打赏
  • 举报
回复
mark.
hhxkss 2007-06-02
  • 打赏
  • 举报
回复
public static void DataTableExcel(System.Data.DataTable dtData, String FileName)
{
System.Web.UI.WebControls.GridView dgExport = null;
//当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
//IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;

if (dtData != null)
{
//设置编码和附件格式
//System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)作用是方式中文文件名乱码
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF7;
curContext.Response.Charset = "GB2312";

//导出Excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);

//为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView
dgExport = new System.Web.UI.WebControls.GridView();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
dgExport.DataBind();

//下载到客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
zhudao92 2007-06-02
  • 打赏
  • 举报
回复
你可以把dataset绑定到gridview上,把格式设定好,然后倒入,代码如下:

public override void VerifyRenderingInServerForm(Control control)
{
//这个方法一定要有
//base.VerifyRenderingInServerForm(control);
}

protected void btnConvert_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "gb2312";

//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开

//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
string strHeader = "post";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + strHeader + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档

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);

// 三、将目标数据绑定到输入流输出

//oHtmlTextWriter.WriteEncodedText(Server.UrlDecode(strHeader));
grdExcel.RenderControl(oHtmlTextWriter);

//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件

Response.Write(oStringWriter.ToString());
Response.End();
}
liudao 2007-06-02
  • 打赏
  • 举报
回复
http://www.51aspx.com/S/excel.aspx
CathySun118 2007-06-02
  • 打赏
  • 举报
回复
http://home.msuniversity.edu.cn/members/ChallengeGroup/m_Article/Detail.aspx?id=401
banni2003 2007-06-01
  • 打赏
  • 举报
回复
这里面有很不错的例子一定会对你有所帮助

http://blog.csdn.net/zuoyefeng_com/archive/2007/06/01/1633643.aspx
ckpckphaha 2007-05-30
  • 打赏
  • 举报
回复
引用自别人的blog:
地址:http://blog.csdn.net/yumanqing/archive/2007/01/06/1475393.aspx

代码:

try
...{ string filename;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择将导出的EXCEL文件存放路径";
sfd.Filter="excel文档(*.xls)|*.xls";
//sfd.OpenFile();
sfd.ShowDialog();
if (sfd.FileName != "")
...{
if(sfd.FileName.LastIndexOf(".xls")<=0)
...{
sfd.FileName=sfd.FileName+".xls";
}
filename=sfd.FileName;
if(System.IO.File.Exists(filename))
...{
System.IO.File.Delete(filename);
}
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
if (xlApp == null)
...{
MessageBox.Show("无法创建Excel对象,可能您的机器未安装Excel");
return;
}
Excel.Workbooks workbooks = xlApp.Workbooks;
Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
DataTable dt1 = (DataTable)dataGrid2.DataSource;
float percent=0;
long rowRead=0;
if(dt1==null)
...{
MessageBox.Show("1");
return;
}
long totalCount=dt1.Rows.Count;
this.progressBar1.Visible=true;
for (int i = 0; i < dt1.Rows.Count; i++)
...{
this.progressBar1.Value=i;
for (int j = 0; j < dt1.Columns.Count; j++)
...{
if (i == 0)
...{
worksheet.Cells[1, j + 1] = dt1.Columns[j].ColumnName;
}
worksheet.Cells[i + 2, j + 1] = dt1.Rows[i][j].ToString();
}
rowRead++;
percent=((float)(100*rowRead))/totalCount;
this.progressBar1.Text= "正在导出数据["+ percent.ToString("0.00") +"%]...";
}
this.progressBar1.Visible=false;
workbook.Saved = true;
workbook.SaveCopyAs(filename);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
MessageBox.Show("导出Excel完成!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//button2_Click(null,null);

}
}
catch(Exception ex)
...{
MessageBox.Show("导出Excel失败!"+ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Information);
}

62,046

社区成员

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

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

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

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