My goal is to set the border for the spreadsheet data's contents while leaving the cell's other characteristics alone.
Instead of merely formatting the area of the spreadsheet that contains actual data, the code below formats(sets the border) the entire spreadsheet.
Is there a purpose behind using the formatting throughout the spreadsheet? And can this be overcome in any way?
package learning.selenium.self.begining;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class testing {
public static void main(String[] args) throws Exception {
File myFile = new File("TestFile.xlsx");
Workbook myWorkbook = WorkbookFactory.create(new FileInputStream(myFile));
Sheet mySheet = myWorkbook.getSheetAt(0);
Iterator<Row> r = mySheet.rowIterator();
while (r.hasNext()) {
Row myR = r.next();
Iterator<Cell> c = myR.cellIterator();
while (c.hasNext()) {
Cell myC = c.next();
System.out.println("precessing (" + myR.getRowNum() + "," + myC.getColumnIndex() + ")");
CellStyle s = myC.getCellStyle();
s = myC.getCellStyle();
s.setBorderBottom(BorderStyle.THIN);
s.setBorderTop(BorderStyle.THIN);
s.setBorderLeft(BorderStyle.THIN);
s.setBorderRight(BorderStyle.THIN);
myC.setCellStyle(s);
}
}
FileOutputStream fos = new FileOutputStream(myFile);
myWorkbook.write(fos);
fos.close();
}
}