Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

java - Redefine Named Excel Range then Save using Apache POI

Using Apache POI, I'm able to find a named range:

XSSFName[] ranges = new XSSFName[workbook.getNumberOfNames()];
for (int i = 0; i < _wb.getNumberOfNames(); i++)
    ranges[i] = workbook.getNameAt(i);

With that, I'm able to cell an AreaReference:

AreaReference area = new AreaReference(ranges[0].getRefersToFormula());

And then finally I can get all the cells within that range:

CellReference[] cells = area.getAllReferencedCells();

That all works just fine. Burt I have a use case where I have to redefine the area that the range covers. Is there a way to do that? I notice that the range.getRefersToFormula() method return a String, something like MySheet!$A$1:$B$8. There is a range.setRefersToFormula(String formula), but I've got to believe there's a way other than resorting to writing an excel range formula parser on my own. Is there no way to generate an AreaReference with a set to Cell references of something more type-safe? Do I actually have to generate a String to represent the new range? I would think there would be API somewhere to help me with this but I can't seem to find it.

Update

I found some API, but it doesn't seem to work, at least it doesn't save properly. Here's what I did.

AreaReference newArea = new AreaReference(firstCell, lastCell);
ranges[0].setRefersToFormula(newArea.formatAsString())

It seems to set the formula correctly, but when I stream the workbook back out to disk, the range is completely wrong.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can update the existing Reference and set it as per your requirement. Suppose the reference contains TestSheet!$A$1:$B$8and you want to change it to MySheet!$B$5:$C$12

For any cell, say "B5", at runtime,

cell.getReference(); 

will give you cell reference (like in example... it will return you "B5")

char startCellColRef = cell.getReference().toString().charAt(0); 

will give you the Column Reference (will give you "B" if the current cell is B5). Now

int startCellRowRef = cell.getReference().toString().charAt(1);

will give you Row Index (will give you "5" if the current cell is B5).

By the same way you can get your start and end cell references (say B5 and C12).

Now comes how can I update the existing references. Just update its value with newly created reference string

Name reference = wb.getName("NameReferenceInExcelSheet");
referenceString = sheetName+"!$"+startCellColRef+"$"+startCellRowRef+":$"+endCellColRef+"$"+endCellRowRef;
reference.setRefersToFormula(referenceString);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...