简体   繁体   English

如何使用Npoi数据集创建Excel电子表格

[英]How to create an excel spreadsheet using a dataset with Npoi

I am completely new to Npoi and I am struggling trying to find some information on how to create a basic spreadsheet from a DataSet. 我是Npoi的新手,我正在努力寻找有关如何从DataSet创建基本电子表格的一些信息。 I have a DataSet being returned that will only ever contain one table I am trying to get the columns and values from these columns into an dynamically generated excel file that I can allow users to download. 我有一个返回的DataSet,它只包含一个表我试图将这些列中的列和值转换为动态生成的excel文件,我可以允许用户下载。

So far I have been able to create the workbook and sheet but I can't figure out how to properly populate sheet using the Npoi.dll 到目前为止,我已经能够创建工作簿和工作表,但我无法弄清楚如何使用Npoi.dll正确填充工作表

My code for creating the workbook (so far) is as follows: 我创建工作簿的代码(到目前为止)如下:

private void CreateWorkbook(DataSet ds)
{
    var table = ds.Tables[0];
    var workbook = new HSSFWorkbook();
    var sheet = workbook.CreateSheet();
    foreach (DataColumn col in table.Columns)
    {
        //seems like I should create the columns for the sheet here

        foreach (DataRow row in table.Rows)
        {
            sheet.CreateRow(); //then populate each column with the approriate data
        }
    }         
}

In addition, and I may be missing something but where is all the documentation Npoi on codeplex mentions, I cannot find anything relevant for Mvc apps dynamically creating a sheet and returning it to the client? 另外,我可能会遗漏一些东西,但是在Codeplex上提到的所有Npoi文档中,我找不到任何与Mvc应用程序相关的动态创建工作表并将其返回给客户端? What I am trying to accomplish is to create create the file and pass it to the client without storing it on the server. 我想要完成的是创建创建文件并将其传递给客户端而不将其存储在服务器上。

I'm sure this is basic but I can't figure out where to look for information. 我确信这是基本的,但我无法弄清楚在哪里寻找信息。 I'd appreciate any suggestions. 我很感激任何建议。

-cheers -干杯

A couple links that got me started: 一些让我入手的链接:

How to read in XLSX data for editing with NPOI 如何读取XLSX数据以便使用NPOI进行编辑

Creating Excel spreadsheets .XLS and .XLSX in C# 在C#中创建Excel电子表格.XLS和.XLSX

And finally at quick overview of basic functions from the POI documentation like reading a named range, collapsing rows, etc. As NPOI is a close match to POI the guide is fairly effective. 最后快速浏览POI文档中的基本功能,例如读取命名范围,折叠行等。由于NPOI与POI非常匹配,因此指南非常有效。

Busy Developers' Guide to HSSF and XSSF Features 繁忙的开发人员HSSF和XSSF功能指南

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

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