I'm working in a project in Java and I Need to create one xls file with some information. So, depending on the amount of information, I need to create automatically Rows and cells to put this information..
Example: if the input documents has 13 Site Information, I need to create 13 Rows with 4 cell.. How I do it? .. my attempt to code:
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
int numberrows = Integer.parseInt(JOptionPane.showInputDialog(null, "numbers of sites??"));
String siteName = JOptionPane.showInputDialog(null, "Site name");
String rncname = JOptionPane.showInputDialog(null, "RncName");
for (int i = 0; i < numberrows; i++) {
HSSFRow linha = (HSSFRow) sheet.createRow(i);
linha.createCell((short) i ).setCellValue(siteName);
linha.createCell((short) i ).setCellValue(rncname);
}
Thanks in advance..
Implementing the Row Insert. For inserting m rows in the middle of an existing Excel sheet, all the rows from the insertion point to the last row should be moved down by m rows. In this step, we get the last row number by using the getLastRowNum() method and shift the rows using the shiftRows() method.
getPhysicalNumberOfCells. Gets the number of defined cells (NOT number of cells in the actual row!). That is to say if only columns 0,4,5 have values then there would be 3.
getSheet("sheet1"); int rows=sh. getLastRowNum();
Can you not just do something simple like:
int nextRow = 12;
Row r = sheet.getRow(nextRow);
if (r == null) {
r = sheet.createRow(nextRow);
}
Cell c = r.getCell(2, Row.CREATE_NULL_AS_BLANK);
c.setCellValue("My String");
nextRow++;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With