// Create a new workbook HSSFWorkbook workbook = new HSSFWorkbook(); // Add a sheet to the workbook HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("Sheet1"); // Add some cells to the sheet HSSFRow row = (HSSFRow)sheet.CreateRow(0); HSSFCell cell = (HSSFCell)row.CreateCell(0); cell.SetCellValue("Hello World!"); // Create a named range for the first cell HSSFName namedRange = workbook.CreateName(); namedRange.NameName = "FirstCell"; namedRange.RefersToFormula = "Sheet1!$A$1";
// Open an existing workbook HSSFWorkbook workbook = new HSSFWorkbook(File.OpenRead("Test.xls")); // Get the first sheet in the workbook HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0); // Create a named range for the first column HSSFName namedRange = workbook.CreateName(); namedRange.NameName = "FirstColumn"; namedRange.RefersToFormula = "Sheet1!$A$1:$A$" + sheet.LastRowNum; // Set the width of the first column sheet.SetColumnWidth(0, 5000);In this example, an existing workbook is opened, and the first sheet and named range called "FirstColumn" are created. The named range refers to all cells in column A, from cell A1 to the last cell in the first column. Finally, the width of the first column is set to a custom value of 5000. The NPOI.HSSF.UserModel package library provides functionality for working with Microsoft Excel files in the XLS format.