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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/532199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:03:48  来源:igfitidea点击:

Merging Cells in Excel using C#

c#excel

提问by

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.

请帮我。

回答by C. Ross

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

回答by C. Ross

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

谢谢

回答by Imamul Karim Tonmoy

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

回答by Vipswelt

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 所有合并单元格将具有相同的值

回答by Ahmad naser

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;

回答by Shalin Jirawla

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

回答by Sobhan

You can use Microsoft.Office.Interop.Excel:

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

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

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

You can also use NPOI:

您还可以使用 NPOI:

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

回答by Ana Carolina Manzan

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

回答by Subha8

Worksheet["YourRange"].Merge();

回答by Ly Thanh Ngo

Try it.

尝试一下。

ws.Range("A1:F2").Merge();