I want to insert a string to a Matrix of Cells in Excel, Using Apache POI, Here is the code :
for (int j=0;j<13;j++){
for(int i=6;i<30;i++)
{
XSSFRow row=sheet6.createRow(i);
cell0=row.createCell(j);
cell0.setCellValue("SampleRules_CI_01");
}
}
After executing my program, I get the column j=12 correctly filled, but the other columns (from 0 to 11) are still empty
You are replacing the row each time you get back around to the same value of i, when you call the createRow method. This has the effect of erasing whatever was on that row already, and only the last column doesn't get erased this way.
First, inside your i for loop, call the getRow method to see if it already exists. If it returns null, then the row doesn't exist yet and then you can call createRow.
XSSFRow row = sheet6.getRow(i);
if (row == null)
{
row = sheet6.createRow(i);
}
Related
I am getting values from my program and I want to save them in an Excel file, I am using poi library, I should have 9 sheets and in each single sheet I have to obtain a matrix (row* columns) in my case (whatever number of rows * 13 columns),,but I only obtain 1 sheet named by the last name in the array and filled only by the column number 13
Here is the method
public static void writeExcelSheet(String categoryName) throws IOException {
Workbook workbook=new HSSFWorkbook();
Sheet sheet=workbook.createSheet(categoryName);
int allength=arraylistCategory.size();
for(int row=0;row<allength;row++){
for(int column=0;column<13;column++){
Cell cell=sheet.createRow(row).createCell(column);
cell.setCellValue(arraylistCategory.get(row)[column]);
}
}
FileOutputStream outputStream=new FileOutputStream("mel coef excel sheet.xls");
workbook.write(outputStream);
outputStream.close();
arraylistCategory.clear();
}
can you please tell me whats missing or wrong,thanks in advance
I should have 9 sheets
Your method only creates one sheet in the workbook. Assuming you are attempting to call this method 9 times, you are re-creating a new, blank Workbook each time you call this method, overwriting the file each time. This explains why you are only getting the last name in the array.
Instead, create the Workbook once, then pass it into this method so you can create sheets on the same Workbook. Then after the last call to this method, then write it to the FileOutputStream.
and filled only by the column number 13
You have a similar problem here. You are creating the row with createRow(row) for each column. When you do this, you are overwriting whatever row was there with a new, empty Row, erasing all cell values except for the last value. Create the Row outside the inner for loop but inside the outer for loop, and use it inside the inner for loop.
for(int row = 0; row < allength; row++){
Row currentRow = sheet.createRow(row);
for(int column = 0; column < 13; column++){
Cell cell = currentRow.createCell(column);
cell.setCellValue(arraylistCategory.get(row)[column]);
}
}
I have a Jtable that I've loaded with values from a .csv file, but it creates a new row for every instance of 5/13/2013 that shows up in the file, like so:
I'd like to remove all rows with this info from the table, but am not sure how to do so. Any suggestions for me?
Here's my code to add the data to the table, if it helps:
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
tableModel.insertRow(tableModel.getRowCount(), values);
}//end of while block`
To reiterate and be completely clear, I want to remove every row that contains "5/13/2013" from the table completely. And I'm using the deafault table model, by the way.
I ended up applying a for loop and an if statement to get this working for me.
//remove rows with instances of "5/13/2013"
for (int i = 0; i < tableModel.getRowCount(); i++) {
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}//end of if block
}//end of for block
It's worked well for me and has gotten rid of each of those rows. Hopefully this may help someone else out.
while(i < tableModel.getRowCount()) {
//if the value at (i, 0) match the specified value the row will be removed
/*
if the row removed all row will move up and their index will be changed
so you have to add a condition if the value from the table doesn't match
the specified value the iterator i will iterate by one to jump to the next
row
*/
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}else {
++i;
}
}
I am using Aspose to read a CSV file.
I do not beforehand know the number of cells for each row of the file, but I will need to know it for further processing.
Unfortunately, I see no way to find out the number of cells in a CSV row.
Imagine the following row in the CSV file. It contains 7 cells, 4 of which are empty:
1,2,,4,,,
Using
row.iterator();
Aspose will only return 3 cells, as it ignores all empty cells.
As an alternative, I now do the following:
Cell lastCell = row.getLastCell();
int count = 0;
do {
cell = row.getCellOrNull(count);
String cellValue = cell == null ? "" : cell.getStringValueWithoutFormat();
//do something with the cell value...
count++;
} while (cell == null || ! lastCell.equals(cell));
This works better, as it returns the first 4 cells.
However, it still ignores the last 3 cells .
Is there any way to get information about the missing cells?
(It would be sufficient for me if Aspose could return the original Row as a String - I could then count the number of commas and find out the number of cells this way)
You may use Worksheet.getCells().getMaxDisplayRange() method to get the maximum display range.
Please consider this CSV. If you open it in MS-Excel and check the last cell, you will find it is Q2
Book1.csv
2,,,,1,,,,,,,,,,,,,
,,3,,,,
Aspose.Cells returns the same via the following code.
TxtLoadOptions opts = new TxtLoadOptions(LoadFormat.CSV);
Workbook wb = new Workbook("Book1.csv", opts);
Worksheet ws = wb.getWorksheets().get(0);
Range rng = ws.getCells().getMaxDisplayRange();
System.out.println(rng);
Here is the console output of the code.
Console Output
Aspose.Cells.Range [ Sheet1!A1:Q2 ]
Note: I am working as Developer Evangelist at Aspose
I am trying to get the Cell from the Row by a Cell reference and I have a problem. For example I have:
row.getCell(CellReference.convertColStringToIndex("B"));
That works fine if the Column index is 1, but if Column was deleted so the B Column index became 2 and the method: CellReference.convertColStringToIndex("B") is still converting it to 1 in which case I can't get my Column, I am getting null.
So the question is how can I get the Column from the Row depending on Cell identifier which is a letter?
Take a look at my answer to a previous question:
You probably want to use the CellReference utility class to help you out. It offers conversion between Excel style letter+number references, and POI style 0-based rows+columns. When using it, you can do something like:
Sheet sheet = workbook.getSheet("MyInterestingSheet");
CellReference ref = new CellReference("B12");
Row r = sheet.getRow(ref.getRow());
if (r != null) {
Cell c = r.getCell(ref.getCol());
}
That will let you find the cell at a given Excel-style reference (if it's defined, else null)
If I've got an list of parameters 'x,y,z' that aren't sorted, is there a straightforward way to write them to particular cells in an excel document created with POI, as though the first two parameters are X and Y coordinates?
For example, I have rows like:
10,4,100
Is it possible to write the value '100' in the cell at the 10th row, 4th column?
Looking at the documentation, it looks straightforward to iterate values into the next row, but I can't see any way of creating a fixed number of rows and columns and writing particular values to only certain cells.
Any advice or suggestions would be appreciated, thanks!
Sure, it's very easy, just remember that POI is 0 based not 1 based in addressing. Assuming you want to write to the 10th row, 4th column, you'd do something like
Row r = sheet.getRow(9); // 10-1
if (r == null) {
// First cell in the row, create
r = sheet.createRow(9);
}
Cell c = r.getCell(3); // 4-1
if (c == null) {
// New cell
c = r.createCell(3, Cell.CELL_TYPE_NUMERIC);
}
c.setCellValue(100);