繁体   English   中英

将数据集导出到Excel

[英]Export Dataset to Excel

如何将数据集导出到可由Excel 2003打开的文件?

你会详细说明吗? 因为很难理解CSV / TSV

马克,您能给我们做个例子吗。v现在只有听到术语csv / tsv

我认为这会对您有所帮助。 使用http处理程序

<%@ WebHandler Language="C#" Class="DownloadAllEvent" %>

using System;
using System.Web; 
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.IO;

public class DownloadAllEvent : IHttpHandler
{ 
    const int BUFFERSIZE = 1024;

    public bool IsReusable
    {
         get
    {
          return true;
    }
 } 

 public void ProcessRequest(HttpContext context) 
{ 
   HttpResponse response = context.Response; 
   HttpRequest request = context.Request; 
   response.BufferOutput = true;
   response.ContentType = "application/octet-stream";
   response.AddHeader("Content-Disposition", "attachment; filename=Events.csv");
   response.Cache.SetCacheability(HttpCacheability.NoCache);
   //string  csvfile = request.QueryString["csvfile"];
   string strNoofIds = request.QueryString["NoofIds"];
   // declare variables or do something to pass parameter to writecalEntry function

   writeCalEntry(response.Output, strguid, sectionid); 
  response.End(); 
 }

   public void writeCalEntry(TextWriter output, string[] strguid,string sectionid)
   {
        DataTable dt = createDataTable();
        DataRow dr;

        StringBuilder sbids = new StringBuilder();



        // process table if neeed.. use following code to create CSV format string from table

        string separator;      

        separator = ","; //default

        string quote = "\"";

        //create CSV file
        //StreamWriter sw = new StreamWriter(AbsolutePathAndFileName);

        //write header line

        StringBuilder sb = new StringBuilder();


        int iColCount = dt.Columns.Count;
        for (int i = 0; i < iColCount; i++)
        {
            //sw.Write(TheDataTable.Columns[i]);
            sb.Append(dt.Columns[i]);
            if (i < iColCount - 1)
            {
                //sw.Write(separator);
                sb.Append(separator);
            }
        }
        //sw.Write(sw.NewLine);
        sb.AppendLine();

        //write rows
        foreach (DataRow  tempdr in dt.Rows)
        {
            for (int i = 0; i < iColCount; i++)
            {
                if (!Convert.IsDBNull(tempdr[i]))
                {
                    string data = tempdr[i].ToString();
                    data = data.Replace("\"", "\\\"");
                    //sw.Write(quote + data + quote);
                    sb.Append(quote + data + quote);
                }
                if (i < iColCount - 1)
                {
                    //sw.Write(separator);
                    sb.Append(separator);
                }
            }
            //sw.Write(sw.NewLine);
            sb.AppendLine();
        }
        //sw.Close();
        UnicodeEncoding uc = new UnicodeEncoding();
        output.WriteLine(sb);

}

public static DataTable createDataTable()
{
    DataTable dt = new DataTable("EventsData");
    // create tables as needed which will be converted to csv format.
    return dt;
}

将此httphandler文件称为要在其中将数据导出为ex​​cell格式的文件

Response.Redirect("downloadFile.ashx");

您也可以在Response.Redirect中发送参数,该参数可以在.ashx文件中获取。 我想这会帮助你。

最简单的方法可能是将单个表导出为csv / tsv。 网上有很多这样的样本。

如果您想在2003年这样做,那么您已经太晚了六年。 ;)

如果您用“ 2003”表示别的意思,也许您可​​以澄清一下,人们可以给出更好的答案(尽管以前的答案,导出为CSV是一个很好的答案)。

XML Spreadsheet可能就是您想要的。

这个答案。

我个人发现的最好方法是使用XML和电子表格组件。

示例代码太乱了,无法在此处发布,但请从此处开始,看看它的位置: http : //msdn.microsoft.com/zh-cn/library/aa140062.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM