Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环浏览一个或多个用户标识的数据,并为每个用户创建新的工作表_C#_Asp.net Mvc_Excel_Npoi - Fatal编程技术网

C# 循环浏览一个或多个用户标识的数据,并为每个用户创建新的工作表

C# 循环浏览一个或多个用户标识的数据,并为每个用户创建新的工作表,c#,asp.net-mvc,excel,npoi,C#,Asp.net Mvc,Excel,Npoi,现在,我的代码按用户ID和特定日期范围从数据库返回数据。然后将结果导出到Excel电子表格。如果搜索了多个用户ID,那么我需要让每个用户ID在工作簿中都有自己的工作表。现在所有结果都显示在“工作表1”上。我会循环检查结果并使用if语句检查多个唯一id吗?对于每个id,将创建一个新的工作表并填充数据?我对编程非常陌生,任何帮助都会很好 控制器 [HttpPost] public FileResult Export(ReportPhoneSupportVM model) {

现在,我的代码按用户ID和特定日期范围从数据库返回数据。然后将结果导出到Excel电子表格。如果搜索了多个用户ID,那么我需要让每个用户ID在工作簿中都有自己的工作表。现在所有结果都显示在“工作表1”上。我会循环检查结果并使用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

}

使用以下代码:

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

}

我收到一条错误消息,在var sheet=null时,无法分配给隐式类型的局部变量;还有一个问题,userID是一个字符串,而不是int。这是如何工作的?thanksI添加了整个控制器。我本来应该这么做的我改变了代码。因此,它将涵盖您上面提到的问题。