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