Merging Cells in Excel using C#

后端 未结 11 1060
耶瑟儿~
耶瑟儿~ 2020-12-24 01:32

I have a database which contains 5 tables. Each table contains 24 rows and each row contains 4 columns.

I want to display these records in Excel sheet. The heading o

相关标签:
11条回答
  • 2020-12-24 01:51

    You can use Microsoft.Office.Interop.Excel:

    worksheet.Range[worksheet.Cells[rowNum, columnNum], worksheet.Cells[rowNum, columnNum]].Merge();

    You can also use NPOI:

    var cellsTomerge = new NPOI.SS.Util.CellRangeAddress(firstrow, lastrow, firstcol, lastcol);
    _sheet.AddMergedRegion(cellsTomerge);
    
    0 讨论(0)
  • 2020-12-24 02:01

    Using the Interop you get a range of cells and call the .Merge() method on that range.

    eWSheet.Range[eWSheet.Cells[1, 1], eWSheet.Cells[4, 1]].Merge();
    
    0 讨论(0)
  • 2020-12-24 02:02

    This solves the issue in the appropriate way

    // Merge a row
                ws.Cell("B2").Value = "Merged Row(1) of Range (B2:D3)";
                ws.Range("B2:D3").Row(1).Merge();
    
    0 讨论(0)
  • 2020-12-24 02:11
    Excel.Application xl = new Excel.ApplicationClass();
    
    Excel.Workbook wb = xl.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorkshe et);
    
    Excel.Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;
    
    ws.Cells[1,1] = "Testing";
    
    Excel.Range range = ws.get_Range(ws.Cells[1,1],ws.Cells[1,2]);
    
    range.Merge(true);
    
    range.Interior.ColorIndex =36;
    
    xl.Visible =true;
    
    0 讨论(0)
  • 2020-12-24 02:11

    take a list of string as like

    List<string> colValListForValidation = new List<string>();
    

    and match string before the task. it will help you bcz all merge cells will have same value

    0 讨论(0)
  • 2020-12-24 02:14

    You can use NPOI to do it.

    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");
    
    Row row = sheet.createRow((short) 1);
    Cell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of merging");
    
    sheet.addMergedRegion(new CellRangeAddress(
            1, //first row (0-based)
            1, //last row  (0-based)
            1, //first column (0-based)
            2  //last column  (0-based)
    ));
    
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
    
    0 讨论(0)
提交回复
热议问题