如何在Silverlight 3中将SQL Server数据库数据导出到Excel中

时间:2012-10-07 07:34:35

标签: silverlight-3.0

我是Silverlight的新手。

我正在使用VS-2008和Silverlight 3,SQL Server 2005。

我的要求是:我必须从数据库中检索数据并导出到Excel。

我用谷歌搜索,但我没有得到适当的链接或材料来满足我的要求。

有人可以指导我怎么做吗?

预先谢谢,

1 个答案:

答案 0 :(得分:0)

  

最简单的方法是使用NPOI(npoi.codeplex.com)..

     

基本上,您可以在xaml中定义以下事件:

private void Button_Click(object sender, RoutedEventArgs e)
{
    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    client.DownloadStringAsync(new Uri("DownloadFile.aspx", UriKind.Absolute));
}
     

以及服务器项目页面DownloadFile.aspx中的以下内容:

using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;

public partial class DownloadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    string filename = "test.xls";
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
    Response.Clear();

    InitializeWorkbook();
    GenerateData();
    Response.BinaryWrite(WriteToStream().GetBuffer());
    Response.End();
}

HSSFWorkbook hssfworkbook;

MemoryStream WriteToStream()
{
    //Write the stream data of workbook to the root directory
    MemoryStream file = new MemoryStream();
    hssfworkbook.Write(file);
    return file;
}

void GenerateData()
{
    Sheet sheet1 = hssfworkbook.CreateSheet("Sheet1");

    sheet1.CreateRow(0).CreateCell(0).SetCellValue("This is a Sample");
    int x = 1;
    for (int i = 1; i <= 15; i++)
    {
        Row row = sheet1.CreateRow(i);
        for (int j = 0; j < 15; j++)
        {
            // add you data from the db
            row.CreateCell(j).SetCellValue(x++);
        }
    }
}

void InitializeWorkbook()
{
    hssfworkbook = new HSSFWorkbook();

    ////create a entry of DocumentSummaryInformation
    DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
    dsi.Company = "NPOI Team";
    hssfworkbook.DocumentSummaryInformation = dsi;

    ////create a entry of SummaryInformation
    SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
    si.Subject = "NPOI SDK Example";
    hssfworkbook.SummaryInformation = si;
    }
}
  

检查NPOI版本中的样本......   我希望它有所帮助!

来源:http://go4answers.webhost4life.com/Question/easiest-npoi-codeplex-basically-define-817046.aspx