Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name a cell with POI

In normal excel one can name a cell (or a range) using the little text box in the top left part of the sheet, so for example one could name a cell from H13 to "total".

Is there a way to do that through Apache POI?

like image 817
Victor Blaga Avatar asked Aug 19 '11 11:08

Victor Blaga


1 Answers

From the POI 'Busy Developer's User Guide'

// setup code
String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(sname);
sheet.createRow(0).createCell((short) 0).setCellValue(cvalue);

// 1. create named range for a single cell using areareference
Name namedCell = wb.createName();
namedCell.setNameName(cname);
String reference = sname+"!A1:A1"; // area reference
namedCell.setRefersToFormula(reference);
like image 129
Wee Shetland Avatar answered Oct 03 '22 02:10

Wee Shetland