如何在Excel中更改工作表名称?

时间:2014-04-30 09:11:54

标签: c# .net excel npoi

我想使用自定义名称表将一些数据导出到Excel。

如何从C#更改工作表名称?

这是我的代码:

var workbook = new HSSFWorkbook();
var font = workbook.CreateFont();
font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD;
sheet.SetColumnWidth(0, 10 * 256);
sheet.SetColumnWidth(1, 30 * 256);
headerRow.CreateCell(0).SetCellValue("Column1");
headerRow.CreateCell(1).SetCellValue("Column2");

int rowNumber = 1;

//Populate the sheet with values from the grid data
foreach (var m in model)
{
    //Create a new row
    var row = sheet.CreateRow(rowNumber++);
    //Set values for the cells
    row.CreateCell(0).SetCellValue(m.Column1);
    row.CreateCell(1).SetCellValue(m.Column2);
}

1 个答案:

答案 0 :(得分:2)

Workbook workbook = new HSSFWorkbook();
Worksheet sheet = (Worksheet)workbook.Worksheets[index];
// you can also use the current worksheet name here

要更改名称,您必须写

sheet.Name = "TheNewWorksheetName";