Click here to Skip to main content
14,486,704 members
Rate this:
Please Sign up or sign in to vote.
See more:
First i have arraylist data like 1,2,3,4,5,6,7,8
after i click the button it will put the arraylist data to my excel database.
But the arraylist data wouldn't start from column A but start from column F in the excel

Before that, i want to using excel to be my database to store some data. This is my first time i using excel for my database
Posted
Updated 5 days ago
Comments
Nountylegre 11-Apr-19 10:06am
   
Here is another sample based on this java excel library:

List<integer> data = new ArrayList<integer>();
data.add(1);
data.add(2);
data.add(3);
// ...

SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

ExcelFile workbook = ExcelFile.load("input.xlsx");
ExcelWorksheet worksheet = workbook.getWorksheet(0);
ExcelColumn column = worksheet.getColumn("F");

for (int i = 0; i < data.size(); i++) {
Integer value = data.get(i);
column.getCell(i).setValue(value);
}

workbook.save("output.xlsx");

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Hello,

Here is a sample based on apache POI.
public void fillData(int[] intArr)
{
    int startCell = 6;     // Corrosponds to F
    HSSFWorkbook workbook = null;
    HSSFSheet sheet = null;
    HSSFCell cell = null;
    HSSFRow sheetRow = null;

    workbook = new HSSFWorkbook();
    sheet = workbook.createSheet("WorkSheet");
    sheetRow = getRow(sheet, 0);
    for (int val : intArr)
    {
        cell = sheetRow.createCell(startCell);
        cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        cell.setCellValue(((Integer) value).doubleValue());
    }
}

/**
 * Helper method to retrieve the numbered row. If row does not exists then
 * a new row is inserted and it's reference is returned.
 * @param sheet The worksheet from which a row reference is sought.
 * @param rowNum the row number
 * @return the reference to the numbered row.
 */
protected final HSSFRow getRow(HSSFSheet sheet, int rowNum)
{
    HSSFRow sheetRow = null ;

    sheetRow = sheet.getRow(rowNum);
    if (null == sheetRow)
        sheetRow = sheet.createRow(rowNum);

    return sheetRow;
}

More info can be found at Apache POI Site[^].

Regards,
   
v3
Comments
yudhistira adytia 1-Apr-13 3:03am
   
@PrasadKhandekar Your code is for create a new excel right? I have the excel file before to export to JTable. I want to add the data from my arraylist to the next column, start from F
Prasad Khandekar 1-Apr-13 6:58am
   
The code for adding values should remain same. Please check the Apache POI documentation for reading existing excel part.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100