asp.net导出EXCEL问题!!

xiaoxiao8372 2010-07-13 11:42:40
ExportExcel(mydt, sw);
Response.AddHeader("Content-Disposition", "attachment; filename=MsgCountandorderstuCount.xls");//
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();

上边的代码看似导出的EXCEL,可实际上却是CSV类型,因为用记事本可以打开的!
我现在要实现导出的是真正的EXCEL,各位大侠有什么好的方法啊,请赐教!!
...全文
1886 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
liiifeiii 2010-07-13
  • 打赏
  • 举报
回复
dui
骑猪看海 2010-07-13
  • 打赏
  • 举报
回复
GC.Collect();
ApplicationClass excel;
int rowIndex = 1;
int colIndex = 1;
_Workbook xBk;
_Worksheet xSt;
string sTimes = DateTime.Now.ToString("yyyyMMddHHmmss");
string strFileName = "";
System.IO.FileInfo file;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
xSt = (_Worksheet)xBk.ActiveSheet;
excel.Cells[rowIndex, colIndex]=“aa” 给excel第一行第一列赋值
#region 保存文件
strFileName = Server.MapPath("Uploads/qafiles/") + "QAIndividualQC" + sTimes + ".xls";
xBk.SaveCopyAs(strFileName);

xBk.Close(false, null, null);

excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();

file = new System.IO.FileInfo(strFileName);
Response.Clear();
//Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
Response.Flush();
file.Delete();
// 停止页面的执行
Response.End();
#endregion
骑猪看海 2010-07-13
  • 打赏
  • 举报
回复
快速的方法都是用流,用记事本打开的都是html代码,要导出真正的EXCEL,你可以导出后打开,在另存为excel格式就行了。或者用Microsoft.Office.Interop.Excel组件,以下代码参考:

GC.Collect();
ApplicationClass excel;
int rowIndex = 1;
int colIndex = 1;
_Workbook xBk;
_Worksheet xSt;
string sTimes = DateTime.Now.ToString("yyyyMMddHHmmss");
string strFileName = "";
System.IO.FileInfo file;
excel.Cells[rowIndex, colIndex]=“aa” 给excel第一行第一列赋值
#region 保存文件
strFileName = Server.MapPath("Uploads/qafiles/") + "QAIndividualQC" + sTimes + ".xls";
xBk.SaveCopyAs(strFileName);

xBk.Close(false, null, null);

excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();

file = new System.IO.FileInfo(strFileName);
Response.Clear();
//Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(file.FullName);
Response.Flush();
file.Delete();
// 停止页面的执行
Response.End();
#endregion
myhope88 2010-07-13
  • 打赏
  • 举报
回复
那就用excel组件,不过相对来说就麻烦多了
zhulong1111 2010-07-13
  • 打赏
  • 举报
回复
public void DGToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
BaoShiqiang 2010-07-13
  • 打赏
  • 举报
回复
已经有答案
moonwrite 2010-07-13
  • 打赏
  • 举报
回复
ExportExcel(mydt, sw);
如果你这里SW是CSV的话 就是你把的后缀改成xls 也没有用

NPOI官方网站:http://npoi.codeplex.com/ | QQ交流群: 78142590

NPOI处理Excel的好东西
happy664618843 2010-07-13
  • 打赏
  • 举报
回复
都回答了 帮顶
chen_ya_ping 2010-07-13
  • 打赏
  • 举报
回复
楼主,网上找个好点的导出excel的代码,你的那个我看的都很简单。
zhulong1111 2010-07-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 laoyingisme 的回复:]
这点代码很重要,特别是用了Masterpage
public override void VerifyRenderingInServerForm(Control control)
{
}
[/Quote]错误提示:该控件应放在runat=“server”标记内
laoyingisme 2010-07-13
  • 打赏
  • 举报
回复
这点代码很重要,特别是用了Masterpage
public override void VerifyRenderingInServerForm(Control control)
{
}
adu_ado 2010-07-13
  • 打赏
  • 举报
回复
来学习~~~
mao44mao 2010-07-13
  • 打赏
  • 举报
回复
用 Microsoft.Office.Interop.Excel 组件
马老虎 2010-07-13
  • 打赏
  • 举报
回复
可以使用 com组件
http://topic.csdn.net/u/20100520/12/7e077e0b-6336-4972-bd54-c3b48338f9ba.html

也可以使用第三方的组件
wuyq11 2010-07-13
  • 打赏
  • 举报
回复
打开EXCEL模板,赋值到单元格实现EXCEL文件下载

protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "a.xls");
}

private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
2321zhf 2010-07-13
  • 打赏
  • 举报
回复

set objExcel1=server.CreateObject("Excel.Application")

objExcel1.Workbooks.Open(Server.Mappath("model.xlt")) '用Excel模板定义EXCEL样式

objExcel1.DisplayAlerts=false

objExcel1.Application.Visible=false

objExcel1.Sheets(1).Select '选中工作页
Set sheetActive=objExcel1.ActiveWorkbook.ActiveSheet

sheetActive.range("A1").value=“单元格写入值"

62,046

社区成员

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

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

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

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