使用c#合并Excel中的单元格

时间:2022-07-10 20:22:42

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

我有一个包含5个表的数据库。每个表包含24行,每一行包含4列。

I want to display these records in Excel sheet. The heading of each table is the name of the table, but I am unable to merge the columns for heading.

我想在Excel表格中显示这些记录。每个表的标题都是表的名称,但是我无法合并标题的列。

Please help me.

请帮助我。

9 个解决方案

#1


47  

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

使用Interop,您将获得一个单元格范围,并在该范围上调用. merge()方法。

eWSheet.Range[eWSheet.Cells[1, 1], eWSheet.Cells[4, 1]].Merge();

#2


10  

oSheet.get_Range("A1", "AS1").Merge();

#3


6  

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;

#4


3  

Code Snippet

代码片段

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private Excel.Application excelApp = null;
    private void button1_Click(object sender, EventArgs e)
    {
        excelApp.get_Range("A1:A360,B1:E1", Type.Missing).Merge(Type.Missing);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        excelApp = Marshal.GetActiveObject("Excel.Application") as Excel.Application ;
    }
}

Thanks

谢谢

#5


3  

You can use NPOI to do it.

你可以用NPOI来做。

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

#6


1  

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

#7


1  

You can use Microsoft.Office.Interop.Excel:

您可以使用Microsoft.Office.Interop.Excel:

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

worksheet.Range[工作表。细胞(rowNum columnNum),工作表。细胞[rowNum,columnNum]].Merge();

You can also use NPOI:

你也可以使用NPOI:

var cellsTomerge = new NPOI.SS.Util.CellRangeAddress(firstrow, lastrow, firstcol, lastcol);
_sheet.AddMergedRegion(cellsTomerge);

#8


1  

Worksheet["YourRange"].Merge();

#9


0  

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

在任务之前匹配字符串。它将帮助您bcz所有合并单元将具有相同的价值。

#1


47  

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

使用Interop,您将获得一个单元格范围,并在该范围上调用. merge()方法。

eWSheet.Range[eWSheet.Cells[1, 1], eWSheet.Cells[4, 1]].Merge();

#2


10  

oSheet.get_Range("A1", "AS1").Merge();

#3


6  

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;

#4


3  

Code Snippet

代码片段

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private Excel.Application excelApp = null;
    private void button1_Click(object sender, EventArgs e)
    {
        excelApp.get_Range("A1:A360,B1:E1", Type.Missing).Merge(Type.Missing);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        excelApp = Marshal.GetActiveObject("Excel.Application") as Excel.Application ;
    }
}

Thanks

谢谢

#5


3  

You can use NPOI to do it.

你可以用NPOI来做。

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

#6


1  

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

#7


1  

You can use Microsoft.Office.Interop.Excel:

您可以使用Microsoft.Office.Interop.Excel:

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

worksheet.Range[工作表。细胞(rowNum columnNum),工作表。细胞[rowNum,columnNum]].Merge();

You can also use NPOI:

你也可以使用NPOI:

var cellsTomerge = new NPOI.SS.Util.CellRangeAddress(firstrow, lastrow, firstcol, lastcol);
_sheet.AddMergedRegion(cellsTomerge);

#8


1  

Worksheet["YourRange"].Merge();

#9


0  

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

在任务之前匹配字符串。它将帮助您bcz所有合并单元将具有相同的价值。