Asp.net 在导出到Excel时使用电子表格gear将行合并为单个行

Asp.net 在导出到Excel时使用电子表格gear将行合并为单个行,asp.net,webforms,excel-2010,spreadsheetgear,Asp.net,Webforms,Excel 2010,Spreadsheetgear,如果可能,我必须使用电子表格gear控件合并excel的行。只有单列的特定行 我所做的改变是 DataTable dt = (DataTable)ViewState["dtGrid"] System.Random rd = new System.Random(DateTime.Now.Millisecond); int MyValue = rd.Next(1000000, 99999999); s

如果可能,我必须使用电子表格gear控件合并excel的行。只有单列的特定行

我所做的改变是

            DataTable dt = (DataTable)ViewState["dtGrid"]

            System.Random rd = new System.Random(DateTime.Now.Millisecond);
            int MyValue = rd.Next(1000000, 99999999);
            sUniqueName = MyValue.ToString();

            // Create a new workbook.
            SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
            SpreadsheetGear.IRange cells = workbook.Worksheets[0].Cells;

            cells.CopyFromDataTable(dt, SpreadsheetGear.Data.SetDataFlags.None);


            cells.Rows[0, 0, 0, 51].Interior.Color = System.Drawing.Color.Navy;
            cells.Rows[0, 0, 0, 51].Font.Color = System.Drawing.Color.White;
            cells["A:R"].Columns.AutoFit();

            string filename = string.Format("{0}-{1}-{2}", "AOMIndoorInventoryReport", DateTime.Now.ToString("MM-dd-yy"), sUniqueName);
            Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename + ".xls");
            workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8);
            Response.End();

应该添加什么?

您可以通过调用所需单元格上的IRange.()来合并单元格。例如:

cells["A7:A8"].Merge();
cells[8, 0, 22, 0].Merge();

更新: 您询问了如何基于列中具有相同值的相邻行动态合并一系列单元格。要实现这一点,需要循环遍历此列中的每一行,并将每个单元格的值与上一个值进行比较,然后在适当的情况下沿列合并

我在下面提供了一些示例代码,其中演示了一种方法(当然还有许多其他方法)。我必须对您的基础数据和需求做出一些假设,因此可能需要对您的终端进行一些修改。请注意,在界面下使用了一些方便的方法(请参阅IRange(…/(…)/(…)方法),这些方法允许您“交互”两个IRange以创建新的第三个IRange

...

// Create a new workbook and some local variables for convenience.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets[0];
SpreadsheetGear.IRange cells = worksheet.Cells;

// Copy data from DataTable to worksheet
cells.CopyFromDataTable(dt, SpreadsheetGear.Data.SetDataFlags.None);

// Here I am creating an IRange representing the used part of column a and without
// the header row, which I presume is in Row 1, and should not be included in this
// merging routine.
SpreadsheetGear.IRange usedColA = worksheet.UsedRange.Intersect(
    cells["A:A"]).Subtract(cells["1:1"]);

// No point in attempting to merge cells if there's only a single row.
if (usedColA.RowCount > 1)
{
    // Some variables to keep track of the content of the "current" and "previous"
    // cells as we loop through "usedColA".
    string prevCellVal = "";
    string curCellVal = "";

    // We'll use this variable to keep track of ranges which will need to be merged
    // during the routine.  Here I seed "mergeRange" with the first cell in usedColA.
    SpreadsheetGear.IRange mergeRange = usedColA[0, 0];

    // Loop through each cell in the used part of Column A.
    foreach (SpreadsheetGear.IRange cell in usedColA)
    {
        // Get text of current "cell".
        curCellVal = cell.Text;

        // Your screenshot seems to indicate that you don't care about merging empty
        // cells so this routine takes this into account.  Either an empty cell or
        // mismatched text from the "current" and "previous" cell indicate we should
        // merge whatever cells we've accumulated in "mergeRange" and then reset this
        // range to look for more ranges to merge further down the column.
        if (curCellVal.Length == 0 || curCellVal != prevCellVal)
        {
            // Only merge if this range consists of more than 1 cell.
            if (mergeRange.CellCount > 1)
                mergeRange.Merge();

            // Reset merged range to the "current" cell.
            mergeRange = cell;
            prevCellVal = curCellVal;
        }
        // If the current and previous cells contain the same text, add this "cell"
        // to the "mergeRange".  Note the use of IRange.Union(...) to combine two 
        // IRange objects into one.
        else if (curCellVal == prevCellVal)
            mergeRange = mergeRange.Union(cell);
    }

    // One last check for any cells to merge at the very end of the column.
    if (mergeRange.CellCount > 1)
        mergeRange.Merge();
}
...

这很好,但我有动态行,应该合并,就像你在上面的例子中提到的,应该在第一个例子中传递什么,而不是A7和A8谢谢。