Excel 中的合并单元格未反映在网格视图中

问题描述 投票:0回答:1

我有一个具有以下格式的Excel My excel file has 3 columns Emp.Id, Employee Name, Designation each of which have two cells merged and then you have other columns which are not merged.

现在,当我导入 Excel 文件并在网格视图中显示其内容时,合并的单元格显示为两个单独的单元格,单元格值显示在第一个单元格中,第二个单元格为空 It is showing merged cell as two separate cells and instead merging the column sunday as both are empty cells

我试过这个代码

protected DataTable YourExcelFileProcessingMethod(Stream excelFileStream)
{
    ExcelPackage.LicenseContext = LicenseContext.Commercial;

    using (ExcelPackage package = new ExcelPackage(excelFileStream))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets[0];

        DataTable dt = new DataTable();

        for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
        {
            dt.Columns.Add("Column" + col);
        }

        for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
        {
            DataRow newRow = dt.NewRow();

            for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
            {
                ExcelRange cell = worksheet.Cells[row, col];

                if (cell.Merge)
                {
                    
                    newRow[col - 1] = cell.Merge && cell.Start.Row == row ? cell.Text : cell.Text;
                }
                else
                {
                    
                    newRow[col - 1] = cell.Text;
                }
            }

            dt.Rows.Add(newRow);
        }

        return dt;
    }
}

protected void btnImportData_Click(object sender, EventArgs e)
{
    if (FileUploadAttendance.HasFile)
    {
        HttpPostedFile file = FileUploadAttendance.PostedFile;

        if (file.FileName.EndsWith(".xls") || file.FileName.EndsWith(".xlsx") || file.FileName.EndsWith(".xlsm"))
        {
            DataTable dt = YourExcelFileProcessingMethod(file.InputStream);

            GridAttendance.DataSource = dt;
            GridAttendance.DataBind();
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "FailureAlert", "alert('Only Excel files are accepted');", true);
        }
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "FailureAlert", "alert('Please Select a file to import');", true);
    }
}


protected void GridAttendance_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int rowIndex = GridAttendance.Rows.Count - 2; rowIndex >= 0; rowIndex--)
    {
        GridViewRow gvRow = GridAttendance.Rows[rowIndex];
        GridViewRow gvPreviousRow = GridAttendance.Rows[rowIndex + 1];
        for (int cellCount = 0; cellCount < gvRow.Cells.Count;
                                                      cellCount++)
        {
            if (gvRow.Cells[cellCount].Text ==
                                   gvPreviousRow.Cells[cellCount].Text)
            {
                if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
                {
                    gvRow.Cells[cellCount].RowSpan = 2;
                }
                else
                {
                    gvRow.Cells[cellCount].RowSpan =
                        gvPreviousRow.Cells[cellCount].RowSpan + 1;
                }
                gvPreviousRow.Cells[cellCount].Visible = false;
            }
        }
    }
}   
c# asp.net excel gridview
1个回答
0
投票

1- 这个 if 块是不必要的,等于

newRow[col - 1] = cell. Text;
:

if (cell. Merge)
                {
                    newRow[col - 1] = cell.Merge && cell.Start.Row == row ? cell. Text : cell. Text;
                }
                else
                {
                    newRow[col - 1] = cell. Text;
                }

2- 要合并单元格,

RowDataBound
不是正确的事件,因为需要所有 gridview 数据。之后就可以完成
GridAttendance.DataBind();

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Bind();
    }
}

private void Bind()
{
    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
    string filepath = Server.MapPath("~/App_Data/77388146.xlsx");
    DataTable dt = new DataTable();
    using (FileStream stream = File.OpenRead(filepath))
    {
        using (ExcelPackage package = new ExcelPackage(stream))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
            for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
            {
                dt.Columns.Add(worksheet.Columns[col].Range.Start.Address.Replace("1",""));
            }
            for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
            {
                DataRow newRow = dt.NewRow();
                for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
                {
                    newRow[col - 1] = worksheet.Cells[row, col].ToText();
                }
                dt.Rows.Add(newRow);
            }
            gv.DataSource = dt;
            gv.DataBind();

            for (int col = 1; col <= worksheet.Dimension.Columns; col++)
            {
                for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
                {
                    ExcelRange cell = worksheet.Cells[row, col];
                    if (cell.Merge && cell.Value != null)
                    {
                        gv.Rows[row - 1].Cells[col - 1].RowSpan++;
                        int start_row = row;
                        row++;
                        while (
                            worksheet.Cells[row, col].Merge &&
                            worksheet.Cells[row, col].Value == null &&
                            row <= worksheet.Dimension.End.Row)
                        {
                            gv.Rows[start_row - 1].Cells[col - 1].RowSpan++;
                            gv.Rows[row - 1].Cells[col - 1].Visible = false;
                            row++;
                        }
                    }
                }
            }
        }
    }
}

这是原始代码,可能需要微调。

© www.soinside.com 2019 - 2024. All rights reserved.