在Excel中为工作表2设置标题 [英] Set header in Excel for worksheet 2

查看:432
本文介绍了在Excel中为工作表2设置标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设置工作表2的标题时遇到了一些问题。

I habe some problems with setting the header for my worksheet 2

这是我的代码:

            Excel.Application xlApp1 = new Excel.Application();
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet1 = new Excel.Worksheet();
            Excel.Worksheet xlWorkSheet2 = new Excel.Worksheet();
            object misValue = System.Reflection.Missing.Value;

            xlWorkBook = xlApp1.Workbooks.Add(misValue);
            xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); // Sheet1
            xlWorkSheet1.Name = "XX";
            xlWorkSheet2 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); // Sheet2
            xlWorkSheet2.Name = "YY";

            Excel.Range headerRange = xlApp1.get_Range("A1", "V1");
            headerRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {

                xlApp1.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;

            }

如果我为工作表2的新标题创建新的Excel应用程序?但我没有任何意义。任何人都可以帮助我吗?谢谢你们......

Should I make new Excel Application for new header for Worksheet 2? but i doesn't make any sense. Can anyone help me with it? thanks you guys...

推荐答案

您可以写入任何想要的单元格,包括标题单元格。

You can write to any cell you want, including the header cells. Just choose the worksheet in which you want to insert the header.

Excel.Range headerRange = xlWorkSheet1.get_Range("A1", "V1");
headerRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange.Value = "Header text 1";

headerRange = xlWorkSheet2.get_Range("A1", "V1");
headerRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
headerRange.Value = "Header text 2";

这篇关于在Excel中为工作表2设置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆