Hello Java Developers,
Here's a straight question. How would I be able to set the Cell's formula within a loop in Apache POI?
Here's an information to support my question:
Illustration 1 : Cell C1 has the formula of SUM(C6:C7)
which can be done by simply hard coding the formula myCell.setCellFormula("SUM(C6:C7)")
.
On the other hand:
Illustration 2 : This is my desired output. I want to set a formula on Cells C1, D1, and E1
for example. How can this be done within a loop?
Why would I want it within a loop?
- Hard-coding would be tiresome. 3 cells are just and example. Currently I have hundreds of cells waiting for their formula.
- Row count are not specified, it will be based on the number of rows created by the program.
What I have done?
- Googled.
- Searched for similar question.
- Apache POI's column is represented by an Integer (Number of cells within a row).
The formula requires Alpha C2 (Column C, Row 2). I tried to loop through the Spreadsheet and realized that if I continue, the formula will result to SUM(26:27)
(Where: 2 = Column C, and Row 6(Base 1: rowCount + 1) which I want to be SUM(C6:C7)
I have finally solved the problem.
CellReference.convertNumToColString(myColumnNumber)
will return the column name
What have you tried so far (code)?
Try something like this (but you have to put more thought into it when you get past column Z:
int col=2;
int row=6;
char ccol = (char) (('A'+col)&0xff);
String formula=String.format("SUM(%c%d:%c%d)", ccol, row, ccol, row+1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.