简体   繁体   English

将数据集导出到Excel

[英]Export Dataset to Excel

How can I export a dataset to file that can be opened by Excel 2003 ? 如何将数据集导出到可由Excel 2003打开的文件?

will you elaborate it ? 你会详细说明吗? because it is diffculties to understand the CSV/TSV 因为很难理解CSV / TSV

marc will u give us a sample for doing it .v now ony heard the terms csv/tsv 马克,您能给我们做个例子吗。v现在只有听到术语csv / tsv

I think This will help you. 我认为这会对您有所帮助。 Use http handler 使用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;
}

call this httphandler file where you want to export data in to excell format as 将此httphandler文件称为要在其中将数据导出为ex​​cell格式的文件

Response.Redirect("downloadFile.ashx");

you can send parametres also in Response.Redirect which can be fetched in .ashx file. 您也可以在Response.Redirect中发送参数,该参数可以在.ashx文件中获取。 I think this will hepl you. 我想这会帮助你。

Probably the easiest route is to export individual tables as csv/tsv. 最简单的方法可能是将单个表导出为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. 如果您想在2003年这样做,那么您已经太晚了六年。 ;) ;)

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

XML Spreadsheet could be what you're looking for. XML Spreadsheet可能就是您想要的。

Look at this answer. 这个答案。

The best way that I've personally found is to use XML and the spreadsheet component. 我个人发现的最好方法是使用XML和电子表格组件。

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

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

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