导出到Excel

时间:2016-05-19 06:22:15

标签: asp.net webforms excel-2010 spreadsheetgear

我必须使用电子表格齿轮控件合并excel行才有可能。只有特定行的单列

All detail is being included in this screencast

我所做的改变是

            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();

应该添加什么?

1 个答案:

答案 0 :(得分:0)

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

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

<强>更新 您已经询问了如何根据列中具有相同值的相邻行动态合并一系列单元格。要实现这一点,需要循环遍历此列中的每一行,并将每个单元格的值与先前的值进行比较,在适当的时候合并到列中。

我在下面提供了一些示例代码,演示了您可能采用的一种方式(当然还有很多其他方法)。我不得不对您的基础数据和要求做出一些假设,因此可能需要对您的结果进行一些修改。请注意在IRange界面下使用一些方便的方法(请参阅IRange。Intersect(...)/ Subtract(...)/ Union(.. 。)方法),让你进行互动&#34;两个IRanges创造了新的第三个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();
}
...