【问题标题】:How to export SQL Server Database Data into Excel in Silverlight 3如何在 Silverlight 3 中将 SQL Server 数据库数据导出到 Excel
【发布时间】:2012-10-07 07:34:34
【问题描述】:

我是 Silverlight 的新手。

我将 VS-2008 与 Silverlight 3、SQL Server 2005 一起使用。

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

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

谁能指导我怎么做?

提前谢谢,

【问题讨论】:

    标签: silverlight-3.0


    【解决方案1】:

    最简单的方法是使用 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

    【讨论】:

    • 嗨,Hidde,非常感谢您的回复;但是,我不允许在服务器上安装新软件 (NPOI)。服务器位于法国,我在印度。因此,您能否建议我一些不需要安装其他软件的其他方法!??
    • 你可以看看这个:rshelby.com/post/…
    • 嗨隐藏。在您提供的链接中,我们可以将数据从 DataGrid 导出到 Excel,但不能从数据库导出到 Excel。正如我的帖子解释了我的要求,我想要一些将数据从数据库导出到 Excel 的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    相关资源
    最近更新 更多