简体   繁体   中英

Export Dataset to Excel

How can I export a dataset to file that can be opened by Excel 2003 ?

will you elaborate it ? because it is diffculties to understand the CSV/TSV

marc will u give us a sample for doing it .v now ony heard the terms csv/tsv

I think This will help you. Use http handler

<%@ 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;
}

call this httphandler file where you want to export data in to excell format as

Response.Redirect("downloadFile.ashx");

you can send parametres also in Response.Redirect which can be fetched in .ashx file. I think this will hepl you.

Probably the easiest route is to export individual tables as csv/tsv. The 'net is full of samples of this.

If you want to do it in 2003, you're already six years too late. ;)

If you meant something else by "2003", maybe you could clarify and people could give a better answer (although the previous answer, export as CSV, is a pretty good one).

XML Spreadsheet could be what you're looking for.

Look at this answer.

The best way that I've personally found is to use XML and the spreadsheet component.

An example code would be far too messy to post here, but start here and see where it leads: http://msdn.microsoft.com/en-us/library/aa140062.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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