Fundamentals 3 min read

Java Method for Writing Data to XLSX Files Using Apache POI

This article presents a Java method that uses Apache POI to write data into XLSX files, detailing the implementation steps, code example, and practical considerations for generating test reports with Excel in automated testing environments.

FunTester
FunTester
FunTester
Java Method for Writing Data to XLSX Files Using Apache POI

The author, while learning Selenium and UiAutomator, explored Excel file manipulation and previously wrote a solution for reading Excel; now shares a method for writing Excel files using a Map<Integer,List<String[]>> as the data source.

//写入xlsx文档
public static void writeXlsx(String filename, Map<Integer,List<String[]>> map) {
    String fileType = filename.substring(filename.lastIndexOf(".") + 1, filename.length()); //提取文件名后缀
    try {
        if (!fileType.equals("xlsx")) { //判断文件名是否正确
            output("文件名错误!");
        }
        XSSFWorkbook wb = new XSSFWorkbook(); //新建工作区
        for(int sheetnum=0; sheetnum<map.size(); sheetnum++) { //遍历表格
            XSSFSheet sheet = wb.createSheet("第" + (sheetnum+1) + "个表格");
            List<String[]> list = map.get(sheetnum+1); //取出需要写入的表格内容,这里需要+1才行
            for(int i=0; i<list.size(); i++) { //遍历行
                XSSFRow row = sheet.createRow(i); //新建行
                String[] str = list.get(i); //取出需要写入的行信息
                for(int j=0; j<str.length; j++) { //遍历写入行单元格
                    XSSFCell cell = row.createCell(j); //创建单元格
                    cell.setCellValue(str[j]); //写入单元格数据
                }
            }
        }
        FileOutputStream outputStream = new FileOutputStream(filename); //新建输出流
        wb.write(outputStream); //写入文件数据
        outputStream.close(); //关闭输出流
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The implementation borrows ideas from earlier work but has been re‑written and optimized to suit the author's specific requirements.

Excel is primarily used here to generate test reports; while simple formatting works, more complex features like merged cells and embedded objects become cumbersome, leading the author to eventually abandon this approach.

Readers are invited to view the original article and join a QQ group for further discussion.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaautomationtestingFile I/OExcelApache POI
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.