首页 > 解决方案 > 使用 NPOI 包导出 excel 文件

问题描述

我正在尝试将数据表导出到导出文件。直到现在,只要我对特定目录执行此操作,它就成功了。用户应该能够做什么基本上能够选择目录以下载excel格式的文件。我为导出函数创建了一个单独的类,如下所示:

[Authorize]
public class Export
{
    

    //Export Method
    public static void ExportExcel(string fileName, decimal projectId, ModelContext context)
    {
        ExcelPackage pck = new ExcelPackage();
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Project");

        var surveys = context.Survey.Where(x => x.ProjectId == projectId)
            .Include(s=> s.Question);

        
        fileName = @("D:\Projects\CodeBooks\Trunk\100-Documents\Codebooks") //it is a specific directory.


        ws.Cells["A1"].Value = "SURVEY_ID";
        ws.Cells["B1"].Value = "PAGE";
        ws.Cells["C1"].Value = "RANKING";
        ws.Cells["D1"].Value = "PARENT_ID";
        ws.Cells["E1"].Value = "ID";
        ws.Cells["F1"].Value = "ITEMTYPE";
        ws.Cells["G1"].Value = "CONTROLTYPE";
        ws.Cells["H1"].Value = "DISPLAYTYPE";
        ws.Cells["J1"].Value = "VARIABLE_DESCRIPTION";
        ws.Cells["K1"].Value = "VARIABLE_NAME";
        ws.Cells["L1"].Value = "VARIABLE_LABEL";
        ws.Cells["M1"].Value = "VALUE_LABEL_ID";
        ws.Cells["N1"].Value = "MISSING_VALUE_ID";
        ws.Cells["O1"].Value = "IS_REQUIRED";
        ws.Cells["P1"].Value = "DATA_TYPE";
        ws.Cells["Q1"].Value = "VARIABLE_LENGTH";
        ws.Cells["R1"].Value = "NR_OF_DECIMALS";
        ws.Cells["S1"].Value = "COMMENT";
        ws.Cells["T1"].Value = "COMMENT_TYPE";
        ws.Cells["U1"].Value = "SHOW_ALL_FOR_STUDY";
        ws.Cells["V1"].Value = "ANSWER_FONT";
        ws.Cells["W1"].Value = "IS_DELETE_QUESTION";
        ws.Cells["X1"].Value = "IS_STOP_QUESTION";
        ws.Cells["Y1"].Value = "ROUTING_TYPE";
        ws.Cells["Z1"].Value = "ROUTING_VALUE";
        ws.Cells["AA1"].Value = "SHOW";

        int rowStart = 2;
        foreach (var survey in surveys)
        {

            foreach (var item in survey.Question)
            {
                ws.Cells[String.Format("A{0}", rowStart)].Value = survey.IdSurvey;
                ws.Cells[String.Format("B{0}", rowStart)].Value = item.Page;
                ws.Cells[String.Format("C{0}", rowStart)].Value = item.Ranking;
                ws.Cells[String.Format("D{0}", rowStart)].Value = item.ParentId;
                ws.Cells[String.Format("E{0}", rowStart)].Value = item.IdQuestion;
                ws.Cells[String.Format("F{0}", rowStart)].Value = item.Itemtype;
                ws.Cells[String.Format("G{0}", rowStart)].Value = item.Controltype;
                ws.Cells[String.Format("H{0}", rowStart)].Value = item.Displaytype;
                ws.Cells[String.Format("J{0}", rowStart)].Value = item.VariableDescription;
                ws.Cells[String.Format("K{0}", rowStart)].Value = item.VariableName;
                ws.Cells[String.Format("L{0}", rowStart)].Value = item.VariableLabel;
                ws.Cells[String.Format("M{0}", rowStart)].Value = item.ValueLabelId;
                ws.Cells[String.Format("N{0}", rowStart)].Value = item.MissingValueId;
                ws.Cells[String.Format("O{0}", rowStart)].Value = item.IsRequired;
                ws.Cells[String.Format("P{0}", rowStart)].Value = item.DataType;
                ws.Cells[String.Format("Q{0}", rowStart)].Value = item.VariableLength;
                ws.Cells[String.Format("R{0}", rowStart)].Value = item.NrOfDecimals;
                ws.Cells[String.Format("S{0}", rowStart)].Value = item.Comment;
                ws.Cells[String.Format("T{0}", rowStart)].Value = item.CommentType;
                ws.Cells[String.Format("U{0}", rowStart)].Value = item.ShowAllForStudy;
                ws.Cells[String.Format("V{0}", rowStart)].Value = item.AnswerFont;
                ws.Cells[String.Format("W{0}", rowStart)].Value = item.IsDeleteQuestion;
                ws.Cells[String.Format("X{0}", rowStart)].Value = item.IsStopQuestion;
                ws.Cells[String.Format("Y{0}", rowStart)].Value = item.RoutingType;
                ws.Cells[String.Format("Z{0}", rowStart)].Value = item.RoutingValue;
                ws.Cells[String.Format("AA{0}", rowStart)].Value = item.Show;
                rowStart++;
            }
            ws.Cells["A:AZ"].AutoFitColumns();

            
        }


        pck.SaveAs(new FileInfo(fileName));

    }
}

然后我在附属控制器中调用此方法:

        public IActionResult Export(IFormCollection form, decimal? id)
    {
        var project = _context.Project.First(x => x.ProjectId == id).ProjectId;
        var fileName = form["CodeBoek.xlsx"];
        
        Models.Export.ExportExcel(fileName, project, _context);
        return RedirectToAction(nameof(Index));
    }

此操作方法也在关联视图中被调用。从那时起我就被困住了,我想得到一些帮助。我试图应用各种方法,但每次我得到一个错误。

标签: c#excelasp.net-corenpoi

解决方案


推荐阅读