数据写在一个Excel列Java中

时间:2023-01-28 20:24:10

Hi i am new to writing data to excel however after doing much research and reading documentation I have done it.

嗨,我是擅长将数据写入excel但是在做了大量研究和阅读文档后,我已经完成了。

My Problem: When string data is stored in my array and written it to excel however it does not put it in one column (which is what I want) it spaces each string out in a column to the right of it in a diagonal descent in my spread sheet. A picture is below.

我的问题:当字符串数据存储在我的数组中并将其写入excel时,它不会将其放在一列中(这就是我想要的)它将每个字符串在对角线下降的右侧列中排出我的电子表格。一张图片如下。

数据写在一个Excel列Java中

What I want is for each string/name to be put in one column under each other. Any suggestions would be appreciated.

我想要的是将每个字符串/名称放在一个列中。任何建议,将不胜感激。

Code Snippet:

    System.out.println("Write data to an Excel Sheet");
    FileOutputStream out = new FileOutputStream("C:/Users/hoflerj/Desktop/text2excel/finish.xlsx");

    XSSFWorkbook workBook = new XSSFWorkbook();
    XSSFSheet spreadSheet = workBook.createSheet("clientid");
    XSSFRow row;
    XSSFCell cell;

    for(int i=0;i<arr.size();i++){
        row = spreadSheet.createRow((short) i);
    cell = row.createCell(i);
    System.out.println(arr.get(i));
    cell.setCellValue(arr.get(i).toString());
    }


    System.out.println("Done");
    workBook.write(out);
    arr.clear();
    out.close();

1 个解决方案

#1


3  

You are increasing column index as well.
Just change from:

您也在增加列索引。只需改变:

cell = row.createCell(i);

To:

cell = row.createCell(0);

So the For Loop will be like:

所以For循环就像:

for(int i=0;i<arr.size();i++){
    row = spreadSheet.createRow((short) i);
    cell = row.createCell(0);
    System.out.println(arr.get(i));
    cell.setCellValue(arr.get(i).toString());
}

Or with fewer lines:

或者用更少的线条:

for(int i=0;i<arr.size();i++){
    row = spreadSheet.createRow((short) i);
    row.createCell(0).setCellValue( arr.get(i).toString() );
    System.out.println(arr.get(i));
}

#1


3  

You are increasing column index as well.
Just change from:

您也在增加列索引。只需改变:

cell = row.createCell(i);

To:

cell = row.createCell(0);

So the For Loop will be like:

所以For循环就像:

for(int i=0;i<arr.size();i++){
    row = spreadSheet.createRow((short) i);
    cell = row.createCell(0);
    System.out.println(arr.get(i));
    cell.setCellValue(arr.get(i).toString());
}

Or with fewer lines:

或者用更少的线条:

for(int i=0;i<arr.size();i++){
    row = spreadSheet.createRow((short) i);
    row.createCell(0).setCellValue( arr.get(i).toString() );
    System.out.println(arr.get(i));
}