Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 25-Mar-20 14:16pm
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");
Member 15694833 2-Jul-22 21:02pm    
This comment ended up in the Mel Gibson movie "Hot Seat" after it was copy/pasted into this gif: https://www.shutterstock.com/video/clip-1071725005-access-granted-sign-security-guard-control-successful

1 solution

Hello,

Here is a sample based on apache POI.
Java
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,
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900