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

Merged Cells from Excel are not reflecting in Grid View

提问人:Rishmitha Tatineni 提问时间:10/30/2023 更新时间:11/1/2023 访问量:21

问:

我有一个格式如下的 excel 我的 excel 文件有 3 列,Emp.Id、员工姓名、职务,每列都合并了两个单元格,然后您有其他未合并的列。

现在,当我导入 excel 文件并在网格视图中显示其内容时,合并的单元格显示为两个单独的单元格,单元格值显示在第一个单元格中,第二个单元格为空 它将合并的单元格显示为两个单独的单元格,而不是合并列,因为两者都是空单元格

我试过这段代码

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

评论


答:

0赞 Shahram Alemzadeh 11/1/2023 #1

1- 如果块是不必要的,则等于: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-要合并单元格,这不是正确的事件,因为需要所有gridview数据。它可以在之后完成RowDataBoundGridAttendance.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++;
                        }
                    }
                }
            }
        }
    }
}

results

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