ASP.NET中将数据输出到Excel
在开发ISO文件管理系统的时候,曾经遇到过要将ASPX直接输出到EXCEL的需求,现将经验所得与大家分享。 其实,利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM等类型的文档很容易的。主要分为三步来完成。
一、定义文档类型、字符编码
Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
//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);
三、将目标数据绑定到输入流输出
this.RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
总结:本例程在Microsoft Visual Studio .NET 2003平台下测试通过,适用于C#和VB,当采用VB的时候将 this 关键字改成 me 。
相关文档:
1、定义CS类 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using Hash ......
ASP.NET中,若要将一个URL参数值赋值给一个变量,都得先判断参数是否存在,否则等待你的很可能就是"未将对象引用设置到对象的实例",以前都是
Request.QueryString["xx"] != null比较后再赋值,但今天在一个项目中发现竟还报错,代码大致结构如下:
复制内容到剪贴板
程序代码
int id = 0;
if (R ......
每个Web应用程序都要规划自己的目录结构,除了自己设计的目录结构意外,ASP.Net也定义一些有特殊意义的目录。
1.Bin:这个目录包含了所有的预编译的ASP.NET的Web应用程序使用的.Net程序集(通常是DLLs),这些程序集也包括预编译的网页类,以及被这些类所引用的其他的程序集。
&nb ......
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" & ......
原地址:
http://msdn.microsoft.com/en-us/library/aa480476.aspx
IIS Authentication
ASP.NET authentication is a two-step process. First, Internet Information Services (IIS) authenticates the user and creates a Windows token to represent the user. IIS determines the authentication mode that it shoul ......