如何在asp.net mvc

时间:2018-01-10 06:32:55

标签: closedxml

当我在closedxml中使用merge时,excel文件需要恢复并删除表格,我如何在closedxml中创建带有合并单元格的excel文件而没有警告?

我的代码:

 var datatable = DivExportToExcelType1(dataDto);
 var ws = workBook.Worksheets.Add(datatable, company.Value);

private DataTable DivExportToExcelType1(IList<DpsDto> data)
    {
        List<PeriodInfo> result = new List<PeriodInfo>();
        result = GetDivPeriodInfo(data);

        System.Data.DataTable table = new System.Data.DataTable();
        var column = new DataColumn() { ColumnName = DataStoreInitializer.spaceString + DataStoreInitializer.ColumnTitle };
        table.Columns.Add(column);



        var row = table.NewRow();
        row[0] = DataStoreInitializer.Dividend;
        table.Rows.Add(row);


        foreach (var date in result)
        {
            //var persianDate = date.PeriodEndToDate.ToPersianDate();
            column = new DataColumn(date.ColumnHeader);
            table.Columns.Add(column);

            int rowIndex = 0;
            table.Rows[rowIndex][column] = data.FirstOrDefault(x => x.PeriodEndToDate == date.PeriodEndToDate
                && x.PublishDateTime == date.PublisherDateTime).Value;
            if (table.Rows[rowIndex][column] == null)
                table.Columns.Remove(column);
        }

        return table;
    }

1 个答案:

答案 0 :(得分:2)

您必须按标题创建工作表,然后将数据表和工作表发送到新功能并创建自定义excelFile

var datatable = DivExportToExcelType1(dataDto);
 var ws = workBook.Worksheets.Add(company.Value);
ExportToExcelType(ws, datatable);

 private void ExportToExcelType(IXLWorksheet ws, DataTable datatable)
        {
            var rows = datatable.Rows;
            var Cols = datatable.Columns;



            for (int col = 1; col <= Cols.Count; col++)
            {
                //create your cols
            }

            for (int row = 1; row <= rows.Count ; row++)
            {
               //create your rows
            }


            ws.Range("range your cells").Merge(); //for example ws.Range("A11:A19")
        }