使用Apache POI将数据写入Excel电子表格

时间:2022-08-09 20:23:16

I have a Java program to add data into new row in Excel spreadsheet using Apache POI. I am able to print the first 10 row to the spreadsheet but when I tried to print the 11th row, it will somehow create a new row on the first row (without continuing from the previous index) like this:

我有一个Java程序,可以使用Apache POI将数据添加到Excel电子表格中的新行。我可以将前10行打印到电子表格中,但是当我尝试打印第11行时,它会以某种方式在第一行上创建一个新的行(不需要从前一个索引继续),如下所示:

11  Jane    300   //??
12  Jane    300   //??
Emp No. Name    Salary
1   Jane    300
2   Jane    300
3   Jane    300
4   Jane    300
5   Jane    300
6   Jane    300
7   Jane    300
8   Jane    300
9   Jane    300
10  Jane    300

Notice 11th and 12th on top? How do I fix them? This is my complete Java code:

第11和第12题?我怎么修复它们?这是我完整的Java代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

public class Excel {
    public static void main(String[] args) throws Exception {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Sample sheet");

        Map<String, Object[]> data = new HashMap<String, Object[]>();
        data.put("0", new Object[] {"Emp No.", "Name", "Salary"});

        for (int i=1;i<13;i++){

            data.put(i + "", new Object[] {i, "Jane", "300"}); //print in different rows
       }

        Set<String> keyset = data.keySet();
        int rownum = 0;
        for (String key : keyset) {
            Row row = sheet.createRow(rownum++);
            Object [] objArr = data.get(key);
            int cellnum = 0;
            for (Object obj : objArr) {
                Cell cell = row.createCell(cellnum++);
                if(obj instanceof Integer) 
                    cell.setCellValue((Integer)obj);
                else if(obj instanceof Boolean)
                    cell.setCellValue((Boolean)obj);
                else if(obj instanceof String)
                    cell.setCellValue((String)obj);
                else if(obj instanceof Double)
                    cell.setCellValue((Double)obj);
            }
        }

    try {
        FileOutputStream out = 
                new FileOutputStream(new File("new.xls"));
        workbook.write(out);
        out.close();
        System.out.println("Excel written successfully..");

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

}

2 个解决方案

#1


1  

That happens because you put your row numbers into a HashMap, which does not give them back in a useful order.

这种情况发生是因为您将行号放入HashMap中,而这个HashMap并没有返回一个有用的顺序。

Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("0", new Object[] {"Emp No.", "Name", "Salary"});

Why is this a map? Better to use a

为什么这是一张地图?更好的使用

List<Object[]> rows = new ArrayList<>();
rows.add(new Object[] {"Emp No.", "Name", "Salary"});

A list will keep things in the same order you added them.

一个列表会按照你添加的顺序保存。

#2


0  

Your Keyset contains data in order

您的密钥集包含按顺序排列的数据

              [11, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Try to sort these data accoording to your requirement.

尝试根据您的需求对这些数据进行排序。

#1


1  

That happens because you put your row numbers into a HashMap, which does not give them back in a useful order.

这种情况发生是因为您将行号放入HashMap中,而这个HashMap并没有返回一个有用的顺序。

Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("0", new Object[] {"Emp No.", "Name", "Salary"});

Why is this a map? Better to use a

为什么这是一张地图?更好的使用

List<Object[]> rows = new ArrayList<>();
rows.add(new Object[] {"Emp No.", "Name", "Salary"});

A list will keep things in the same order you added them.

一个列表会按照你添加的顺序保存。

#2


0  

Your Keyset contains data in order

您的密钥集包含按顺序排列的数据

              [11, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Try to sort these data accoording to your requirement.

尝试根据您的需求对这些数据进行排序。