在工作表2的Excel中设置标题

我有一些问题,为我的工作表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的新标题创build新的Excel应用程序吗? 但我没有任何意义。 任何人都可以帮助我吗? 谢谢你们

你可以写任何你想要的单元格,包括标题单元格。 只需select您想要插入标题的工作表。

 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";