循环访问一个或多个用户的数据,并为每个用户创建新的工作表

时间:2014-06-22 04:21:34

标签: c# asp.net-mvc excel npoi

现在我的代码通过userId和特定日期范围从数据库返回数据。然后将结果导出到Excel电子表格。如果搜索了多个UserId,那么我需要让每个UserId在工作簿中都有自己的工作表。现在,所有结果都显示在"工作表1和#34;上。我是否会遍历结果并使用if语句来检查多个唯一ID?对于每个id,将创建一个新的工作表并填充数据?我对编程很陌生,任何帮助都会很棒。

控制器

 [HttpPost]
    public FileResult Export(ReportPhoneSupportVM model)
    {
        ReportPhoneSupportResultTypedView results = new ReportPhoneSupportResultTypedView();
        string[] userIds = model.UserId.Split(',');
        foreach (string userId in userIds)
        {
            int iUserId = 0;
            if (Int32.TryParse(userId, out iUserId))
            {

                RetrievalProcedures.FetchReportPhoneSupportResultTypedView(results, model.FromDate, model.ToDate, iUserId);
            }
        }



var ExcelResults = results;  

    //Create new Excel workbook
    var workbook = new HSSFWorkbook();

    //Create new Excel sheet
    var sheet = workbook.CreateSheet();

    //(Optional) set the width of the columns
    sheet.SetColumnWidth(0, 10 * 256);
    sheet.SetColumnWidth(1, 50 * 256);
    sheet.SetColumnWidth(2, 50 * 256);
    sheet.SetColumnWidth(3, 50 * 256);

    //Create a header row
    var headerRow = sheet.CreateRow(0);

    //Set the column names in the header row
    headerRow.CreateCell(0).SetCellValue("ActivityDate");
    headerRow.CreateCell(1).SetCellValue("Assignment");
    headerRow.CreateCell(2).SetCellValue("Action");
    headerRow.CreateCell(3).SetCellValue("ToFrom");
    headerRow.CreateCell(2).SetCellValue("Result");
    headerRow.CreateCell(3).SetCellValue("Description");

    //(Optional) freeze the header row so it is not scrolled
    sheet.CreateFreezePane(0, 1, 0, 1);

    int rowNumber = 1;

    //Populate the sheet with values from the grid data
    foreach (ReportPhoneSupportResultRow ER in ExcelResults)
    {
        //Create a new row
        var row = sheet.CreateRow(rowNumber++);

        //Set values for the cells
        row.CreateCell(0).SetCellValue(ER.ActivityDate);
        row.CreateCell(1).SetCellValue(ER.Assignment);
        row.CreateCell(2).SetCellValue(ER.Action);
        row.CreateCell(3).SetCellValue(ER.ToFrom);
        row.CreateCell(2).SetCellValue(ER.Result);
        row.CreateCell(3).SetCellValue(ER.Description);
    }

    //Write the workbook to a memory stream
    MemoryStream output = new MemoryStream();
    workbook.Write(output);

    //Return the result to the end user

    return File(output.ToArray(),   //The binary data of the XLS file
        "application/vnd.ms-excel", //MIME type of Excel files
        "GridExcelExport.xls");     //Suggested file name in the "Save as" dialog which will be displayed to the end user

}

1 个答案:

答案 0 :(得分:0)

使用以下代码:

var ExcelResults = results; 
string userID = "";
int rowNumber = 1;

//Create new Excel workbook
var workbook = new HSSFWorkbook();
var? sheet = null;

//Populate the sheet with values from the grid data
foreach (ReportPhoneSupportResultRow ER in ExcelResults)
{
    if (ER.UserID != userID)
    {
        //Create new Excel sheet
        sheet = workbook.CreateSheet();

        //(Optional) set the width of the columns
        sheet.SetColumnWidth(0, 10 * 256);
        sheet.SetColumnWidth(1, 50 * 256);
        sheet.SetColumnWidth(2, 50 * 256);
        sheet.SetColumnWidth(3, 50 * 256);

        //Create a header row
        var headerRow = sheet.CreateRow(0);

        //Set the column names in the header row
        headerRow.CreateCell(0).SetCellValue("ActivityDate");
        headerRow.CreateCell(1).SetCellValue("Assignment");
        headerRow.CreateCell(2).SetCellValue("Action");
        headerRow.CreateCell(3).SetCellValue("ToFrom");
        headerRow.CreateCell(2).SetCellValue("Result");
        headerRow.CreateCell(3).SetCellValue("Description");

        //(Optional) freeze the header row so it is not scrolled
        sheet.CreateFreezePane(0, 1, 0, 1);

        userID = ER.UserID; 
        rowNumber = 1;
    }

    //Create a new row
    var row = sheet.CreateRow(rowNumber++);

    //Set values for the cells
    row.CreateCell(0).SetCellValue(ER.ActivityDate);
    row.CreateCell(1).SetCellValue(ER.Assignment);
    row.CreateCell(2).SetCellValue(ER.Action);
    row.CreateCell(3).SetCellValue(ER.ToFrom);
    row.CreateCell(2).SetCellValue(ER.Result);
    row.CreateCell(3).SetCellValue(ER.Description);
}

//Write the workbook to a memory stream
MemoryStream output = new MemoryStream();
workbook.Write(output);

//Return the result to the end user

return File(output.ToArray(),   //The binary data of the XLS file
    "application/vnd.ms-excel", //MIME type of Excel files
    "GridExcelExport.xls");     //Suggested file name in the "Save as" dialog which will be displayed to the end user

}