数据集导出CSV

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Export2Excel.aspx.cs" Inherits="WebApplication1.Export2Excel" %>

<!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">
<head runat="server">
    
<title></title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
        
</asp:ScriptManager>
        
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        
<div>
        
<span>通过UpdatePanel导出Excel</span>
        
<asp:UpdatePanel ID="updatePanel1" runat="server">
        
<ContentTemplate>
        
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button1_Click" />
        
</ContentTemplate>
        
<Triggers>
        
<asp:PostBackTrigger ControlID="Button2" />
        
</Triggers>
        
</asp:UpdatePanel>
        
</div>
    
</div>
    
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace WebApplication1
{
    
public partial class Export2Excel : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
public void DataSetToExcel(DataSet ds, string FileName)
        {
            
try
            {

                HttpResponse resp;
                resp 
= HttpContext.Current.Response;
                resp.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
                resp.AppendHeader(
"Content-disposition""attachment;filename=" +HttpUtility.UrlEncode(FileName) + ".xls");
                resp.ContentType 
= "application/ms-excel";

                
//变量定义 
                string colHeaders = null;
                
string Is_item = null;
                StringWriter sfw 
= new StringWriter();
                
//定义表对象与行对象,同时用DataSet对其值进行初始化 
                System.Data.DataTable dt = ds.Tables[0];
                DataRow[] myRow 
= dt.Select();
                
int i = 0;
                
int cl = dt.Columns.Count;

                
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符 
                for (i = 0; i < cl; i++)
                {
                    colHeaders 
+= dt.Columns[i].Caption.ToString() + "\t";
                }
                sfw.WriteLine(colHeaders);

                
//逐行处理数据 
                foreach (DataRow row in myRow)
                {
                    
//当前数据写入 
                    for (i = 0; i < cl; i++)
                    {
                        Is_item 
+= row[i].ToString() + "\t";
                    }
                    sfw.WriteLine(Is_item);
                    
//sw.WriteLine(Is_item); 
                    Is_item = null;
                }
                resp.Write(sfw);
                
//resp.Clear(); 
                resp.End();
            }
            
catch (Exception e)
            {
                
throw e;
            }
        }

        
protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet ds 
= new DataSet();
            DataTable dt 
= new DataTable();
            dt.TableName 
= "物资供应表";
            dt.Columns.Add(
"Id"typeof(int));
            dt.Columns.Add(
"Wuzi"typeof(string));
            dt.Columns.Add(
"Number",typeof(int));
            
for (int i = 0; i < 100; i++)
            {
                Random r
=new Random(100);

                DataRow row 
= dt.NewRow();
                row[
"Id"= i;
                row[
"Wuzi"= "物资名称"+i.ToString();
                row[
"Number"]=r.Next(100);
                dt.Rows.Add(row);
            }
            ds.Tables.Add(dt);
            
string ff = System.DateTime.Now.Date.ToString().Split(' ')[0].ToString() + "物资统计";
            DataSetToExcel(ds, ff);
        }
    }
}
posted @ 2011-04-18 09:45  清凉一夏  阅读(396)  评论(0编辑  收藏  举报